Hi.
I have a use case where a “master” template should be used to generate a document. The header and footer of this template should be used in the generated document.
I am able to do that, however the format is different from the original.
Aspose version: 16.8.0
This is the code I’m using:
@Test
public void generateForumExample() throws Exception {
final javax.xml.parsers.DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final org.w3c.dom.Document data = db.parse(dataDir + “example_data.xml”);
Locale.setDefault(new Locale(“en”, “US”));
final Document srcDoc = new Document(dataDir + “example_template.docx”);
final Document dstDoc = new Document(dataDir + “example_doc_header_footer.docx”);
srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
srcDoc.getFirstSection().getPageSetup().setPaperSize(PaperSize.LETTER);
srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(true);
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dstDoc.getMailMerge().setUseNonMergeFields(true);
dstDoc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS);
dstDoc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(data));
dstDoc.save(dataDir + “example_result.docx”);
}
Can you give me a hint about what I am not doing correctly?
Hi Tahir,
Please see the attached files. Let me know if more information is necessary.
Hi,
Hi,
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>srcDoc.FirstSection.PageSetup.LeftMargin = dstDoc.FirstSection.PageSetup.LeftMargin; |
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>DocumentBuilder builder = new DocumentBuilder(dstDoc); |
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>dstDoc.FirstSection.Body.AppendParagraph(""); |
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>builder.MoveToDocumentEnd(); |
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>builder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting); |