ASPOSE PDF (From HTML) - Page Layout and Table Sizing Differences

We recently moved from version 11.8 to 23.1.1.
We allow our users to generate document templates for letters etc. (with field merges) from HTML fragments that we pull in and convert to PDF. The code involved hasn’t changed between the 2 versions.
We’ve noticed some anomalies between page layouts between the 2 versions of the DLL that will cause us problems with letter production (address window placing). We can’t change templates as we don’t maintain them, so can you advise how we can overcome these issues?
The issues we are seeing are -

  • PDF Page Sizes
  • Page Margins
  • Table column widths

OLD Margins.PDF (86.8 KB)
NEW Margins.PDF (189.8 KB)
NEW Table.PDF (338.9 KB)
OLD Table.PDF (130.5 KB)

I’ve uploaded documents showing the differences in table column widths (OLD and NEW pdf) as well as page top and bottom margins (OLD Margins.pdf and NEW Margins.pdf)

testhtml.zip (7.3 KB)
This ZIP contains the fragments for each example.

Code snippet follows -

private static void ExportHtmlToPdfFile(string htmlEditorContent, string tempFile, out int pageCount, string previewFolder = "")
{
    pageCount = 0;
    SetAsposeLicence();
    using (Aspose.Pdf.Document pdf = new Aspose.Pdf.Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlEditorContent)), new HtmlLoadOptions().SetHtmlLoadOptions()))
    {
        SetPDFQuality(pdf);
        pageCount = pdf.Pages.Count;
        using (MemoryStream outStream = new MemoryStream())
        {
            pdf.FinalPageSetup().Save(outStream);
            byte[] docBytes = outStream.ToArray();
            using (IFileManager fileMan = FileCommon.GetFileInstance(tempFile))
            {
                fileMan.SaveFile(tempFile, "", docBytes.ToArray());
            }
            pdf.FreeMemory();
        }
    }
}

private static void SetPDFQuality(Aspose.Pdf.Document pdf)
{
    //var optimizeOptions = new Aspose.Pdf.Document.OptimizationOptions();
    var optimizeOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
    optimizeOptions.ImageCompressionOptions.CompressImages = true;
    optimizeOptions.ImageCompressionOptions.ImageQuality = 75;
    optimizeOptions.RemoveUnusedObjects = true;
    optimizeOptions.RemoveUnusedStreams = true;
    optimizeOptions.LinkDuplcateStreams = true;
    pdf.OptimizeResources(optimizeOptions);
}

public static Aspose.Pdf.MarginInfo Margins()
{

    double Top = 0;
    if (!double.TryParse(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["TopMargin"]), out Top))
    {
        Top = 18;
    }

    double Bottom = 0;
    if (!double.TryParse(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["BottomMargin"]), out Bottom))
    {
        Bottom = 18;
    }

    double Left = 0;
    if (!double.TryParse(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["LeftMargin"]), out Left))
    {
        Left = 18;
    }

    double Right = 0;
    if (!double.TryParse(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["RightMargin"]), out Right))
    {
        Right = 18;
    }

    return new Aspose.Pdf.MarginInfo(Left, Bottom, Right, Top);
}

public static HtmlLoadOptions SetHtmlLoadOptions(this HtmlLoadOptions htmlLoadOptions)
{
    htmlLoadOptions.PageInfo.Height =Aspose.Pdf.PageSize.A4.Height;
    htmlLoadOptions.PageInfo.Width = Aspose.Pdf.PageSize.A4.Width;
    htmlLoadOptions.PageInfo.Margin = Margins();
    return htmlLoadOptions;
}

public static Document FinalPageSetup(this Document document)
{

    document.PageInfo.Margin = Margins();
    foreach (Page page in document.Pages)
    {
        if (page.PageInfo.Width > page.PageInfo.Height)
        {
            page.SetPageSize(Width(true), Height(true));                   

        }
        else {
            page.SetPageSize(Width(), Height());  // Width = 597.6, Height = 842.4 from methos returns!

        }
        page.PageInfo.Margin = Margins();
    }


    return document;
}

@pjjonah66,

Can you give me the values of all the variables coming from System.Configuration.ConfigurationManager.AppSettings??

BEcause after checking the old document generated I clearly see it doesnt have symetric margins.

While the new document does have the symetric margins:

Ig anything maybe there was a bug in the old version but the new one is seems to be respecting that.

Then to be sure, I modify the code to have double margin tops vs left:

And I got the top margin increased:

It seems that the old version was not working as intended vs the new one.

Keep in mind the pixels will depend on the quality of the document.

Hi Carlos.

If the old DLL was the problem then could you suggest what kind of remediation we would need to make in the new one to get us back to the same output? Unfortunately, we’ve inherited a lot of documents that were formatted to work with the older DLL and we can’t change them.

Also - any suggestions on making the table layout behave the same as the older DLL would be gratefully received for the same reasons.

These are the key values we’re using here…

<add key="Width" value="597.6" />
<add key="Height" value="842.4" />
<add key="TopMargin" value="18" />
<add key="BottomMargin" value="18" />
<add key="LeftMargin" value="18" />
<add key="RightMargin" value="18" />

Please let me know if you need any more info!

@pjjonah66,

I edited my previous post with more information. It basically seems that the old document where not rendered correctly.

Hello Carlos.

So if we want to try to get margin results closer to the older DLL (as we can’t change all the templates indivudually), what course of action would you recommend? For example, if we tried to offset the top margin with a correction every time, would this be workable or is there a better way of doing this?
ALSO - can you look at the differences we’re seeing with table widths in the OLD Table and NEW Table to see how we can revert to the behaviour that the old DLL was displaying? I don’t think the old and new DLLs are wrong with the way they turn HTML tables into PDF ones, but we need to aim for consistent treatment with the new DLL.

@pjjonah66,

Let me review this and come back with a response. I am going to to try to give you some guidance how to move the itmes but the pixel perfect positioning will be left for you.

So in order to not modify old behavior. I think in order to get the offset at the top the best solution would be to keep your old code and add the offset. Something like this.

int topOffset = 16; // Just some value, you need to calculate the perfect amount here.
double Top = 0;
if (!double.TryParse(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["TopMargin"]), out Top))
{
    Top = 18;
}
Top += topOffset;

Regarding the table itself, even though I am not part of the development team, I know there has been some bug regarding positioning coming from HTML conversion that was fixed and others that are been worked on at the moment. So this may affect the final document you are seeing.

The problem is that we cannot modify the structure of the table already rendered. Since PDF documents are Fixed type and not Flow type. I apologize for the inconvenience.

The real solution would be to modify the templates but I understand you do not have access to them so I do not see a possible solution.