I am using iText 7 to convert an html document to pdf. It works pretty well but images that are larger than the pdf page go outside of the page and that part cannot be seen when opening up the pdf document. I have used auto scale and that fixes the problem but the image loses some information when auto scaling. I would like to create a page for the image that is as big as the image. So whenever an image is processed then it would go on its own page that is exactly as big as the image is. Here is the code I use to create the pdf from html:

using (MemoryStream pdfBodyStream = new MemoryStream()) using (PdfWriter writer = new PdfWriter(pdfBodyStream)) using (PdfDocument pdfDocumentBody = new PdfDocument(writer)) { pdfDocumentBody.AddNewPage(); var properties = new ConverterProperties(); properties.SetTagWorkerFactory(new CustomTagWorkerFactory(Dal, email.getItemAttachments.ToList())); HtmlConverter.ConvertToPdf(email.Body, pdfDocumentBody, properties); pdfDocumentBody.Close(); return pdfBodyStream.ToArray(); } public override ITagWorker GetCustomTagWorker(IElementNode tag, ProcessorContext context) { if ("img".Equals(tag.Name(), StringComparison.OrdinalIgnoreCase)) { return new CustomImgWorker(tag, context, Dal, Attachments); } return null; }

In the CustomImgWorker class I use the overridden method ProcessEnd to fetch the images for the html from a different place and create an image using the ImageDataFactory from iText. Then the overridden method GetElementResult returns that image object. Now I am not sure how I would achieve what I am after since I have not found anything where I have control over how the pages are added to the document in the HtmlConverter.ConvertToPdf method.

Does someone know how I would go about creating a new page for each of the img tags in the html that are as big as the image it self?

Sigmundur's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.