Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Jun 28, 2018 4:10 pm

Hello,

My apology if I have posted the same question since I could not locate the answer.
I have a jpeg file of label page (Avery #5163 - 10 of 2 by 4 labels). When converting to Pdf, the size has changed and the labels shifted around. I am not sure at the size calculation. I just want to keep the original jpg size. Am I doing something wrong here?

Please advise,

Thanks much,

An

Here is my code:

using (PdfDocument pdfDoc = new PdfDocument())
{
PdfSection section = pdfDoc.Sections.Add();
PdfPageBase page = pdfDoc.Pages.Add();
PdfImage pdfImage = PdfImage.FromFile(@"C:\BarcodeImages\20180628090713137.jpg");
float widthFitRate = pdfImage.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
float heightFitRate = pdfImage.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
float fitRate = Math.Max(widthFitRate, heightFitRate);
float fitWidth = pdfImage.PhysicalDimension.Width / fitRate;
float fitHeight = pdfImage.PhysicalDimension.Height / fitRate;
page.Canvas.DrawImage(pdfImage, 5, 5, fitWidth, fitHeight);
pdfFileName = $"~/PDF_FOLDER/{DateTime.Now.ToString("yyyyMMddHHmmss")}.pdf";
pdfDoc.SaveToFile(System.Web.HttpContext.Current.Server.MapPath(pdfFileName));
}

anguyenalb
 
Posts: 8
Joined: Wed Feb 18, 2015 4:39 pm

Fri Jun 29, 2018 3:43 am

Dear An,

Thanks for your inquiry.
Your code is used for fitting the image to a page, if you want to keep original size, there is no need to adjust the image size.
Code: Select all
PdfImage pdfimage = PdfImage.FromFile(@"C:\BarcodeImages\20180628090713137.jpg");
page.Canvas.DrawImage(pdfImage, 5, 5);
pdfFileName = $"~/PDF_FOLDER/{DateTime.Now.ToString("yyyyMMddHHmmss")}.pdf";
pdfDoc.SaveToFile(System.Web.HttpContext.Current.Server.MapPath(pdfFileName));

If you still have issues, please share us your input image and result you got for further investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Jul 02, 2018 8:03 am

Dear An,

Has your issue been solved ? Your feedback would be greatly appreciated.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Jul 03, 2018 4:47 pm

Dear Betsy,

Your quick response is just great and thank you so much. It was really helpful, I was enable to get the original size of as is from Avery #5163 label. However, the left margin is still double the original of jpeg. Is there anyway to reduce the left margin more.
I currently have the below code:

page.Canvas.DrawImage(pdfImage, 0, 20);

Sincerely,

An

anguyenalb
 
Posts: 8
Joined: Wed Feb 18, 2015 4:39 pm

Wed Jul 04, 2018 2:31 am

Dear An,

Thanks for your inquiry.
Please try to set the margin as 0, then draw the image with the position(0,20).
Code: Select all
doc.PageSettings.SetMargins(0);
 ......
page.Canvas.DrawImage(pdfImage, 0, 20);

If you still have issues, please share your input files(PDF and image), the code you were using for further investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Jul 04, 2018 3:19 am

Thanks much Betsy, I will try again on Monday after the holiday. Have a great week!

anguyenalb
 
Posts: 8
Joined: Wed Feb 18, 2015 4:39 pm

Wed Jul 04, 2018 3:54 am

Dear An,

Looking forward to your feedback after testing.
Wish you a happy holiday!

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Jul 09, 2018 3:48 pm

Dear Betsy,

I have tried with setting margin to 0 as suggested but the left margin is no change. I have attached the original Avery Label #5163 and pdf converted which has a large left margin increased.
Here is my code:

string pdfFileName = string.Empty;
using (PdfDocument pdfDoc = new PdfDocument())
{
PdfSection section = pdfDoc.Sections.Add();
PdfPageBase page = pdfDoc.Pages.Add();
PdfImage pdfImage = PdfImage.FromFile("Avery5163.jpg");

pdfDoc.PageSettings.SetMargins(0);
page.Canvas.DrawImage(pdfImage, 0, 20);
}

pdfDoc.PageScaling = PdfPrintPageScaling.ActualSize;
pdfFileName = $"~/{AppConstants.PDF_FOLDER}/{Path.GetFileName(pImageFilePath).Replace(".jpg","")}.pdf";
pdfDoc.SaveToFile(System.Web.HttpContext.Current.Server.MapPath(pdfFileName));


Please advise,

Regards,

An Nguyen

anguyenalb
 
Posts: 8
Joined: Wed Feb 18, 2015 4:39 pm

Tue Jul 10, 2018 2:17 am

Dear An,

Thanks for your feedback.
There is another solution, please change the code "PdfPageBase page = pdfDoc.Pages.Add();" to below code.
Code: Select all
                //set the margin as 0 when adding the page
                PdfPageBase page = pdfDoc.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
               //pdfDoc.PageSettings.SetMargins(0); comment out this code

If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Jul 10, 2018 3:56 pm

Hurrah... it's working perfectly!
Thank you Betsy, your level of support is just fantastic.
We are going to recommend the use of this Spire-Pdf to other subsidiaries of our company as well.
Have a great day,

Regards,

An Nguyen

anguyenalb
 
Posts: 8
Joined: Wed Feb 18, 2015 4:39 pm

Wed Jul 11, 2018 1:50 am

Dear An,

Thanks for your feedback and it is my pleasure to help you.
Any question, just feel free to contact us.
Have a nice day:)

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF