Sequential Page Numbering - Multi-PDF Merge Document

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Merger for .NET (v4)  /  Re: Sequential Page Numbering - Multi-PDF Merge Document

DynamicPDF Merger for .NET (v4) Forum

Hello,

I am creating a document that has the following properties:

1)  First page is an existing PDF
2)  Pages 2 through N are from one of three existing PDFs
3)  Pages N+1 through X are from a PDF created using the Generator
4)  [Optional] Pages X+1 - Z are from an existing PDF

I would like to merge all of these documents together (which I can do already) and then add page numbers to each page within the completely merged document.  I have templates on all of the pages constructed with the Generator.

For example:

PDF1 - 1 page
PDF2 - 6 pages
PDF3 - 178 pages
PDF4 - 15 pages

With the above scenario I would want to have page 1 have "1 of 200" at the bottom of the page and each subsequent page would have "n of 200" until n=200 for "200 of 200"

Is there any way to do this?

Thanks!

Matt L.
Posted by a ceTe Software moderator
Hello Matt,

You can apply the page numbering for the entire merged document using the sample code below:

VB.NET Code:
Dim MyDocument As MergeDocument = New MergeDocument
Dim documentTemplate As Template = New Template()
MyDocument.Template = documentTemplate
documentTemplate.Elements.Add( New PageNumberingLabel( _
            "%%CP%% of %%TP%%", 0, 680, 512, 12, Font.Helvetica, _
            12, TextAlign.Center ) )

C# Code:
MergeDocument document = new MergeDocument();
Template documentTemplate = new Template();
document.Template = documentTemplate;
documentTemplate.Elements.Add( new PageNumberingLabel( 
            "%%CP%% of %%TP%%", 0, 680, 512, 12, Font.Helvetica, 
            12, TextAlign.Center ) );

Thanks,
ceTe Software Support Team
How about if I wanted to exclude the very first page, which will be a cover page that is an image?
Posted by a ceTe Software moderator
---- Edited ----

The best way to accomplish this is to set the Page object's ApplyDocumentTemplate property to false. This can be done to prevent the document template from appearing on any page.

---- Original Post Below ----

Hello,

In that case, you will append the first page, begin a new section and apply the template only to that section. That way the page numbering will start from Page2 onwards.

Dim MyDocument As MergeDocument = New MergeDocument
Dim documentTemplate As Template = New Template()
documentTemplate.Elements.Add( New PageNumberingLabel( _
            "%%CP%% of %%TP%%", 0, 680, 512, 12, Font.Helvetica, _
            12, TextAlign.Center ) )
MyDocument.Append(PDF1)
MyDocument.Sections.Begin(documentTemplate)

C# Code:
MergeDocument document = new MergeDocument();
Template documentTemplate = new Template();
documentTemplate.Elements.Add( new PageNumberingLabel( 
            "%%CP%% of %%TP%%", 0, 680, 512, 12, Font.Helvetica, 
            12, TextAlign.Center ) );
document.Append(PDF1);
document.Sections.Begin(template);

Thanks,
ceTe Software Support Team
The code below only gives me a page numbering label on the 2nd page of the document only (which is where it should start) but doesn't show on any subsequent pages:

 // Create the main merge document to use and start with the coverPagePDF PdfDocument object
            MergeDocument mdPDF = new MergeDocument(coverPagePDF);

            // Add page numbering to all but the first page
            ceTe.DynamicPDF.Template docTemplate = new ceTe.DynamicPDF.Template();
            docTemplate.Elements.Add(new ceTe.DynamicPDF.PageElements.PageNumberingLabel("%%CP%% of %%TP%%", 0, 720, 550, 12, ceTe.DynamicPDF.Font.Helvetica, 12, ceTe.DynamicPDF.TextAlign.Center));
            mdPDF.Sections.Begin(docTemplate);

            // Append the introduction document
            if (introductionPDF != null)
            {
                mdPDF.Append(introductionPDF);
            }

            // Append the content document
            mdPDF.Append(pdfContentPDF);

            // Append the pharmacy document
            if (pharmacyPDF != null)
            {
                mdPDF.Append(pharmacyPDF);
            }
Posted by a ceTe Software moderator
Hello,

Can you please send over the PDF documents you are appending to our Support Team so that we can look into it further?

Thanks,
ceTe Software Support Team
One wierd thing that I did notice was that by using the code I sent you it applies the color "#666666" to the pagenumberinglabel AND the first 2 paragraphs of the first page of the content.  It's as though the label starts on the coverpage and goes to the first page of the introduction then stops for some reason.
Posted by a ceTe Software moderator
Hello,

Please make sure you are using version 4.0.3 and build 70322 or later. There was an issue resolved with this build that should resolve that. It caused section templates not to appear correctly after the first merged page when using MergeDocument.Append().

Thanks,

ceTe Software Support Team
I am not... I was using v4.0.0 build 65243 (or close) and I am in the process of updating now.  I will keep you posted.
So now instead of having a "ceTe.DynamicPDF.dll" there are multiple ones that end in .10.dll, .11.dll and .20.dll.  I assume that if I am using .NET 2.0 that I would use the ".20.dll" file correct? 
Hmm... it "kind of" worked.  I now have page numbers on all pages that do not already have a template associated with them. I.E. the pages that were not created by the Generator.

Also, it made most of my text go from Black to the color I am using for my pageNumberingLabel of "#666666".

Odd behavior!

Any ideas?
Posted by a ceTe Software moderator
Hello,

I'm new to this thread but want to step in and make sure everything gets resolved for you.

On the 3rd post you stated the document template was working fine but that you simply wanted the template not to appear on the first page. The best way to accomplish this is to set the Page object's ApplyDocumentTemplate property to false. This can be done to prevent the document template from appearing on any page.

On the 10th post you asked about the names of the differant DLLs. The one ending in .10 is for the .NET Framework 1.0, the one ending in .11 is for the .NET Framework 1.1 and the one ending in .20 is for the .NET Framework 2.0 and 3.0. more details can be found here:
  Referencing the Assembly

In your last post, you mentioned that the section template was not being applied to the pages created by Generator. You will need to apply the template to the MergeDocument object if you are appending a Document object to it. Make sure you are starting the section on the MergeDocument object before appending the Document object.

Can you explain what you mean by text changing colors? If you set the text on the Page Numbering Label, only the text for the Page Numbering Label will change. Could you be using a variable and applying it to more than one page element?

Thanks,

ceTe Software Support Team
Okay... let me see if I can explain myself a little better.

In regards to your first item, the document template was working fine however it showed the page number on all pages and I didn't want it to show or start on the cover page.  I need it to start from the page after the cover and have the page numbering start at 1.  When I apply the document template to the MergeDocument it shows up on the 2nd page (which I want) however it does a couple of things that are not desireable.  A) It makes the header of that page and the first 2 paragraphs the color of the pageNumberingLabel as opposed to black.  B) It starts on page 2 of XXX instead of page 1 of XXX.

Second, I have already referenced the .20.dll for the .NET 2.0 Framework.

Third, I have sent you my code and what I do is I create the MergeDocument with my CoverPagePDF as the argument.  Then I create a new section and apply a document template to the new section with (mdPDF.Sections.Begin(docTemplate)).  Next, I append the Introduction PDF document to the MergeDocument.  Next, I append the Generated document that I have written to a MemoryStream and created a PdfDocument object out of.  Last, I append my final pages which are optional.  This results in the following:

PDF1(Cover Page) - Created fine

PDF2 (Introduction) - First 2 paragraphs and header of this EXISTING PDF document are changed to a color of "#666666" when they should be black.  This page(s) has the numbering label at the bottom.

PDF3 (Generated Document) - No numbering label is present on these pages and all of the content is in the color of the label "#666666" and should be black instead.  Each of these pages has a template assigned to its page by the following code when I generate these pages:

-- m_ProviderPages is a generic list of pages as follows
-- private List<ceTe.DynamicPDF.Page> m_ProviderPages;

if (m_ProviderPages != null)
            {
                if (m_ProviderPages.Count > 0)
                {
                    // Set section template (provider template)
                    pdf.Sections.Begin(GetTemplate("Provider"));

                    foreach (ceTe.DynamicPDF.Page provPage in m_ProviderPages)
                    {
                        provPage.ApplySectionTemplate = true;
                        pdf.Pages.Add(provPage);
                    }
                }
            }

PDF4 (Optional pages which may or may not be added) - Has the page numbering label, but again the header is a font color of "#666666" when it should be black.

Does this clarify things?

I have also sent you the document that it generates so you can see this first hand.
Posted by a ceTe Software moderator
Hello,

Thanks for the clarifications. We found a bug that was causing some of your text to change colors when applying the template. We have a new build available that resolves the issue and will post it for download shortly. In the mean time, please contact Support if you need this build.

Here is some sample code that shows how to prevent the document template from appearing on the first page and having the second page start numbering at 1. This simpler than using section templates, if you only need to prevent the document template from showing on one or few pages:
  MergeDocument doc = new MergeDocument( "C:\\CoverPage.pdf" );
  Template temp = new Template();
  PageNumberingLabel pageNumberingLabel = new PageNumberingLabel( "%%CP%% of %%TP%%", 0, 755, 612, 12, Font.Helvetica, 12, ceTe.DynamicPDF.TextAlign.Center, RgbColor.Red );
  pageNumberingLabel.PageOffset = -1;
  temp.Elements.Add( pageNumberingLabel ) );
  doc.Template = temp;
  doc.Pages[0].ApplyDocumentTemplate = false;
  doc.Append( "C:\\SecondDocument.pdf" );
  doc.Append( "C:\\ThirdDocument.pdf" );
  doc.DrawToWeb( "Output.pdf" );

Thanks,

ceTe Software Support Team
Thank you very much for the support.  There is still one issue that I cannot figure out.  The page numbering label works great and is appearing on the pages that are from existing PDFs, however there are still no page numbers on the pages that I am creating using the DynamicPDF Generator.  Those pages have templates applied to them for a header and footer with content being placed in the middle of the page.

Any ideas on this?
Posted by a ceTe Software moderator
Hello,

We were able to find out the exact cause of this behavior. When importing PDF documents created with our Generator product, it sets the default margins of 50 on all four sides and so the page numbering label goes beyond that and does not show up. A good workaround to take care of this problem is using the AnchorGroup page element and set its AnchorTo property to either Margins or edge as per your requirement. Add the PageNumberinglabel to the AnchorGroup and add the AnchorGroup to the template. Following is the sample code:

Template temp = new Template();
PageNumberingLabel pageNumberingLabel = new PageNumberingLabel("%%CP%% of %%TP%%", 0, 0, 612, 12, Font.Helvetica, 12, ceTe.DynamicPDF.TextAlign.Center, RgbColor.Red);
pageNumberingLabel.PageOffset = -1;

AnchorGroup group = new AnchorGroup(pageNumberingLabel.Width,pageNumberingLabel.Height,Align.Center,VAlign.Bottom);
group.AnchorTo = AnchorTo.Margins;
group.Add(pageNumberingLabel);

temp.Elements.Add(group);
doc.Template = temp;

Thanks,
ceTe Software Support Team
You guys rule!  I am going to test this right now!
That worked fantastic!

The only thing I had to change was the height of my page numbering label and then do an "AnchorTo.Edges" to keep the page numbering label at the same position on all pages.

Once again, thank you so very much for all of your time,

Matt L.
HI Team ,

We are using ceTe.DynamicPDF.20.dll of version 6.0.4.20  . We have a requirement to start the pdf page numbering not from 1 but from some other number . Is that possible , Please suggest how can we accomplish that .
Posted by a ceTe Software moderator
Hello,

You can achieve your requirement without any problem using DynamicPDF for .NET API. You will need to merge the PDFs and then create a PageNumberingLabel  object and set the desired PageOffset. Also set the ApplyDocumentTemplate property to false for each page in merged output PDF on which you don’t want to apply page number. Then add the PageNumberingLabel to Template object and set it to the MergeDocument class. Below is the code sample for adding page number starting from third page in the merged output PDF.

            MergeDocument document = new MergeDocument();
            document.Append(@"D:\temporary\DocumentA.pdf");
            document.Append(@"D:\temporary\DocumentB.pdf");
            Template template = new Template();
            PageNumberingLabel pageNumberingLabel = new PageNumberingLabel( "%%CP%% of %%TP%%", 200, 600, 100, 12, Font.Helvetica, 12, ceTe.DynamicPDF.TextAlign.Center, RgbColor.Red );
            pageNumberingLabel.PageOffset = -2;
            template.Elements.Add(pageNumberingLabel);
            document.Template = template;
            document.Pages[0].ApplyDocumentTemplate = false;
            document.Pages[1].ApplyDocumentTemplate = false;
            document.Draw( @"C:\Temp\Output.pdf" );

Thanks,
ceTe Software Support Team.
Hi ,


I understood from your post that We need to first merge the PDFs and then set ApplyDocumentTemplate to false wherever not needed for the page number .

My requirement is little different , I do not have PDFs to merge but I need is to create PDF document itself with page numbering starting from some random "X" and not from Default value "1" . Is that possible . Please suggest .
Posted by a ceTe Software moderator
Hello,

Yes, you can create PDF from scratch and then add page numbers starting from the desired page in the PDF. Here is the sample code:

            Document document = new Document();
            //Add pages to the Document.
            for (int i = 0; i < 10; i++)
            {
                Page page = new Page();
                Label label = new Label("Test label on page:"+(i+1), 100, 200, 200, 20);
                page.Elements.Add(label);
                document.Pages.Add(page);
            }
            Template template = new Template();
            PageNumberingLabel pageNumberingLabel = new PageNumberingLabel("%%CP%% of %%TP%%", 200, 600, 100, 12, Font.Helvetica, 12, ceTe.DynamicPDF.TextAlign.Center, RgbColor.Red);
            pageNumberingLabel.PageOffset = -2;
            template.Elements.Add(pageNumberingLabel);
            document.Template = template;
            document.Pages[0].ApplyDocumentTemplate = false;
            document.Pages[1].ApplyDocumentTemplate = false;
            document.Draw(@"C:\Temp\Output.pdf");

Thanks,
ceTe Software Support Team.
Yes it works now , thank you very much for your support.

All times are US Eastern Standard time. The time now is 11:44 PM.