Free Support Forum - aspose.com

Table of Contents AND HeaderFooter

Hi,

Having problems adding a TOC AND a HeaderFooter - seems I can have one but not the other. Can you shed some light on this problem? Also please show example code; I have tried this for three days now with no success. Thank you!

Header and footer are not supported in the TOC page currently. We will add support for this later.

Thanks for the quick reply.

To be more specific about my question, it seems Aspose currently does not support the TOC and any HeaderFooter anywhere in the Pdf document at all - in other words, you cannot have a TOC and then have any HeaderFooters in the same PDF doc; you have to choose whether you want TOC or HF in your Pdf output.

Your answer seemed to indicate that I wanted a HeaderFooter on the TOC page itself, but I don't want that - I want a TOC as the second page (after the cover sheet) and then subsequent pages (pages 3 to n) to have a HeaderFooter, and this does not currently work in Aspose.

As i indicated earlier, the TOC works all by itself, and the HF works all by itself, but they do not work together in a single Pdf doc. I would think many users would want to take advantage of both TOC and HF Aspose features in the same Pdf document.

Thanks for considering Aspose.

Please try the following codes which show how to use Class TOC and HeaderFooter in a same pdf called TOCHeaderFooter.pdf, but not in the same page.

Pdf pdf = new Pdf();

//Create the Table Of Contents. Add it to the pdf like a common Section.
ListSection tocSection = new ListSection("Table Of Contents");
tocSection.ListType = ListType.TableOfContents;
pdf.Sections.Add(tocSection);

//Define the format of the four levels' list.
tocSection.ListFormatArray.Length = 4;
tocSection.ListFormatArray[0].LeftMargin = 0;
tocSection.ListFormatArray[0].TextInfo.IsTrueTypeFontBold = true;
tocSection.ListFormatArray[0].TextInfo.IsTrueTypeFontItalic = true;
tocSection.ListFormatArray[1].LeftMargin = 10;
tocSection.ListFormatArray[1].TextInfo.IsUnderline = true;
tocSection.ListFormatArray[1].TextInfo.FontSize = 10;
tocSection.ListFormatArray[2].LeftMargin = 20;
tocSection.ListFormatArray[2].TextInfo.IsTrueTypeFontBold = true;
tocSection.ListFormatArray[3].LeftMargin = 30;
tocSection.ListFormatArray[3].TextInfo.IsTrueTypeFontBold = true;

//Add four headings.
Aspose.Pdf.Section sec1 = pdf.Sections.Add();
for (int Level = 1;Level != 5; Level++)
{
Heading heading2 = new Heading(pdf,sec1,Level);

Segment segment2 = new Segment(heading2);
heading2.Segments.Add(segment2);
heading2.IsAutoSequence = true;
segment2.Content = "this is heading of level ";
segment2.Content += Level.ToString();
//Add the heading into Table Of Contents.
heading2.IsInList = true;
sec1.Paragraphs.Add(heading2);
}

Aspose.Pdf.Section section = new Aspose.Pdf.Section(pdf);
pdf.Sections.Add(section);

Aspose.Pdf.HeaderFooter hf1 = new Aspose.Pdf.HeaderFooter(section);
section.OddHeader = section.EvenHeader = hf1;
hf1.IsFirstPageOnly = true;

Text text = new Text(hf1);
hf1.Paragraphs.Add(text);
Segment segment = new Segment(text);
text.Segments.Add(segment);
segment.Content = "header for first page";

Aspose.Pdf.HeaderFooter hf2 = new Aspose.Pdf.HeaderFooter(section);
section.AdditionalOddHeader = hf2;
hf2.IsSubsequentPagesOnly = true;

text = new Text(hf2);
hf2.Paragraphs.Add(text);
segment = new Segment(text);
text.Segments.Add(segment);
segment.Content = "odd header for subsequent pages";

Aspose.Pdf.HeaderFooter hf3 = new Aspose.Pdf.HeaderFooter(section);
section.AdditionalEvenHeader = hf3;
hf3.IsSubsequentPagesOnly = true;

text = new Text(hf3);
hf3.Paragraphs.Add(text);
segment = new Segment(text);
text.Segments.Add(segment);
segment.Content = "even header for subsequent pages";

for(int i = 1; i <= 6; i++)
{
Text text1 = new Text(section);
text1.IsFirstParagraph = true;
section.Paragraphs.Add(text1);
Segment segment1 = new Segment(text1);
text1.Segments.Add(segment1);
segment1.Content = "This is the page NO." + i.ToString();
}

pdf.Save("TOCHeaderFooter.pdf");

Thanks for your prompt response.

yes, your example works of course, but we typically have multiple Sections on a single Pdf page, and not just one new section per page;

and also we have found that in order to add anything to the TOC ListSection object we must first add a new Section to the Pdf document, which sometimes is not possible, since the new TOC entry we wish to add is a table which is a part of a Section has already been added as part of a Section in the TOC...

also we're noticing the Page numbering in the footer is nto accurate any more after using TOC with HeaderFooters (in other words, the syntax "$p of $P" does not reflect the page number accurately.)

thanks for any help on these issues.

Thanks for considering Aspose.

1. Class Section in Aspose.Pdf is corresponding to the concept section in MS Word. You could set the property IsNewPage whether to created a new page by a new section or not. The following is sample codes to explain it's usage.

Pdf pdf = new Pdf();
Aspose.Pdf.Section section1 = pdf.Sections.Add();
Text text1 = new Text(section1,"This is text in section1.");
text1.Margin.Top = 30;
section1.Paragraphs.Add(text1);

Aspose.Pdf.Section section2 = pdf.Sections.Add();

//Set the section2 on the same page of section1.
section2.IsNewPage = false;
Text text2 = new Text(section2,"This is text in section2.");
text2.Margin.Top = 30;
section2.Paragraphs.Add(text2);

pdf.Save("SectionsOnAPage.pdf");

2. Could u give more ideals about - "since the new TOC entry we wish to add is a table which is a part of a Section has already been added as part of a Section in the TOC...", I don't extactly understand what is you meaning.

3. Please post the codes which can reproduce the page number errors.

Thanks for your coorperations.

Thanks for your prompt response.

in #1, I understand that IsNewPage = false allows multiple Sections on a single page; I am questioning whether adding 4-5 Sections per page has any problems with #3 which is page numbering in a HeaderFooter. The reason I say this is because the HeaderFooter with page numbering using the syntax ("$p of $P") seems to work flawlessly when there is but one Section on a page - if I add more than one Section on a page, and especially if the last Section is not kept together and flows on to the next page, the page numbers grow exponentially per the Sections I add on the page - in other words, there seems to be some correlation to the number of Sections I add, and the page numbers. Can you please verify? thanks.

in #2 what I am trying to say is that in order for any entry to get into the TOC ListSection, a new Section must be added, or at the very least a new Header must be added, referencing an existing Section that had been previously added to the TOC. This has been my experience thus far - can you please verify? thanks.

in #3, I'm afraid my code is too long to post! I can try to generate a sample of what I'm attempting to do with Aspose, but this will take some time, and I wanted to post back right away in case you have any quick suggestions on the above issues.

THANKS!

hi, i have created a small sample that will display page numbers, or at least the page count, incorrectly:

Pdf pdf = new Pdf();

HeaderFooter hf = new HeaderFooter();
Text text = new Text("page $p of $P");
hf.Paragraphs.Add(text);

Section sec1 = pdf.Sections.Add();
Text t = new Text("section one");
sec1.Paragraphs.Add(t);
sec1.OddHeader = sec1.EvenHeader = hf;
sec1.AdditionalEvenHeader = sec1.AdditionalOddHeader = hf;

Section sec2 = pdf.Sections.Add();
sec2.IsNewPage = false;
Text t2 = new Text("section two");
sec2.Paragraphs.Add(t2);

Section sec3 = pdf.Sections.Add();
sec3.IsNewPage = true;
Text t3 = new Text("section three");
sec3.Paragraphs.Add(t3);

// a big section that spills out onto another page:
Section sec4 = pdf.Sections.Add();
sec4.IsNewPage = false;
Text t4 = new Text("section four");
sec4.Paragraphs.Add(t4);

int iterations = 50;
for(int i = 0; i < iterations; i++)
{
Text textI = new Text("Because the facts of Homer's life -- when he was born or died, where he lived, who he was -- remain unknown and shall most likely never be known, many scholars have doubted the existence of a 'Homer' and point to his texts as the work of a collection of authors over a long period of time. This criticism stems from a disbelief that epics such as The Iliad and The Odyssey could have been formulated, maintained, and transmitted within an oral culture. However, new research on human memory and careful analysis of text reveals evidence that the textual style of each poem does emanate from one author.\n");
sec4.Paragraphs.Add(textI);
}

pdf.Save("sections.pdf",SaveType.OpenInBrowser,Response);

Hi,

Two things to add to this HeaderFooter and page number issue and discussion:

1. it seems that the very FIRST Section on a page dictates how the HF will be used in the page; for example, if I have two Sections, and the first Section does not have the HF set, but the second Section does, the page will NOT display the HF; but of course if the first Section has the HF set, regardless of whether the second section on the same page has the HF set or not, the HF displays.

example code:

Pdf pdf = new Pdf();

HeaderFooter hf = new HeaderFooter();
Text text = new Text("page $p of $P");
hf.Paragraphs.Add(text);

Section sec1 = pdf.Sections.Add();
Text t = new Text("section one");
sec1.Paragraphs.Add(t);
// sec1.OddHeader = sec1.EvenHeader = hf; // <-- when commented, HF does NOT display; uncomment, and HF OK

Section sec2 = pdf.Sections.Add();
sec2.IsNewPage = false;
Text t2 = new Text("section two");
sec2.Paragraphs.Add(t2);
sec2.OddHeader = sec2.EvenHeader = hf;
pdf.Save("sections.pdf",SaveType.OpenInBrowser,Response);

2. and also, it seems that if a Section has IsNewPage = false, and the Section spills onto another page, the HF does NOT work; but when the exact same Section has IsNewPage = true, and the Section spills onto another page, the HF displays just fine. This goes along with what i'm saying in #1, that the very first Section on a page "controls" the HF for that page.

example code (insert the code below into the code above in #1 just before the "pdf.Save" code line) :

// a big section that spills out onto another page:

Section sec4 = pdf.Sections.Add();

sec4.IsNewPage = false; // < -- will not display the HF; but if you set the value to True, the HF will display on all pages!

Text t4 = new Text("section four");

sec4.Paragraphs.Add(t4);

sec4.OddHeader = sec4.EvenHeader = hf;

int iterations = 10;

for(int i = 0; i < iterations; i++)

{

Text textI = new Text("Because the facts of Homer's life -- when he was born or died, where he lived, who he was -- remain unknown and shall most likely never be known, many scholars have doubted the existence of a 'Homer' and point to his texts as the work of a collection of authors over a long period of time. This criticism stems from a disbelief that epics such as The Iliad and The Odyssey could have been formulated, maintained, and transmitted within an oral culture. However, new research on human memory and careful analysis of text reveals evidence that the textual style of each poem does emanate from one author.\n");

sec4.Paragraphs.Add(textI);

}

in #1, you are right. the very FIRST Section on a page dictates how the HF will be used in the page.

in #2, is it possible to add a section before the TOC section. Here is an example:
Section cover = new Section();
cover.Paragraphs.Add(new Text("cover"));
pdf1.Sections.Insert(0,cover);

As for the page number problem, I tested your code and have not found any error. The resulting pdf is attached.

I think the key problem is the understanding of relation between Section and HeaderFooter.

To the above two things, here is my understanding.

If multi-sections need to show on the same page, then this page's HF is decided by the very first section. That is to say, if 1st section set HF, it will display on the page, if not set, it will not display whatever the latter sections set or not. In other words, HF only works for the VERY first section if multi-sections on one page. Please try the following codes.

+++++++++++++++++++++++++++++++++++++++++++++++

Pdf pdf = new Pdf();

Aspose.Pdf.HeaderFooter hf = new Aspose.Pdf.HeaderFooter();
Text text = new Text("page $p of $P");
hf.Paragraphs.Add(text);

Aspose.Pdf.Section sec1 = pdf.Sections.Add();
Text t = new Text("section one");
sec1.Paragraphs.Add(t);
sec1.OddHeader = sec1.EvenHeader = hf;
sec1.AdditionalEvenHeader = sec1.AdditionalOddHeader = hf;

Aspose.Pdf.Section sec2 = pdf.Sections.Add();
sec2.IsNewPage = false;
Text t2 = new Text("section two");
sec2.Paragraphs.Add(t2);

Aspose.Pdf.Section sec3 = pdf.Sections.Add();
sec3.IsNewPage = true;
Text t3 = new Text("section three");
sec3.Paragraphs.Add(t3);

// a big section that spills out onto another page:
Aspose.Pdf.Section sec4 = pdf.Sections.Add();
sec4.IsNewPage = false;
Text t4 = new Text("section four");
sec4.Paragraphs.Add(t4);

sec4.OddHeader = sec4.EvenHeader = hf;

int iterations = 50;
for(int i = 0; i < iterations; i++)
{
Aspose.Pdf.Text textI = new Aspose.Pdf.Text("Because the facts of Homer's life -- when he was born or died, where he lived, who he was -- remain unknown and shall most likely never be known, many scholars have doubted the existence of a 'Homer' and point to his texts as the work of a collection of authors over a long period of time. This criticism stems from a disbelief that epics such as The Iliad and The Odyssey could have been formulated, maintained, and transmitted within an oral culture. However, new research on human memory and careful analysis of text reveals evidence that the textual style of each poem does emanate from one author.\n");
sec4.Paragraphs.Add(textI);
}

pdf.Save("sections.pdf");

+++++++++++++++++++++++++++++++++++++++++++

  1. On page 1, sec1 and sec2 on the same page, HF displays because of the setting of sec1, whereas no setting of sec2.
  2. On page2, sec3 and sec4 on the same page, HF doesn't display because of no setting of sec3 whereas setting of sec4.
  3. On the latter, HF displays because of setting of sec4.

Hi, Thanks for all of your considerations and effort on this problem.

The problem is, that like many users who need to create Pdf documents dynamically, i have no way of telling whether the Section i'm adding to a page is going to be the first Section on a page; and therefore, this HF model does not work at all for me.

In other words, I have ten to twenty Sections to add per Pdf document. For example, in Document #1 the Sections 1,2,3 fit on one page, and the rest of the Sections spread out across the next ten pages. Document #2 has Section 1 flowing across three pages, then Sections 2,3 on the next page, then the rest of the Sections spread out across the next twenty pages.

My solution was to add the HF to each and every Section, so if the Section happens to be the first Section on the page, it would create the correct page number in the HF - but this does not work. When using this approach, the HF seemingly generates new page numbers as Sectiosn are added and spill out over two or more pages, and the total page count becomes higher than the actual page count. The page numbers in the TOC are also somehow influenced by the incorrect page number generated by the HF, so even if I don't display the incorrect HF, the TOC has incorrect page numbers. Lastly, the biggest problem is that the HF page numbers do not match the Adobe Reader's page numbers, which is confusing for the user.

Thanks for trying, I think this issue is unsolvable by Aspose! I have worked with your product for a few months now, so I can't believe my understanding of how the HF and TOC and Sections work is any less than any other developer trying implement your product. Please, prove me wrong, and enlighten me! I can't be the first Aspose user to require dynamic Pdf Sections and also a TOC and HF that displays page numbers!

Thank you for your prompt response.

There seems to be a communication problem here - I'm afraid I'm not clear in what I'm asking - I need to find a way for Aspose to put page numbers on a Pdf document with multiple Sections per page that are dynamically added, and the Sections vary in length.

#1, thank you for verifying this - to reiterate, the first Section on a page, if that Section has a HeaderFooter set, it dictates how the HeaderFooter will be used in the page.

#2 - I didn't ask about this type of functionality, and this is not a requirement for our Pdf document (i'm still confused as to how you even thought this was a concern of ours - thanks for providing this information, but we don't need this knowledge right now.)

#3 - your test.pdf you have attached has a HeaderFooter on the first page only - my question was, how does the subsequent Sections / pages get the HeaderFooter to display page numbers, if the IsNewPage=false ? That is the answer I'm looking for. Thanks.

Thanks for considering Aspose.

Could u provide a Word document which is a best explaination of your requirement? We will consider if Aspose.Pdf can meet the desire. I think it is a good way to interact with eath other.

Thank you for your response.

i cannot provide a Word document for requirements; this is not at all an extra-ordinary request for Pdf output; i want to know if the Aspose.Pdf product can do it, and if so, HOW to do it. The request is simple:

1. Pdf document has a Table of Contents (with correct page numbers!)

2. Pdf document has a header and a footer on every Pdf page (with correct page numbers!)

I'm not sure why it is so difficult to understand my request, but i can tell you I'm getting a little frustrated.

Please find attached a sample Pdf document created with Aspose.Pdf that is typical of the Pdf output we need. Please NOTE the page numbers are incorrect - the question to the Aspose.Pdf support team is HOW TO FIX IT ! Thanks.

This is really strange. Can you please provide the code or an simple example that can reproduce this error? We will fix this bug soon.

Thank you for your response.

I have already previously posted this code and sample Pdf documents; I am posting it again below for the last time. The code below produces and error in the Aspose.Pdf HeaderFooter page numbering. To be specific, here is what is displaying in the Adobe Reader:

1. Sections 1 and 2 start and end on the first page of the Pdf document, with a HeaderFooter displaying the text "page 1 of 4" (incorrect - the Adobe Reader displays "1 of 3" at the bottom.)

2. Section 3 and 4 both start on the second page, and Section 4 continues on to the third page. The HeaderFooter on the second page displays "page 2 of 4" while the Adobe Reader displays "2 of 3".

3. on the third page, Section 4 completes its output, and Section 5 starts and ends. The HeaderFooter on the third page displays "page 4 of 4" and the Adobe Reader displays "3 of 3".

If this is a bug, great. I look forward to the fix. If this is the intended behavior, I cannot with a clear conscience recommend or use your product.

code:

Pdf pdf = new Pdf();

HeaderFooter hf = new HeaderFooter();

Text text = new Text("page $p of $P");

hf.Paragraphs.Add(text);

Section sec1 = pdf.Sections.Add();

Text t = new Text("section one");

sec1.Paragraphs.Add(t);

sec1.OddHeader = sec1.EvenHeader = hf;

Section sec2 = pdf.Sections.Add();

sec2.IsNewPage = false;

Text t2 = new Text("section two");

sec2.Paragraphs.Add(t2);

sec2.OddHeader = sec2.EvenHeader = hf;

Section sec3 = pdf.Sections.Add();

sec3.IsNewPage = true;

Text t3 = new Text("section three");

sec3.Paragraphs.Add(t3);

sec3.OddHeader = sec3.EvenHeader = hf;

// a big section that spills out onto another page:

Section sec4 = pdf.Sections.Add();

sec4.IsNewPage = false;

Text t4 = new Text("section four");

sec4.Paragraphs.Add(t4);

sec4.OddHeader = sec4.EvenHeader = hf;

int iterations = 10;

for(int i = 0; i < iterations; i++)

{

Text textI = new Text("Because the facts of Homer's life -- when he was born or died, where he lived, who he was -- remain unknown and shall most likely never be known, many scholars have doubted the existence of a 'Homer' and point to his texts as the work of a collection of authors over a long period of time. This criticism stems from a disbelief that epics such as The Iliad and The Odyssey could have been formulated, maintained, and transmitted within an oral culture. However, new research on human memory and careful analysis of text reveals evidence that the textual style of each poem does emanate from one author.\n");

sec4.Paragraphs.Add(textI);

}

Section sec5 = pdf.Sections.Add();

sec5.IsNewPage = false;

Text t5 = new Text("section five");

sec5.Paragraphs.Add(t5);

pdf.Save("sections.pdf",SaveType.OpenInBrowser,Response);

In testing with different client computers, it appears that the Adobe Reader is possibly causing the page number error - Adobe Reader 6 displays the correct page numbers in Aspose HeaderFooter and in Table of Contents; in Adobe 7.07 Reader (which is what I have) there is the issue or problem with the page numbers not synching up. Can you please verify this? Thanks.

Thanks for considering Aspose.

I've tested your codes and the generated pdf document , which is attached, looks fine both with Adobe Reader 6.0 and Adobe Reader 7.0.

The result pdf contains three pages, so I don't really understand what you said, page x of 4. If the pdf document you generated is not as same as that I attached, please provide it to us for analysis.

hi, yes there seems to be some issue with my computer and my Aspose setup and my Aspose implementation - the test document you sent me, which I'll assume you have compiled on your computer, looks fine in my Adobe 7 Reader (as you said.) To be clear, the page numbers are correct in the file you posted in your previous post.

so now i'm really baffled as to what may be causing the problem - I currently have an Aspose evaluation copy, the DLL is assembly version 2.9.2.0, product version 2006.02.04 and I'm using it in a C# Visual Studio.NET 2003 project. We are in the process of purchasing Aspose.Pdf but of course we would like to understand this problem and its solution first before making a commitment to using Aspose for our Pdf documents.

Attached is a screen shot (JPG)of what I get when I compile the same test code on my computer.

If you have any insight on this problem, I would greatly appreciate it, as the problem is now beyond my capability as a developer to understand why the same code compiles and produces correct page numbers for you, while on my computer the same code produces the page number error. I am currently testing on another computer to see if the problem recurs.