Welcome Guest Search | Active Topics | Sign In | Register

Convert a JPG to a PDF Options
Gabe
Posted: Wednesday, February 1, 2017 1:57:59 PM
Rank: Member
Groups: Member

Joined: 5/4/2016
Posts: 19
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.aspx

I 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!
eo_support
Posted: Wednesday, February 1, 2017 2:20:14 PM
Rank: Administration
Groups: Administration

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

The easiest way is to use HtmlToPdf.ConvertHtml to do that. The code would be something like this:

Code: C#
//These two options automatically scale the image into a single page
HtmlToPdf.Options.AutoFitX = HtmlToPdfAutoFitMode.ScaleToFit;
HtmlToPdf.Options.AutoFitY = HtmlToPdfAutoFitMode.ScaleToFit;

//Convert the img element
PdfDocument doc = new PdfDocument();
HtmlToPdf.ConvertHtml("<img src='your_image_file_name_here' />", doc);


Please let us know if this works for you.

Thanks!
Gabe
Posted: Thursday, February 2, 2017 3:12:32 PM
Rank: Member
Groups: Member

Joined: 5/4/2016
Posts: 19
Thank you for the suggestion, but the file is being uploaded and I don't want to save to disk so I can do a HTML conversion. I found the trick was to set a custom DPI resolution to image file (by wrapping it in a bitmap) so that the image would "AutoScale() to fit the page. Here is the code in case anyone needs it. Thanks again to "B at Diamond Drugs" for the image centering code. I made it a little more generic so it should work with any image size/dpi and any size PDF page...

public static void BitmapToPDF(Image bitmap, PdfDocument PDFDoc)
{
//takes an image and adds a single page with the image centered and scaled to fit the page.

//'Create a new page
var docPage = PDFDoc.Pages.Add();

//setting the DPI to custom so it sizes the image for the whole page
using (var img = new Bitmap(bitmap))
{
if (img.Width > img.Height)
{
float dpi = (float)img.Width / (docPage.Size.Width - 1); // the -1 is for margin
img.SetResolution(dpi, dpi);
}
else
{
float dpi = (float)img.Height / (docPage.Size.Height - 1); // the -1 is for margin
img.SetResolution(dpi, dpi);
}


//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 = (docPage.Size.Width * 72) / 2;
double middlePageY = (docPage.Size.Height * 72) / 2;

//Calculate the values for X and Y to move the image. I'm using 72 for my DPI.
double xPerInch = (img.Width / img.HorizontalResolution);
double yPerInch = (img.Height / img.VerticalResolution);
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);

}
}
eo_support
Posted: Thursday, February 2, 2017 3:15:20 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
I see. Thank you very much for sharing!


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.