Free Support Forum -

Conditional Display of Text in a PDF based on # of pages

I have a requirement to create a pdf document that contains several tables of information. In the event that the information flows onto a second or third page, the users want to see 'Continued' at the bottom of each of the previous pages, and to have the table 'broken' across the page with the header repeated. If it can be contained on one page, they do not want to see 'Continued'.

I have purchased both PDF and PDF.Kit, and it appears that PDF does not know the number of pages until Save() is called. I have been tinkering using PDF in combination with PDF.Kit, by creating placeholders in the footer during the PDF creation. I was able to use PdfContentEditor and the 'ReplaceText' mechanism, but this increased the file size 4x. I cannot get either Kit.Form or Kit.PdfFileMend to work correctly, as I am getting 'Object reference not set ...' on both Form.Save() and PdfFileMend.AddText() even if i remove all processing on the filestream and just attempt to instantiate and save the object. I am using Aspose.Pdf v3.1.7.1 and Aspose.Pdf.Kit v2.1.3.0


Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
MemoryStream tempPDF = new MemoryStream();
MemoryStream m_PDF_Stream = new MemoryStream();
tempPDF.Position = 0;
Aspose.Pdf.Kit.PdfFileInfo info = new Aspose.Pdf.Kit.PdfFileInfo(tempPDF);
tempPDF.Position = 0;
Aspose.Pdf.Kit.Form x = new Aspose.Pdf.Kit.Form(tempPDF,m_PDF_Stream);
if(info.NumberofPages > 1){

Your help would be greatly appreciated. Thanks

Can you please save the tempPDF into a file and send it to me? We will check it and reply to you soon. If the error occurs in the Save method of Aspose.Pdf, please provide the complete code that can reproduce the error.

I have attached the pdf stream as it exists after Aspose.Pdf.Save(), which is working correctly. (you have a very nice product!) The 'object reference not set ...' occurs when I attempt Kit.Form.Save() or Kit.PdfFileMend.AddText() when using two stream objects.

Please confirm that a FormField object, though inheirited from Paragraph, does not render when added to a Header/Footer object.


We tested your document and no form field is found. Can you please provide the code that you used to create the form field?

I don't think my form field is coming through. I had placed it in a footer (which doesn't seem to support form fields). I am also attempting to use Kit.PdfFileMend on the stream created by Aspose.Pdf, which also is causing a Object ref not found. I have attached the code that I am using to create the pdf with the PdfFileMend code enabled.

The pdf that i am creating is normally a one page document that has 3 copies of the document attached. (Normally 4 pages in length). In the situation where the initial document is more than one page in length, then they want 'Continued' marked at the bottom of pages 1-> (N-1).

Any thoughts on a better way to do this other that coming back through in Pdf.Kit once it is created by Pdf?

Currently Aspose.Pdf can't support your requirement directly. As a workaround, you can add "continued" as footer of all pages and use a blank to overlap the footer of last page. Here is an example:

Pdf pdf1 = new Pdf();
Section sec1 = pdf1.Sections.Add();

//create the footer
HeaderFooter hf1 = new HeaderFooter();
sec1.OddFooter = sec1.EvenFooter = hf1;
hf1.Paragraphs.Add(new Text("(Continued)"));

//create 3 pages
for(int j = 1; j <= 3; j++)
Text t = new Text("page " + j.ToString());
t.IsFirstParagraph = true;

//create a blank to overwrite the footer of last page
Graph g1 = new Graph(200,50);
Rectangle r1 = new Rectangle(g1,0,0,200,50);
r1.GraphInfo.Color = new Color("White");
r1.GraphInfo.FillColor = new Color("White");
r1.GraphInfo.IsFilled = true;
g1.PositioningType = PositioningType.PageRelative;
g1.Top = sec1.PageInfo.PageHeight - sec1.PageInfo.Margin.Bottom;
g1.Left = sec1.PageInfo.Margin.Left;