Free Support Forum - aspose.com

Issue with headers while saving word as PDF

Hi,


I have two documents and I am merging them. I need header on one and the other one I do not want the header. After merging I want to save it as a PDF. On saving I can see extra space in the resultant pdf document. Below is the code I have used. I have also attached the documents.

Document dstDoc = new Document(“C:/documents/testdata/TestDestination.docx”);
Document srcDoc = new Document(“C:/documents/testdata/meetingroster.docx”);
DocumentBuilder docBuilder = new DocumentBuilder(dstDoc);
Section currentSection = docBuilder.getCurrentSection();
PageSetup pageSetup = currentSection.getPageSetup();
pageSetup.setHeaderDistance(50);
docBuilder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
docBuilder.getCellFormat().clearFormatting();
docBuilder.getFont().setSize(10);
docBuilder.getFont().setName(“Arial”);
docBuilder.insertCell();
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.NONE);
docBuilder.getCellFormat().getBorders().setLineWidth(0);
// Set first cell to 1/3 of the page width.
docBuilder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 / 3));
docBuilder.write(“Dummy text 1”);
docBuilder.insertCell();
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.NONE);
docBuilder.getCellFormat().getBorders().setLineWidth(0);
docBuilder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 / 3));

docBuilder.insertField(“PAGE”, “”);
docBuilder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

docBuilder.insertCell();
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.NONE);
docBuilder.getCellFormat().getBorders().setLineWidth(0);
docBuilder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(100 / 3));

docBuilder.write(“Dummy text 2”);
docBuilder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
docBuilder.endRow();
// Remove the headers and footers from each of the sections in the source document.
for (Section section : srcDoc.getSections())
{
section.clearHeadersFooters();
}

// Even after the headers and footers are cleared from the source document, the “LinkToPrevious” setting
// for HeadersFooters can still be set. This will cause the headers and footers to continue from the destination
// document. This should set to false to avoid this behaviour.
srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(false);

dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dstDoc.save(“C:/documents/testdata/testdata.docx”);
dstDoc.save(“C:/documents/testdata/testdata.pdf”, SaveFormat.PDF);

Regards,

Hi Rajeev,


Thanks for your inquiry. After an initial test with Aspose.Words for Java 15.7.0, I was unable to reproduce this issue on my side (please see attached 15.7.0.pdf). I would suggest you please upgrade to the latest version of Aspose.Words. You can download it from the following link:
http://www.aspose.com/community/files/72/java-components/aspose.words-for-java/default.aspx

I hope, this helps.

Best regards,