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

Free Support Forum - aspose.com

Missing Sections When Saving to PDF from HTML

Hello. I am trying to apply formatting to two HTML documents, append one onto the other, and then save the result to PDF. However, the resulting PDF is missing the content from the second HTML document that gets appended.

Here is the logic I’m using:

public static void formattedConvert(String html1, String html2, String outFile) {

Document firstDoc = new Document(html1);
Document secondDoc = new Document(html2);

// Set margins for second section
Section secondDocSection = secondDoc.FirstSection;
secondDocSection.PageSetup.LeftMargin = ConvertUtil.MillimeterToPoint(25);
secondDocSection.PageSetup.TopMargin = ConvertUtil.MillimeterToPoint(25);
secondDocSection.PageSetup.RightMargin = ConvertUtil.MillimeterToPoint(15);
secondDocSection.PageSetup.BottomMargin = ConvertUtil.MillimeterToPoint(10);

// Don’t adopt formatting of previous section when appending to firstDoc

// Set margins for first section
Section firstSection = firstDoc.Sections[0];
firstSection.PageSetup.LeftMargin = ConvertUtil.MillimeterToPoint(25);
firstSection.PageSetup.RightMargin = ConvertUtil.MillimeterToPoint(20);
firstSection.PageSetup.TopMargin = ConvertUtil.MillimeterToPoint(20);
firstSection.PageSetup.BottomMargin = ConvertUtil.MillimeterToPoint(20);

// Add page numbers for first section
DocumentBuilder firstBuilder = new DocumentBuilder(firstDoc);
firstBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

// Append secondDoc to firstDoc
firstDoc.AppendDocument(secondDoc, ImportFormatMode.KeepSourceFormatting);

firstDoc.Save(outFile, SaveFormat.Pdf);

If I save as a DOC or DOCX it has the second section and looks as expected, but saving to PDF leaves it off. Attached is a zip archive containing the HTML files and an accompanying image. Any help with this issue would be very appreciated!


Hi Matt,

Thanks for your request. Please try using the following code to save your final document to PDF:


firstDoc.Save("out.pdf", SaveFormat.Pdf);

This should help.

Best regards,

That seems to work. Thank you!