Free Support Forum - aspose.com

Convert HTML document to PDF- does not maintain format

Hi. I need to convert so much HTML (or Word) documents to PDF but your component does not maintain the right format.

The HTML document is in attachment.

This is my code to convert it:
private byte[] GeneratePdf(string html)
{
Pdf pdf = new Pdf();
byte[] pdfBytes;
using (StringReader reader = new StringReader(richText))
{
MemoryStream stream = new MemoryStream();
MarginInfo margin = new MarginInfo();
margin.Top = 2;
margin.Left = 2;
margin.Right = 2;
margin.Bottom = 2;
pdf.PageSetup.Margin = margin;
pdf.BindHTML(reader);
pdf.Save(stream);
pdfBytes;= stream.GetBuffer();
}
return pdfBytes;
}

Thanks for suggestions!!
Matteo.

Hi,

Please change your code as follows:

private byte[] GeneratePdf(string html)
{
Pdf pdf = new Pdf();
byte[] pdfBytes;
using (StringReader reader = new StringReader(richText))
{
MemoryStream stream = new MemoryStream();
pdf.BindHTML(reader);

foreach (Aspose.Pdf.Section sec in pdf.Sections)

{

sec.PageInfo.Margin.Top = 144;

sec.PageInfo.Margin.Left = 144;

sec.PageInfo.Margin.Right = 144;

sec.PageInfo.Margin.Bottom = 144;

}


pdf.Save(stream);
pdfBytes;= stream.GetBuffer();
}
return pdfBytes;
}

The unit for the margins is point. There are 72 in one inch.

Thanks.