We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Different Header on each page in Aspose.Words

Hi,


Please give me sample code to display different Header on each page of the pdf file which is generated by using Aspose.Words.

e.g. I have Case Number and Tox Number which is different on each page and that needs to be displayed on the header of each page.

Thanks!
Hi there,

Thanks for your inquiry. HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter or each HeaderFooterType in a Section.

In your case, we suggest you please insert the section break at each page of document and insert header for each section. Following code example shows how to insert section break continuous on each page of document and insert header for each section. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

//Insert section break at each page

LayoutCollector collector = new LayoutCollector(doc);

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);

int pageIndex = 1;

foreach (Paragraph para in paragraphs.ToArray())

{

if (collector.GetStartPageIndex(para) == pageIndex)

{

builder.MoveTo(para.AppendChild(new Run(doc)));

builder.InsertBreak(BreakType.SectionBreakContinuous);

pageIndex++;

}

}

pageIndex = 1;

//Create header for each section

foreach (Section section in doc.Sections)

{

HeaderFooter header = section.HeadersFooters[HeaderFooterType.HeaderPrimary];

if (header == null)

{

// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);

section.HeadersFooters.Add(header);

}

builder.MoveToSection(doc.Sections.IndexOf(section));

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

builder.Writeln("page : " + pageIndex);

pageIndex++;

}

doc.Save(MyDir + "Out.docx");

Thanks for the sample code but looks like it is not relevant in my scenario.


I have several merge field in a .doc template and one of the merge field may go beyond one page where I am unable to set the header.

Here is .doc template where «Exhibits» field content may go beyond one page where I need to show Non-ME Case Number«CaseNumber», Tox Number «ToxicologyNumber» on each page as header but unable to do so in this condition.

Non-ME Case Number«CaseNumber», Tox Number «ToxicologyNumber»

Case Information

Case Number

«CaseNumber2»

Tox Number

«ToxicologyNumber»

«Note»

Case Type

«CaseType»

Offense Date

«OffenseDate»


Agency Information

Agency Name

«AgencyName»

Service Number

«ServiceNumber»

Contact Name

«AgencyContact»

Contact Address

«AgencyAddress»

Contact Phone

«AgencyPhone»

«CasePerson»

«Exhibits»

Hi there,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for our reference:

  • Your input Word document
  • Your target Word document showing the desired behavior. You can use Microsoft Word to create your target Word document.
  • Please create a standalone console application (source code without compilation errors) that helps us to generate the output document.

As soon as you get these pieces of information ready, we will investigate how you are expecting your final document be generated like. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click 'Reply' button that will bring you to the 'reply page' and there at the bottom you can include any attachments with that post by clicking the 'Add/Update' button.

Hi,


I have attached below documents for this reference and would appreciate for any help.

TargetOutput.PDF - Expected output document; refer the second page header which is missing in this document and highlighted with hand written.

CurrentOutput.PDF - The output which we are getting with the current code.

Aspose_DiffPageHeader.zip.txt - Please remove the “.txt” extension for sample source code to generate CurrentOutput.PDF file. Also, I have removed the content of Aspose.Total.lic file which you need to update to see entire output.

Thanks in advance!

Please do let me know if you have any suggestion for the resolution.


Thanks in advance!

I am not sure if you have any recommendation on this but any help would be appreciated.


Thanks!
Hi there,

Please accept my apologies for late response. We are working over your query and will update you soon.
Hi,

Appreciate your response and hope you will suggest some solution for the requested problem.

Thanks!

Hi there,


Thanks for your patience. We have checked your code example and have noticed that you are performing mail merge and inserting some contents at the position of CasePerson and Exhibits mail merge fields. In your case, we suggest you following solution.

1) Move the header contents (Non-ME Case Number«CaseNumber», Tox Number«ToxicologyNumber») into the document’s header. Please check attached in.dotx.

2) Implement IFieldMergingCallback interface and in FieldMerging method move the cursor to the mail merge fields CasePerson and Exhibits and insert the desired contents.

3) Perform the mail merge operation using MailMerge.Execute (DataTable) method instead of moving the cursor to each mail merge field and write the contents.

Hope this helps you. Please let us know if you have any more queries.

Thanks for the reply.


The header content was initially (Non-ME Case Number«CaseNumber», Tox Number«ToxicologyNumber») into the document’s header but was giving trouble to display different Case Number/Tox Number and hence it moved to top section of the document such that we can handle the header pragmatically.

I will try to implement the suggested option but would appreciate if you can share some sample code for this.

Please remember, the problem is to display different header on each page based on certain condition.

Thanks again!

Hi there,


Thanks for your inquiry.
Dilip Singh:
The header content was initially (Non-ME Case Number«CaseNumber», Tox Number«ToxicologyNumber») into the document’s header but was giving trouble to display different Case Number/Tox Number and hence it moved to top section of the document such that we can handle the header pragmatically.
Could you please share some detail about the problem which you are facing when (Non-ME Case Number«CaseNumber», Tox Number«ToxicologyNumber») is in the header of document? We will then provide you more information on this along with code.
Dilip Singh:
Thanks for the reply.
Please remember, the problem is to display different header on each page based on certain condition.
As per my understanding, for each record of DataTable you want to insert some contents into document and insert page beak. If the contents for one record is more than one page, the CaseNumber and ToxicologyNumber should be same for a specific record one all pages. Please check attached out.docx and confirm if following text is correct on first two page. Following text is same on first two page.

Non-ME Case Number181374, Tox Number1600004

Thanks for the reply.


Yes, the attached out.docx is the expected output that we are looking. Could you please share the code to get that expected output.

Appreciated your response. Thanks again!

Hi there,


Thanks for sharing the detail. We have modified the template document and code. Please check modified template (Non-METransfer.docx) in Templates folder. We have attached the output document with this post for your kind reference.

Hope this helps you. Please let us know if you have any more queries.

Thanks a lot for all your help. I have found another way to handle my issue but your code works great and will be using your code for the resolution of this issue.


Thanks again and appreciate all your help!

Hi there,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.