Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Jun 09, 2015 4:11 pm

I have a PDF template defined and want to add automatic page numbering.

The 'demo' example shows:

Code: Select all
PdfPageNumberField pageNumber = new PdfPageNumberField();
PdfPageCountField pageCount = new PdfPageCountField();
PdfCompositeField pageNumberLabel = new PdfCompositeField();
pageNumberLabel.AutomaticFields = new PdfAutomaticField[] { pageNumber, pageCount };


I am using the Free Spire.PDF for .NET. I don't seem to have the above classes e.g. 'PdfPageNumberField' etc... Is this correct?

Is there any other way of adding page numbers to a PDF template?

dh29
 
Posts: 9
Joined: Mon Mar 02, 2015 9:03 am

Wed Jun 10, 2015 3:07 am

Hello,

Thanks for your inquiry.
I have tested your issue with Free Spire.PDF,it worked well in our side.
The PdfPageNumberField is in Spire.Pdf.AutomaticFields namespace.
Here is the code about template defined and automatic page numbering for your reference.

Code: Select all
PdfDocument doc = new PdfDocument();

            //margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(0.5f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
            SetDocumentTemplate(doc, PdfPageSize.A4, margin);
            doc.SaveToFile("output.pdf");
            System.Diagnostics.Process.Start("output.pdf");
        }
        static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
        {
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            PdfPageTemplateElement bottomSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
            bottomSpace.Foreground = true;
            doc.Template.Bottom = bottomSpace;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));         
            float y = 0;
            PdfPageNumberField pageNumber = new PdfPageNumberField();
            PdfPageCountField pageCount = new PdfPageCountField();
            PdfCompositeField pageNumberLabel = new PdfCompositeField();
            pageNumberLabel.AutomaticFields = new PdfAutomaticField[] { pageNumber, pageCount };       
            pageNumberLabel.Brush = PdfBrushes.Black;
            pageNumberLabel.Font = font;
            pageNumberLabel.StringFormat = format;
            pageNumberLabel.Text = "page {0} of {1}";
            pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - 150, y);
        }


Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Wed Jun 10, 2015 8:38 am

Hi Sweety1,

Thanks for your prompt reply.
Looks like a 'school-boy error' from me...I'd searched the Spire.Pdf-FE.chm help file...but obviously not well enough.
I'll give it a go and let you know how I get on.

Regards,
dh29

dh29
 
Posts: 9
Joined: Mon Mar 02, 2015 9:03 am

Wed Jun 10, 2015 9:51 am

Hi Sweety1,

Page numbering is now working in my PDF template.

Many thanks,
dh29

dh29
 
Posts: 9
Joined: Mon Mar 02, 2015 9:03 am

Thu Jun 11, 2015 2:39 am

Hello,

Thanks for your response.
Please feel free to contact us, if you have any questions or needs. We are here for help.

Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Return to Spire.PDF