Welcome Guest Search | Active Topics | Sign In | Register

HtmlToPdf + PdfCreator OutOfMemory Exception Options
Ed Boykin
Posted: Thursday, July 17, 2014 2:49:12 PM
Rank: Newbie
Groups: Member

Joined: 8/17/2012
Posts: 3
I'm converting multiple html files into a single pdf by calling ConvertToHtml multiple times and it's working beautifully. However, when I try to loop through the pages after the fact to add footer content, I run into an OutOfMemory exception. Can someone please point out what I'm doing wrong?



Code: C#
PdfDocument doc = new PdfDocument();
HtmlToPdf.Options.PageSize = new SizeF(PdfPageSizes.Letter.Width, PdfPageSizes.Letter.Height);
HtmlToPdf.Options.AutoFitX = HtmlToPdfAutoFitMode.None;
HtmlToPdf.Options.AutoFitY = HtmlToPdfAutoFitMode.None;

float pageWidth = HtmlToPdf.Options.PageSize.Width-(float)0.25;
float pageHeight = HtmlToPdf.Options.PageSize.Height - (float)1.75;
HtmlToPdf.Options.OutputArea = new RectangleF(1.0f, 1.0f, pageWidth, pageHeight);

HtmlToPdf.ConvertHtml(html, doc); //<--- called several times for different html

for (int i = 0; i < doc.Pages.Count; i++)
{
     AcmRender render = new AcmRender(doc.Pages[i], 0, new AcmPageLayout(new AcmPadding(0)));
     AcmText text = new AcmText(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count));
     text.Style.ForegroundColor = Color.LightGray;

     AcmBlock block = new AcmBlock(text);
     block.Style.Top = 11.15f;
     block.Style.Left = 7.0f;

     AcmBlock footer = new AcmBlock(block);
     render.Render(footer);
}

MemoryStream stream = new MemoryStream();
doc.Save(stream);
return stream.ToArray();




While debugging, I have noticed that every time render.Render() is called another page gets added to my doc.Pages collection. It just keeps expanding until I run out of memory.

Thanks.
eo_support
Posted: Friday, July 18, 2014 3:42:09 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

You must make sure the footer renders within the page. Your page size is Letter size, the height of letter size is 11 inch. So you can not put your text at 11.15f. That will cause automatic overflow to the next page, and when the next page does not exist, it will automatically add a new page. Thus causing the problem for you.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.