Hello everybody,
we currently have the problem that saving as PDF sets headers incorrect in some constellations. If the same document is saved as a Word file, the header is set correctly. Here is a minimal example of what we use for testing (please adjust the paths in the code so that they can be found):
public class AsposeTest {
public static void main(String[] args) throws Exception {
Document fails = new Document(new FileInputStream(new File(AsposeTest.class.getResource("/checkliste.doc").getPath())));
Document template = new Document(new FileInputStream(new File(AsposeTest.class.getResource("/orgavision_schablone_original.docx").getPath())));
replaceHeadersFooters(fails, template);
File pdfResult = new File("./failed.pdf");
File docxResult = new File("./worked.docx");
SaveOptions docxSaveOptions = new OoxmlSaveOptions();
docxSaveOptions.setSaveFormat(SaveFormat.DOCX);
fails.save(new FileOutputStream(pdfResult), new PdfSaveOptions());
fails.save(new FileOutputStream(docxResult), docxSaveOptions);
}
private static void replaceHeadersFooters(Document document, Document template) {
final NodeImporter importer = new NodeImporter(template, document, ImportFormatMode.KEEP_SOURCE_FORMATTING);
final HeaderFooterCollection templateCol = template.getFirstSection().getHeadersFooters();
boolean firstSection = true;
for (final Section section : document.getSections()) {
final HeaderFooterCollection sectionCol = section.getHeadersFooters();
for (int i = 0; i < templateCol.getCount(); i++) {
final HeaderFooter templateHF = templateCol.get(i);
final HeaderFooter sectionHF = sectionCol.getByHeaderFooterType(templateHF.getHeaderFooterType());
if (sectionHF != null) {
sectionHF.remove();
}
if (firstSection) {
sectionCol.add((importer.importNode(templateHF, true)));
}
}
firstSection = false;
}
}
}
files.zip (42.9 KB)
It should look like worked.docx. failed.pdf is the wrong result.
I attached the template and the document.
We use the latest version of aspose-words (19.1) and aspose-pdf (18.11).
Can you tell us where the problem is?
If you need more information, just let me know.
Thank you and best regards