Print Page | Close Window

Drawing Page numbers onto PDF document

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: Sample Code
Forum Description: Share Debenu Quick PDF Library sample code with other forum members
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2494
Printed Date: 22 Nov 24 at 2:18PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Drawing Page numbers onto PDF document
Posted By: Ingo
Subject: Drawing Page numbers onto PDF document
Date Posted: 18 Jan 13 at 5:52PM
A sample using/doing pagenumbering with QuickPDF made by our user KerryWales:

public bool RenumberPages(string filename, int startNumber, string outfile, int position, double xOffset, double yOffset)
        {
            //offset in points 1/72 of inch
            //always measurement to nearest edge
            //position
            // 0 = bottom left
            // 1 = top left
            // 2 = top right
            // 3 = bottom right
            bool result = false;
            int pageNo = startNumber;
            double xPos;
            double yPos;
            try
            {
                DebenuPDFLibraryLoadFromFile(InstanceID, filename, "");

                int id = DebenuPDFLibraryAddStandardFont(InstanceID, 4);
                DebenuPDFLibrarySelectFont(InstanceID, id);
                DebenuPDFLibrarySetTextSize(InstanceID, 12);
                   
                for (int i = 1; i <= DebenuPDFLibraryPageCount(InstanceID); i++)
                {
                    DebenuPDFLibrarySelectPage(InstanceID, i);
                    DebenuPDFLibrarySetOrigin(InstanceID, 1);
                    DebenuPDFLibrarySetMeasurementUnits(InstanceID, 0);
                    DebenuPDFLibraryNormalizePage(InstanceID, 1);

                    double pw = DebenuPDFLibraryPageWidth(InstanceID);
                    double ph = DebenuPDFLibraryPageHeight(InstanceID);

                    //calc x & y based on where page number wanted
                    switch (position)
                    {
                        case 0:
                            xPos = xOffset;
                            yPos = ph - yOffset;
                            break;
                        case 1:
                            xPos = xOffset;
                            yPos = yOffset;
                            break;
                        case 2:
                            xPos = pw - xOffset;
                            yPos = yOffset;
                            break;
                        default:
                            xPos = pw - xOffset;
                            yPos = ph - yOffset;
                            break;
                    }
                    DebenuPDFLibraryDrawText(InstanceID, xPos, yPos, "[" + pageNo++.ToString() + "]");
                }
                DebenuPDFLibrarySaveToFile(InstanceID, outfile);
                result = true;
            }
            catch { };
       
            return result;
        }



Replies:
Posted By: brettb
Date Posted: 07 May 14 at 5:50AM
Thanks - most helpful. Here is a similar version for VBA in case others need it.

' In a module or class
Dim qp As DebenuPDFLibraryAX1014.PDFLibrary

'A sample using/doing pagenumbering with QuickPDF made by our user KerryWales:
'offset in points 1/72 of inch
'always measurement to nearest edge
'position
' 0 = bottom left
' 1 = top left
' 2 = top right
' 3 = bottom right

Private Sub RenumberPages(filename As String, startNumber As Long, outfile As String, position As Long, xOffset As Double, yOffset As Double)


    Dim pw As Double, ph As Double
    Dim pageNo As Long
    Dim xPos As Double
    Dim yPos As Double
    Dim fontID As Long
    Dim retval As Long
    Dim i As Long
    pageNo = startNumber
    retval = qp.LoadFromFile(filename, "")
    fontID = qp.AddStandardFont(4)
    Call qp.SelectFont(fontID)
    qp.SetTextSize (8)
   
    For i = pageNo To qp.PageCount() + pageNo - 1
  

        qp.SelectPage (i)
        qp.SetOrigin (1)
        qp.SetMeasurementUnits (0)
        qp.NormalizePage (1)

        pw = qp.PageWidth()
        ph = qp.PageHeight()

        'calc x & y based on where page number wanted
        Select Case (position)

        Case 0:
            xPos = xOffset
            yPos = ph - yOffset

        Case 1:
            xPos = xOffset
            yPos = yOffset

        Case 2:
            xPos = pw - xOffset
            yPos = yOffset
        Case Else:

            xPos = pw - xOffset
            yPos = ph - yOffset

        End Select
       
        qp.SetTransparency (50)
        Call qp.DrawBox(xPos - 10, yPos - 15, 100, 20, 1) ' 1= fill
        qp.SetTransparency (0)
        retval = qp.DrawText(xPos, yPos, "[Credit Page " & Format(i, "000") & "]")
        If Not retval Then
            ' error or warning...
        End If
    Next
    retval = qp.SaveToFile(outfile)
    If Not retval Then
            ' error or warning...
        End If
End Sub


Posted By: GMahler
Date Posted: 06 Jan 16 at 2:57PM
Ingo, thank you to you and KerryWales! Your example was very helpful to me as well... Thumbs Up



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk