Is there a way to make PdfImage.Load run quicker

To minimize the ability for people to alter the text values of our PDF certificates but to also keep the final file size as small as possible, we want to use GemBox to load a template PDF, the program then creates a single PNG of all the text for that page and then uses PdfImage.Load to import that PNG from a MemoryStream and then save the PDF. The final file is perfect and all stages process very quickly with the exception of PdfImage.Load which takes, on average, 1241 milliseconds. I apricate the PNG is large but is there anyway to speed the Load function up (we can create up to 200 PDFs in one go, so 1.2 seconds is quite a lot).
I`ve also tried to create each text value as a separate PNG and import them all individually, but that takes even longer.
Any advice speeding it up would be apricated.

Hi Simon,

If the same PNG image is used for all 200 PDFs in one go, then load the PdfImage once, cache that instance of PdfImage, and use it in all 200 PDFs.

Another optimization would be to not use transparency in the PNG image. If transparency is not used, then, in most cases, GemBox.Pdf would be able to simply copy the PNG image to the loaded PdfImage without any decoding and additional processing.

Another solution would be to use digital signatures.
The main purpose of digital signatures is to check if the document has been tampered with after it was signed and to verify the signer / creator of the PDF file.

In many cases the vectorized PDF content (which is also compressed) is smaller than its rasterized counterpart, so the goal of minimizing the PDF file size is questionable.

Regards,
Stipo

Hi Simon,

If I understood correctly, you are replacing the content of all the PDF pages with images, is that right?
That’s why the image sizes are large and cannot be reduced.

I’m afraid that the only way to speed up the PdfImage.Load is to provide a smaller image, but I don’t think that is possible for your context.

Regards,
Mario

Can you show us exactly how you’re doing this?

Dear Mario, Sorry for slow response (no notification of your replies came to my email?).
Code below:

Dim new_bitmap As New Bitmap(2481, 3509)
Dim page As Graphics = Graphics.FromImage(new_bitmap)

'code then creates the image

GemBox.Pdf.ComponentInfo.SetLicense("******")

Dim opt As New GemBox.Pdf.PdfLoadOptions

opt.ReadOnly = True

Dim aa = GemBox.Pdf.PdfDocument.Load(lastBaseImages.basePDF)

Dim pp As GemBox.Pdf.PdfPage = aa.Pages(0)

Dim ms1 As New MemoryStream
new_bitmap.Save(ms1, Drawing.Imaging.ImageFormat.Png)

Dim ii = GemBox.Pdf.Content.PdfImage.Load(ms1)

Dim marg As Double = 0
Dim x, y As Double
x = marg
y = pp.CropBox.Top - marg - ii.Size.Height

Dim trans = GemBox.Pdf.Content.PdfMatrix.Identity
trans.Translate(x, y)

pp.Content.DrawImage(ii, New GemBox.Pdf.Content.PdfPoint(x, y))

aa.Save("C:\SW\123.pdf")

Alternatively, is there a way to create text that is not actually text? i.e. insert the text as a vector graphic so as to make it hard to simply edit it?

That is the part I was asking for, how are you creating an image?

Apologies (still not receiving email notifications of your replies?).

The code is too long to show all, but in short…

Dim new_bitmap As New Bitmap(2481, 3509)
Dim page As Graphics = Graphics.FromImage(new_bitmap)

draw_text(page, "Result", 1, greyBrush, 696, 372, 1680, 66) 'this calls the function to draw the text. Will be repeated for each text item on the page.


Private Sub draw_text(ByRef page As Graphics, text As String, font As Integer, brush1 As Brush, left1 As Single, top1 As Single, width1 As Single, height1 As Single, Optional is_bold As Boolean = False, Optional justification As Int16 = 0, Optional rows As Integer = 1, Optional row_gap_multiplier As Double = 1)

        Dim f1 As Font = font1

        If is_bold Then
            f1 = New Font(f1.FontFamily.Name, f1.Size, FontStyle.Bold)
        End If

        Dim rec As Rectangle
        rec = New Rectangle(left1, top1, width1, height1)

        Dim bp As Bitmap = New Bitmap(rec.Width, rec.Height)
        bp.SetResolution(96, 96) 'needed as GemBox document report calls can set this to 216dpi

        Dim GR As Graphics = Graphics.FromImage(bp)
        GR.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias

        If drawBox Then GR.DrawRectangle(New Pen(New SolidBrush(Color.Blue), 1), 0, 0, rec.Width - 1, rec.Height - 1)

        Dim sf As SizeF
        sf = GR.MeasureString(text, f1)
            If drawBox Then GR.DrawRectangle(New Pen(New SolidBrush(Color.Red), 1), 0, (rec.Height - sf.Height) / 2, sf.Width, sf.Height)

            text = CheckTextWidthbyLetter(text, GR, f1, rec)

            Select Case justification
                Case 0 'left
                    GR.DrawString(text, f1, brush1, 0, (rec.Height - sf.Height) / 2)
                Case 1 'center
                    GR.DrawString(text, f1, brush1, (rec.Width - sf.Width) / 2, (rec.Height - sf.Height) / 2)
                Case 2 'right
                    GR.DrawString(text, f1, brush1, rec.Width - sf.Width, (rec.Height - sf.Height) / 2)
            End Select


        page.DrawImage(bp, New RectangleF(left1, top1, rec.Width, rec.Height))

End Sub

Hi Simon,

Instead of that, can you try saving the PDF page to an image using GemBox.Pdf?
Please check the following examples:

And try the following:

Dim imageOptions = New ImageSaveOptions(ImageSaveFormat.Png) With {
    .DpiX = 96,
    .DpiY = 96,
    .PixelFormat = PixelFormat.Rgb24
}

Using destination = New PdfDocument()
    Using source = PdfDocument.Load("input.pdf")

        For pageIndex As Integer = 0 To source.Pages.Count - 1
            Dim sourcePage = source.Pages(pageIndex)
            Dim destinationPage = destination.Pages.Add()
            destinationPage.SetMediaBox(sourcePage.CropBox.Width, sourcePage.CropBox.Height)

            Using memoryStream = New MemoryStream()
                imageOptions.PageNumber = pageIndex
                source.Save(memoryStream, imageOptions)
                Dim destinationImage = PdfImage.Load(memoryStream)
                destinationPage.Content.DrawImage(destinationImage, New PdfPoint(0, 0))
            End Using
        Next

        destination.Save("output.pdf")

    End Using
End Using

I hope this helps.

Regards,
Mario