insert page number in PDF and little image

Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.

Moderator: Alex

Post Reply
Massimo
Posts: 4
Joined: Mon Aug 05, 2019 3:39 pm

insert page number in PDF and little image

Post by Massimo »

Hi,
I need to insert a page number in a pdf file.
With this code I can to insert page number in bottom left position but I don't understant how to insert in bottom right.
I can't figure out how to transform the value of PDFPage.Size.Height and PDFPage.Size.Width in coordinate to draw text or image in PDF page.

Can you help me ?
Thanks in advance....

Code: Select all

Dim stream As System.IO.Stream = System.IO.File.Open(PDFName, System.IO.FileMode.Open)
        Dim document As PdfDocument = PdfDocumentController.OpenDocument(stream)
        For p As Integer = 0 To document.Pages.Count - 1
            Using g As PdfGraphics = PdfGraphics.FromPage(document.Pages(p))

                Dim font As New Font("Arial", 12)
                Dim brush As New PdfBrush(Color.Black)
                Dim PDFPage As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages(p)
                Dim X As Single = PDFPage.Size.Height
                Dim Y As Single = PDFPage.Size.Width
                Dim Location As New System.Drawing.Point(1, 1)
                g.DrawString(p.ToString("00000"), font, brush, Location)
            End Using
        Next
        document.SaveChanges()
        PdfDocumentController.CloseDocument(document)
Massimo
Posts: 4
Joined: Mon Aug 05, 2019 3:39 pm

Re: insert page number in PDF and little image

Post by Massimo »

at the moment I solved in this way... is the best way ?

Code: Select all

        Dim stream As System.IO.Stream = System.IO.File.Open(PDFName, System.IO.FileMode.Open)
        Dim document As PdfDocument = PdfDocumentController.OpenDocument(stream)
        For p As Integer = 1 To document.Pages.Count - 1
            Using g As PdfGraphics = PdfGraphics.FromPage(document.Pages(p))

                Dim font As New Font("Arial", 12, FontStyle.Bold)
                Dim brush As New PdfBrush(Color.Black)
                Dim PDFPage As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages(p)
                Dim X As Single = PDFPage.Size.Height
                Dim Y As Single = PDFPage.Size.Width

                ' --------------------------
                Dim StringPage As String = p.ToString("00000")
                Dim StringSize As Size = TextRenderer.MeasureText(StringPage, font)
                Dim Location As New System.Drawing.Point(Y - StringSize.Width, 10)
                
                
                g.DrawString(StringPage, font, brush, Location)



            End Using
        Next
        document.SaveChanges()
        PdfDocumentController.CloseDocument(document)
        stream.Close()
        
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: insert page number in PDF and little image

Post by Alex »

Massimo wrote: Mon Aug 05, 2019 8:07 pm at the moment I solved in this way... is the best way ?
...
Your code adds number as vector graphics. Added text is not searchable on PDF page.

Here is optimal code that adds page number using PDF font:

Code: Select all

Dim font As PdfFont = document.FontManager.GetStandardFont(PdfStandardFontType.TimesRoman)
For p As Integer = 1 To document.Pages.Count - 1
    Using g As PdfGraphics = PdfGraphics.FromPage(document.Pages(p))
       Dim brush As New PdfBrush(Color.Black)
       Dim PDFPage As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages(p)
       Dim StringPage As String = p.ToString("00000")
       Dim textWidth As Single
       Dim textHeight As Single
       g.MeasureString(StringPage, font, 12, textWidth, textHeight)
       Dim Location As New System.Drawing.Point(PDFPage.MediaBox.X + PDFPage.MediaBox.Width - textWidth - 10, 10)
       g.DrawString(StringPage, font, 12, brush, Location)
   End Using
Next
Best regards, Alexander
Massimo
Posts: 4
Joined: Mon Aug 05, 2019 3:39 pm

Re: insert page number in PDF and little image

Post by Massimo »

Thanks a lot !
with this method the document size is smaller :-)
Another two advice :
I need to insert a image, for every page, that rappresent a stamp, and a page blank with annotation, is it correct this way ?
Because in document with 25 page , 1200KB, if I insert a image (18 kb, gif) and PDF page (181KB) the final dimension is 2750 KB

Code: Select all

       - insert a image every page
        Dim stream As System.IO.Stream = System.IO.File.Open(PDFName, System.IO.FileMode.Open)
        Dim document As PdfDocument = PdfDocumentController.OpenDocument(stream)
        Dim image As New Vintasoft.Imaging.VintasoftImage("F:\stamp.GIF")
        Dim imageDim As New System.Drawing.SizeF(60, 60)

        For p As Integer = 0 To document.Pages.Count - 1
            Dim PdfPage As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages(p)
            Dim X As Single = PdfPage.Size.Height
            Dim Y As Single = PdfPage.Size.Width
            Dim imageXY As New System.Drawing.PointF(Y - 65, 5)
            Using graphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = PdfPage.GetGraphics()
                Dim imageResource As New Vintasoft.Imaging.Pdf.Tree.PdfImageResource(document, image, PdfCompression.Auto)
                graphics.DrawImage(imageResource, New System.Drawing.RectangleF(imageXY, imageDim))
            End Using
        Next
        document.SaveChanges()
        PdfDocumentController.CloseDocument(document)
        stream.Close()
Thanks in advance.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: insert page number in PDF and little image

Post by Alex »

In your code you are creating new image-resource for each PDF page:

Code: Select all

Dim imageResource As New Vintasoft.Imaging.Pdf.Tree.PdfImageResource(document, image, PdfCompression.Auto)
Do not do this and use 1 image-resource for all PDF pages.

Best regards, Alexander
Massimo
Posts: 4
Joined: Mon Aug 05, 2019 3:39 pm

Re: insert page number in PDF and little image

Post by Massimo »

Great !
Thanks again !
Bye
Post Reply