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

Free Support Forum - aspose.com

Inserting Footer while generating document using insertHTML

Hi,


I am generating a word document by using stitching multiple HTML documents at various levels. I want to insert Footer on each page. I have referred to this guide for inserting Footers :
http://www.aspose.com/docs/display/wordsjava/Working+with+Headers+and+Footers

However, what I deduced from trying this out is, it inserts a footer only when there is a page break. The issue I am facing is, as I am generating the document from HTML, I do not know when the next page break will happen (depending on the contents of HTML file). So is it possible to insert a generic footer on all pages when document is generated from builder.insertHTML method ?

Please point me to any documentation that can help me in this regards.

Thanks,
Kunal

PS: Let me know if you want me to attach sample html files, but you can easily create 2 or 3 html files with large contents to see the problem I am facing.

Hi Kunal,


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

  • Your input Word document
  • HTML files/strings
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word.
  • Please create a standalone Java application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. 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.

Best regards,

Hi Hafeez,


Sorry for the delay in reply. I am attaching a zip file which contains a sample method that I used to generate the word document along with 3 different HTML files that I used for my word document generation as well as Actual Document it is generating and What is the desired document it should generate.

You may use any HTML file you like, just ensure that its content spans across pages for replicate the issue I am facing.

Please let me know if you need anything else from my side.

Thanks,
Kunal

Hi Kunal,


Thanks for your inquiry. You can fix the output of “Generated Word Doc with Footers.docx” document by using the following code (see attached output):
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document doc = <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>new <span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document(<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>“D:<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>temp<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>Generated Word Doc with Footers.docx”<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>);
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>DocumentBuilder builder = new DocumentBuilder(doc);

for(Section sec : doc.getSections()){
sec.clearHeadersFooters();
}

insertFooterForDocument(builder);

doc.save(“D:\temp\awjava-17.2.0.docx”);


<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>private static void insertFooterForDocument(DocumentBuilder builder){
Section currentSection = builder.getDocument().getFirstSection();
PageSetup pageSetup = currentSection.getPageSetup();

// Specify if we want headers/footers of the first page to be different from other pages.
// You can also use PageSetup.OddAndEvenPagesHeaderFooter property to specify
// different headers/footers for odd and even pages.
pageSetup.setDifferentFirstPageHeaderFooter(true);

// — Create footer for pages other than first. —
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
insertHFContent(builder);

// — Create footer for first page. —
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_FIRST);
insertHFContent(builder);

builder.moveToDocumentEnd();
}

private static void insertHFContent(DocumentBuilder builder){
// We use table with two cells to make one part of the text on the line (with page numbering)
// to be aligned left, and the other part of the text (with copyright) to be aligned right.
builder.startTable();

// Clear table borders
builder.getCellFormat().clearFormatting();

builder.insertCell();
// Set first cell to 1/3 of the page width.
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 / 3));

// Insert page numbering text here.
// It uses PAGE and NUMPAGES fields to auto calculate current page number and total number of pages.
builder.write("Page “);
builder.insertField(“PAGE”, “”);
builder.write(” of ");
builder.insertField(“NUMPAGES”, “”);

// Align this text to the left.
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);

builder.insertCell();
// Set the second cell to 2/3 of the page width.
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 * 2 / 3));

builder.write(“ABC Classified”);

// Align this text to the right.
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);

builder.endRow();
builder.endTable();
}

Hope, this helps.

Best regards,

HI Hafeez,


Thanks for your reply. However, I think I have not explained my problem clearly.

I am generating a word document in memory from different HTML files from the database. I do not have a physical document. I tried your code on my in memory document and it only inserts page number on the last page.

My input for creating word document are HTML files that I have attached to the thread. I traverse through them, keep adding to Document builder using document.insertHTML method and at last I save it as an Word document. I want to add the footer to each page this in memory document.

How can I achieve that ?

Thanks,
Kunal

Hi Kunal,


Thanks for your inquiry. Please take the following two methods mentioned in my previous post:

  • insertFooterForDocument
  • insertHFContent

Now inside your “CreateWordDocument” method, please add the following lines at the end:
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”> <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(128, 128, 128); font-style: italic;”>// The newly inserted table of contents will be initially empty.
<pre style=“font-family: “Courier New”; font-size: 9pt;”> // It needs to be populated by updating the fields in the document.
field.update();

builder.moveToDocumentStart();

for(Section sec : doc.getSections()){
sec.clearHeadersFooters();
}

insertFooterForDocument(builder); // Use the method that I shared in my previous post

//Save it to a byte stream
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, SaveFormat.
DOCX);

//Code To save the dstStream inside pvim_publish Subject.
InputStream decodedInput=new ByteArrayInputStream(dstStream.toByteArray());
saveWordDocument(strTitle, decodedInput, id, strTenant, dstStream.size(), tracker);
}

Hope, this helps.

Best regards,

Hi Awais,


The solution you provided worked. It is behaving as I wanted.
Thank you so much for your help.

You may consider this thread closed.

Thanks,
Kunal