Free Support Forum - aspose.com

Header in TOC page: Exception occurs on a document save

Hello.


We need to generate PDF document with TOC. All pages should contain the logo on the top.
What we try to do is to add TOC page, add a document page and assign a header with image to both. When we save the document the “Null Ref. Exception” occurs. The exception is raised only if a TOC page contains a header (or a footer). If it does not - the doc is saved OK.

Here it is the sample code:

Document document = new Document();
Page tocPage = document.Pages.Add();
tocPage.Header = new HeaderFooter();
tocPage.Header.Paragraphs.Add(new TextFragment(“Test”));
tocPage.TocInfo = new TocInfo
{
Title = new TextFragment(“Table of Contents”)
};

Page docPage = document.Pages.Add();
tocPage.Header = new HeaderFooter();
tocPage.Header.Paragraphs.Add(new TextFragment(“Test 2”));

document.Save(“output.pdf”);

For us having the logo in all TOC pages is the critical requirement. We tried to play with ImageStamp, but it is added to the first page only. We do not know in advance how many pages will be in the generated TOC.

Please provide the solution.

As an alternative. Is there a way to add a stamp (image stamp) to every page when generating the document from the scratch?

Hi there,


Thanks for your inquiry. I am afraid you can not add HeaderFooter to a new page. Please note PDF document is created dynamically by API and in order to get real PDF document, it needs to render its programming/dynamic model. So we can render PDF document model by Save(). Please save your document to memory stream and add HeaderFooter later as following, it will help you to accomplish the task.

…<o:p></o:p>

....

MemoryStream ms = new MemoryStream();

document.Save(ms);

document = new Document(ms);

document.Pages[1].Header = new Aspose.Pdf.HeaderFooter();

document.Pages[1].Header.Paragraphs.Add(new TextFragment("Test"));

document.Pages[2].Header = new Aspose.Pdf.HeaderFooter();

document.Pages[2].Header.Paragraphs.Add(new TextFragment("Test 2"));

document.Save(myDir+"output.pdf");


Please feel free to contact us for any further assistance.


Best Regards,

Hi there,

ShmelP:
As an alternative. Is there a way to add a stamp (image stamp) to every page when generating the document from the scratch?

Similarly for adding image stamp to a PDF document, you need to render its structure first by saving it. You can save new document to stream, open it again to iterate through all pages and add Image stamp into header.

Best Regards,

HeaderFooter is added automatically to all new pages if it’s not TOC without any issue. If it’s TOC - the exception (null ref) is thrown - why? Any “null ref” exception on save in the production app should be considered as a bug, shouldn’t it ?


Also the approach works fine if to do the same using “Generator” API.

Hi Dmitry,


We are sorry for the confusion. I have manged to notice issue when adding header to TOC Page and logged a ticket PDFNEWNET-39010 in our issue tracking system for further investigation and resolution. We will notify you as soon as it is resolved.

We are sorry for the inconvenience caused.

Best Regards,

The issues you have found earlier (filed as PDFNEWNET-39010) have been fixed in Aspose.Pdf for .NET 10.7.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.