We have the need to convert a user uploaded JPG to a single page PDF document. Reading the documentation and searching the forum, I haven't found a solution that will size the image correctly to fit on the page. The file being uploaded is from external users, so it could be any resolution/DPI. We just want to shrink it to fit on a page.
Referring to the following post...
https://www.essentialobjects.com/forum/postst8470_Simple-pdf-with-one-image-on-it--Need-to-center-image.aspxI have taken this code and cannot get the image to shrink to fit the page. Instead the image appears to large and is cut off the page. It appears the "content.AutoScale()" is not working or I don't understand it's proper usage. Here is the code I'm using...
if (PostedFile.ContentType == "image/jpeg")
{
var img = System.Drawing.Image.FromStream(PostedFile.InputStream);
//'Create a new page
var adddoc = new EO.Pdf.PdfDocument();
var docPage = adddoc.Pages.Add();
//Create a new PdfImage object
var pdfImage = new EO.Pdf.Drawing.PdfImage(img);
//Create a new PdfImageContent object
var content = new EO.Pdf.Contents.PdfImageContent(pdfImage);
//The unit for X, Y is 1/72 inch.
double middlePageX = (8.5 * 72) / 2;
double middlePageY = (11 * 72) / 2;
//Calculate the values for X and Y to move the image. I'm using 96 for my DPI.
double xPerInch = (img.Width / 96);
double yPerInch = (img.Height / 96);
double x = middlePageX - ((xPerInch * 72) / 2);
double y = middlePageY - ((yPerInch * 72) / 2);
//Move the image
content.GfxMatrix.Translate(Convert.ToSingle(x), Convert.ToSingle(y));
//Scale the image
content.AutoScale();
//Place the image on the page
docPage.Contents.Add(content);
outdoc = EO.Pdf.PdfDocument.Merge(outdoc, adddoc);
}
Can you assist on what I may be missing to properly size the image? Also, is the above a good approach or should I be using the ACM functions? I have an input JPG and the resulting output PDF, although I don't see a way to attach them to this post, so let me know if you want me to email them. Thanks for your help!