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;
}