Q: I’m experiencing some issues when adding an image to a pdf page. I
have a PDF document of 91 x 61 mm. I add an image to this which is 20mm
(236px with a dpi of 300). When I step trough my code I see that those
values are nicely used but in the resulting pdf the image is too big.
I have the following code:
pdftron.PDFNet.Initialize();
using (PDFDoc doc = new PDFDoc(pdffile))
{
using (ElementBuilder bld = new ElementBuilder
())
{
using (ElementWriter writer = new ElementWriter
())
{
Page page = doc.GetPage(pageIndex);
writer.Begin(page);
Matrix2D mtx = new Matrix2D(bmp.Width,
0,0,bmp.Height, xLocation, yLocation);
Image img = Image.Create(doc, bmp);
Element element = bld.CreateImage(img, mtx);
writer.WritePlacedElement(element);
writer.End();
doc.Save(pdffile,
SDFDoc.SaveOptions.e_linearized);
}
}
}
Am i doing something wrong in the matrix creation? Or is there
something else you can think of?
-------
A: The dimensions of the image added using your code snippet will be
[bmp.Width, bmp.Height] points and will appear much larger than
expected. One PDF point is 1/72 of an inch. To address this you need
to take into account image resolution (e.g. GetHorizontalResolution()
from .NET framework) when placing the page on the page:
double img_width = (bmp.Width * 72) / bmp.GetHorizontalResolution
();
double img_height = (bmp.Height * 72) / bmp.GetVerticalResolution
();
Matrix2D mtx = new Matrix2D (img_width, 0, 0, img_height,
xLocation, yLocation);
If the image does not have accurate resolution you could also find
your own scaling factor (e.g. a scaling factor required to fit the
image within a given box on the page etc).