Convert HTML to PDF

Since old post is in ready only this issue is this

using the attached output.pdf from (Tahir Manzoor) see printing_output.pdf. all formatting is lost as well as it still prints letter.

can you check from your side what happens when printing
output.pdf then select a printer? see my printing_output.png
output.pdf (37.6 KB)
printing_output.png (93.3 KB)


Thanks for your inquiry. Generally, Aspose.Words mimics the behavior of MS Word. Please open your HTML file with MS Word 2016, set the paper size and margins and then Save As PDF. Now open this PDF and try to Print it, you will notice that the paper size and margins settings are not preserved even when printing MS Word 2016 generated PDF document. So, this seems to be an expected behavior. If we can help you with anything else, please feel free to ask.

EDIT: We have also noticed that the ‘print dialog boxes’ of Foxit and Adobe viewers present an option “Choose paper source by PDF page size” that you can tick mark to meet your requirement. Hope, this helps.

Best regards,

Thanks awais, just to confirm, I did try to directly convert HTML to PDF without converting to word document then save as PDF , which it did not work as well

Aspose.Pdf.Document pdf;

            Aspose.Pdf.HtmlLoadOptions htmlLoadOptions = new Aspose.Pdf.HtmlLoadOptions();
            MemoryStream outputstream = new MemoryStream();
            htmlLoadOptions.InputEncoding = "UTF-8";
            htmlLoadOptions.PageInfo.Margin.Left = 0;
            htmlLoadOptions.PageInfo.Margin.Right = 0;
            htmlLoadOptions.PageInfo.Margin.Top = 0;
            htmlLoadOptions.PageInfo.Margin.Bottom = 0;
            htmlLoadOptions.PageInfo.IsLandscape = true;
            htmlLoadOptions.PageInfo.DefaultTextState.FontSize= 7;

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(pageSource)))
                pdf = new Aspose.Pdf.Document(stream, htmlLoadOptions);
                // Search text fragments 
                   pdf.Pages[1].SetPageSize(612, 1008);
                Aspose.Pdf.Text.TextFragmentAbsorber absorber = new Aspose.Pdf.Text.TextFragmentAbsorber();
                //accept the absorber for all the pages
                // traverse through all the TextFragments
                foreach (Aspose.Pdf.Text.TextFragment textFragment in absorber.TextFragments)
                    textFragment.TextState.FontSize = 7;


            byte[] bytes = outputstream.GetBuffer();
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "List.pdf"));
            HttpContext.Current.Response.ContentType = "application/pdf";
            //  render the htmlwriter into the response                    

so you are saying there is no way using Aspose to preserve margins and page size once we generate then print PDF?

Let me first clarify you that you can convert an HTML to PDF directly with both Aspose.Words and Aspose.Pdf APIs. You were facing the problem of slow text searching with Aspose.Pdf for .NET API, which had already been logged under the ticket ID PDFNET-42875 (status: open), so we suggested you try the latest version of Aspose.Words for .NET API (the text searching is good with Aspose.Words API).

Now the problem is when you open the output PDF in the Acrobat viewer, it does not preserve the actual paper size and margin settings. In the perspective of Aspose.Words API, it is not a bug because Aspose.Words API mimics the behavior of Microsoft Office Word. In the perspective of the Aspose.Pdf API, we have logged an investigation to check if we can map the actual page size and margins in the PDF viewer (PDFNET-42865). In reference to the page margins, it is important that your printer should support border-less printing.

As a workaround, please manually apply print settings in the PDF viewer as narrated in the above two quotes. You can also specify the page and printer settings of a PDF while printing through the Aspose.Pdf for .NET API. Please refer to this help topic: Printing PDF File to Default Printer using Printer and Page Settings

Please let us know in case of any confusion or questions.