Welcome Guest Search | Active Topics | Sign In | Register

Determine if image fits on page Options
gaden
Posted: Tuesday, April 24, 2012 12:46:36 PM
Rank: Newbie
Groups: Member

Joined: 4/6/2012
Posts: 3
I am currently evaluating your product and have found it very good so far. I am using the PDF library and need to support the addition of images to a created PDF. Is there a way to determine if an image will fit on a page? If not, is there a way to shrink the image so that it does it?

Thanks in advance.
eo_support
Posted: Tuesday, April 24, 2012 1:00:02 PM
Rank: Administration
Groups: Administration

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

Whatever you want to do you will do it in your HTML page, not on the HTML to PDF converter. For example, you can use JavaScript to detect whether the image will fit on the page and scale it (by setting the img tag's width/height property) if necessary. The converter will run all your JavaScript code the same way as a browser does. To ensure your JavaScript code has enough time to run, you will want to increase HtmlToPdf.Options.MinLoadWaitTime if necessary; or use manual trigger as explained on this topic:

http://www.essentialobjects.com/doc/4/htmltopdf/eo_js.aspx

You may also want to take a look of the paging options:

http://www.essentialobjects.com/doc/4/htmltopdf/paging.aspx

This doesn't scale image for you, but if you put "page-break-inside:avoid" on your images, the converter will try not to break a single image into multiple pages.

Thanks!
gaden
Posted: Tuesday, April 24, 2012 1:14:54 PM
Rank: Newbie
Groups: Member

Joined: 4/6/2012
Posts: 3
eo_support wrote:
Hi,

Whatever you want to do you will do it in your HTML page, not on the HTML to PDF converter. For example, you can use JavaScript to detect whether the image will fit on the page and scale it (by setting the img tag's width/height property) if necessary. The converter will run all your JavaScript code the same way as a browser does. To ensure your JavaScript code has enough time to run, you will want to increase HtmlToPdf.Options.MinLoadWaitTime if necessary; or use manual trigger as explained on this topic:

http://www.essentialobjects.com/doc/4/htmltopdf/eo_js.aspx

You may also want to take a look of the paging options:

http://www.essentialobjects.com/doc/4/htmltopdf/paging.aspx

This doesn't scale image for you, but if you put "page-break-inside:avoid" on your images, the converter will try not to break a single image into multiple pages.

Thanks!


I'm not translating html to PDF but creating a PDF. Here is how I am currently adding an image to a PDF:

var pdfDoc = new PdfDocument();
var pageLayout = new AcmPageLayout(new AcmPadding(.5f, .5f, 0f, 0f)); // Set page margin
var renderer = new AcmRender(pdfDoc, pageLayout);
var gdiImage = System.Drawing.Image.FromStream(imageStream);
var imageContent = new AcmContent(new AcmImage(gdiImage));
renderer.Render(imageContent);
eo_support
Posted: Tuesday, April 24, 2012 1:30:55 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
I see. In that case you would need to calculate the layout and scale the image yourself. If your page only has images, then you can just check the height of your image to see if it exceeds the page height and scale it accordingly. If your page contains other contents as well, then you can render other contents without images first and then use the following method to get the total height of your contents without the image:

1. Place a blank marker content at the end of your contents. For example, a blank AcmText content;
2. Render your contents;
3. Call this method on your marker content:
http://www.essentialobjects.com/doc/4/eo.pdf.acm.acmcontent.createdestination.aspx
4. Check the returned PdfDestination object's FitRect.Y property;
5. Calculate the total content height based on FitRect.Y;

Step 5 is a bit tricky and it depends on your page size. FitRect.Y goes from the bottom of your page upwards and is in "user space unit", which is 1/72 inch. So for example, if your paper height is 10 inch, then a value of 720 (10 * 72) would point to the top of your page, 0 would point to the bottom of your page, and 360 would point to the middle of your page (note this does not count margins, you may have to adjust for margins).

Alternatively, you can render all of your "non-image" contents with the HTML to PDF converter and then use AcmRender to "fill in" the images. The benefit of using HTML to PDF converter is the HTML to PDF converter gives you height/size in a much more straight forward way. For example, to get the total content height, you just check HtmlToPdfResult.LastPosition, which is already in inch.

Hope this helps. Please feel free to let us know if you still have any questions.

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.