HTML to PDF conversion issue with table position using C#

HTML to PDF conversion using Aspose.Words adding extra space. The font is slightly larger than that in HTML file.
attaching html and converted pdf files.
CRReport.pdf (5.7 KB)
CRReport.zip (8.1 KB)

@nbhele

In your case, we suggest you please set the table’s width as shown below to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "CRReport.html");
foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    table.PreferredWidth = PreferredWidth.FromPercent(100);
}
doc.Save(MyDir + "20.2.pdf");

@tahir.manzoor
Thanks for quick reply!
I tested above fix and this didn’t work as well.
attaching output pdf file here 20.2.pdf (77.6 KB)

@nbhele

Please note that Aspose.Words mimics the behavior of MS Word. If you opens the HTML in MS Word and convert it to PDF, you will get the same output.

Please get the temporary license and apply it before loading the document into Aspose.Words’ DOM. Please share the screenshot of problematic section of output document and your expected output PDF. We will then provide you more information about your query.

@tahir.manzoor I tried opening the HTML in MS Word and I can see this extra space there as well.
Is there any workaround in Aspose.Words to get rid of it.

@nbhele

Please use the following code example to remove the extra space (set page margins). Hope this helps you.

If you still face problem, please ZIP and attach your expected output documents here for our reference. We will then provide you more information on it.

Document doc = new Document(MyDir + "CRReport.html");
                
doc.FirstSection.PageSetup.LeftMargin = 0;
doc.FirstSection.PageSetup.RightMargin = 0;
doc.FirstSection.PageSetup.TopMargin = 0;
doc.FirstSection.PageSetup.BottomMargin = 0;

foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    table.PreferredWidth = PreferredWidth.FromPercent(100);
}
doc.UpdateTableLayout();
doc.Save(MyDir + "20.2.pdf");

I can still see the issue. extra space is in table cells and I think this is not related to margins of page.
I am attaching the expected pdf document and html here - ExpectedDoc.zip (87.4 KB)

@nbhele

The extra space in the table’s cell is due to row height. You can use RowFormat.HeightRule to set height rule. Please check the following modified code example. Hope this helps you.

Document doc = new Document(MyDir + "CRReport.html");

doc.FirstSection.PageSetup.LeftMargin = 0;
doc.FirstSection.PageSetup.RightMargin = 0;
doc.FirstSection.PageSetup.TopMargin = 0;
doc.FirstSection.PageSetup.BottomMargin = 0;

foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    table.PreferredWidth = PreferredWidth.FromPercent(100);
    foreach  (Row row in table.Rows)
    {
        row.RowFormat.HeightRule = HeightRule.Auto;
    }
}

foreach (Paragraph paragraph in doc.GetChildNodes(NodeType.Paragraph, true))
{
    paragraph.ParagraphFormat.SpaceAfter = 0;
    paragraph.ParagraphFormat.SpaceBefore = 0;
}
doc.UpdateTableLayout();
doc.Save(MyDir + "20.2.pdf");