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.

Mon Jun 03, 2019 2:04 pm

Dear Sirs,

I have to implement the printing of labels in a certain size. For that I have to create a new, empty pdf file and place an image in it. PDF creation and image placing is not the problem, problem is sizing the pdf file.

Label size is 10 x 15 cm.
Image size is 9 x 14

I convert the sizes to pixel and generate a new page with this sizes:
PdfDocument doc = new PdfDocument();
PdfUnitConvertor converter = new PdfUnitConvertor();
float width = converter.ConvertUnits(10, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Pixel);
float height = converter.ConvertUnits(15, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Pixel);
PdfPageBase page = doc.Pages.Add(new SizeF(width, height), new PdfMargins(0));

At least a add the label image with smaller sizes for width and height and save the file.
page.Canvas.DrawImage(label, xmargin, ymargin, iwidth, iheight);

The image is placed fine but the pdf file has a size of 13,3 x 20 cm and not the given 10 x 15.
So it seems to be not the correct way for sizing the pdf file.
Thanks for any support and best regards
Arne

astoffregen
 
Posts: 2
Joined: Fri Feb 23, 2018 12:31 pm

Tue Jun 04, 2019 2:34 am

Hello,

Thank you for contacting.
Since the internal default unit of our Spire.PDF is "point", please use the following code snippet to convert the sizes to point and to generate a PDF page with 10 x 15cm. If there is any other question, just feel free to write back.
Code: Select all
......
float width = converter.ConvertUnits(10, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
float height = converter.ConvertUnits(15, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
......

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1534
Joined: Wed Apr 25, 2018 3:20 am

Tue Jun 04, 2019 11:19 am

Lisa.Li wrote:Hello,

Thank you for contacting.
Since the internal default unit of our Spire.PDF is "point", please use the following code snippet to convert the sizes to point and to generate a PDF page with 10 x 15cm. If there is any other question, just feel free to write back.
Code: Select all
......
float width = converter.ConvertUnits(10, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
float height = converter.ConvertUnits(15, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
......

Sincerely,
Lisa
E-iceblue support team


Hello Lisa,
Thanks for such a quick feedback, works perfect.
Best regards
Arne

astoffregen
 
Posts: 2
Joined: Fri Feb 23, 2018 12:31 pm

Wed Jun 05, 2019 1:18 am

Hello,

Glad to hear that! Just feel free to contact us if you encounter any issue related to our products in the future.
Wish you all the best!

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1534
Joined: Wed Apr 25, 2018 3:20 am

Thu Apr 15, 2021 1:39 pm

I'm having a similar problem.

The page size of the PDF should equate to the same size of the largest image used to build the PDF. However, what we're finding is that it outputs at about three times that size.

We realized this when we pulled in an example PDF into PhotoShop and selected the "page" option to get the dimensions (see attached screenshot).

For example:
1. Largest image is 1687x1987
2. Expected page size is 1687x1987
3. Actual page output is 4979x5888 (see attached image)

Code: Select all
PdfUnitConvertor converter = new PdfUnitConvertor();

var desiredWidth = converter.ConvertUnits(1687, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);  //image width, converted from pixels to points
var desiredHeight = converter.ConvertUnits(1987, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point); //image height, converted from pixels to points

PdfPageBase page = pdfdoc.Pages.Add(new SizeF(desiredWidth, desiredHeight), new PdfMargins(0));


What is going on here? How can we make the page output match the largest image output?

mtceegee
 
Posts: 17
Joined: Wed Oct 28, 2020 4:32 pm

Fri Apr 16, 2021 3:23 am

Hello,

Thanks for your inquiry!

I tested your code to generate one PDF file, and then open the result file in the Adobe Acrobat Reader DC. The size of the generate PDF is 17.57*20.70 inch, and this is the same size as the 1687x1987pixel. I also attached the information of my generate PDF, and you can also calculate the pixels on your side with the following website (https://pixelsconverter.com/) to check whether the PDF is in the correct size.
size.png


Besides, I recommend that you can open your generate PDF in the professional PDF reader like Adobe Acrobat Reader DC, Pdf-XChange Editor and so on to get the size of it, because the Photoshop maybe has some loading methods its own, and I did not know much knowledge about it.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Apr 16, 2021 1:53 pm

Marcia.Zhou wrote:Hello,
I tested your code to generate one PDF file, and then open the result file in the Adobe Acrobat DC. The size of the generate PDF is 17.57*20.70 inch, and this is the same size as the 1687x1987pixel.


A 1687x1987 pixel image, assuming 300 pixels per inch, is supposed to translate to 5.623 inches x 6.623 inches, not 17.57 x 20.70 inches. That seems to be the disconnect.

The following is my full snippet of test code. I've attached a test image as well as the resulting output sizes as they are reported in both Adobe Acrobat DC and in PhotoShop. As you can see, I have a test image that is 3000x3600 pixels. That should translate to 10x12 inches. It doesn't.

Instead, it's indicating the dimensions are 31.25 x 37.5 inches. PhotoShop reports its pixel dimensions are 9375x11250. See attachments. Those are far higher numbers than they should be. 3000x3600 pixels, assuming 300 pixels per inch, is 10x12 inches.

Code: Select all
PdfDocument pdfdoc = new PdfDocument();
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfImage imageBackground = PdfImage.FromFile(@"..\images\test.png");

SizeF pageSize = new Size();         
int backgroundWidthPt = (int)Math.Round(unitCvtr.ConvertFromPixels(imageBackground.Width, PdfGraphicsUnit.Point), 0);
int backgroundHeightPt = (int)Math.Round(unitCvtr.ConvertFromPixels(imageBackground.Height, PdfGraphicsUnit.Point), 0);
pageSize.Width = backgroundWidthPt;
pageSize.Height = backgroundHeightPt;

PdfPageBase page = pdfdoc.Pages.Add(pageSize, new PdfMargins(0));
page.Canvas.Save();

page.Canvas.DrawImage(imageBackground, 0, 0, backgroundWidthPt, backgroundHeightPt);
page.Canvas.Restore();

MemoryStream stream = new MemoryStream();
pdfdoc.SaveToStream(stream, FileFormat.PDF);

byte[] bytes;
using (stream)
{
     bytes = stream.ToArray();
      fileContentResult = File(bytes, MimeTypes.ApplicationPdf, "test.pdf");
}


This is absolutely not the expected output. We're using version 7.4.5.

mtceegee
 
Posts: 17
Joined: Wed Oct 28, 2020 4:32 pm

Mon Apr 19, 2021 6:40 am

Hello,

Thanks for sharing more information!

Kindly note that our Spire.PDF converts pixel to pt with the system default dpi, it means that the generated PDF size depends on your system dpi setting. In my win7 system, the default dpi is 96, if I want to open the PDF in the Photoshop, I need to change the Resolution to 96, and set the Crop To to Media Box to get the real page size.

dpi.png


Hope you can understand!


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Wed Apr 21, 2021 5:27 pm

Marcia.Zhou wrote:
Kindly note that our Spire.PDF converts pixel to pt with the system default dpi, it means that the generated PDF size depends on your system dpi setting. In my win7 system, the default dpi is 96, if I want to open the PDF in the Photoshop, I need to change the Resolution to 96, and set the Crop To to Media Box to get the real page size.

Hope you can understand!


Sorry, I'm not sure that I do understand. We are providing our outputted PDF to a professional printer with the goal of having them use the PDF to print professional-grade print media. Their requirement is that we make the image resolution be 300 DPI. Is that not possible with Spire.PDF?

Is Spire.PDF generating the PDF at 96 DPI even though the images I'm embedding are 300 DPI? I exported the images from PhotoShop to be 3000x3600 pixels set at 300 DPI. I then embedded those images into my PDF using Spire.PDF, pulling them in from our database, as they are stored as binaries. Is there a setting I need to apply to my code to ensure that Spire.PDF recognizes these binary streams with the proper 300 DPI?

The print company we've provided our PDF to is indicating that when they look at our PDF, it "pre-flights" to 96 DPI instead of 300 DPI. That shouldn't be the case, because all the images we're embedding into the PDF are 300 DPI. What am I missing here?

mtceegee
 
Posts: 17
Joined: Wed Oct 28, 2020 4:32 pm

Thu Apr 22, 2021 6:22 am

Hello,

Thanks for your detailed description.

You mean that you need to draw an image with 300 DPI to a PDF of the same size as it, and ensure that the final generated PDF is 300 DPI too, right?

Our Spire.PDF supports this operation. According to the World Wide Web Consortion (W3C) provides: 1 inch = 72pt, and when dpi = 300, 1 inch = 300px. So when dpi = 300, 1px = 0.24pt. Please refer to the following code to set the px to pt and draw the image to PDF under the 300 DPI. I also attached my original 300 DPI image and the generated 300 DPI PDF for your reference.
Code: Select all
            PdfDocument pdfdoc = new PdfDocument();
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfImage imageBackground = PdfImage.FromFile(@"testPngRes.png");



            SizeF pageSize = new Size();
            //1 inch = 72pt, and when dpi = 300, 1 inch = 300px. So when dpi = 300, 1px = 0.24pt
            int backgroundWidthPt = Convert.ToInt32(imageBackground.Width * 0.24);
            int backgroundHeightPt = Convert.ToInt32(imageBackground.Height * 0.24);
            pageSize.Width = backgroundWidthPt;
            pageSize.Height = backgroundHeightPt;

            PdfPageBase page = pdfdoc.Pages.Add(pageSize, new PdfMargins(0));
            page.Canvas.Save();

            page.Canvas.DrawImage(imageBackground, 0, 0, backgroundWidthPt, backgroundHeightPt);
            page.Canvas.Restore();

            MemoryStream ms = new MemoryStream();
            pdfdoc.SaveToStream(ms, FileFormat.PDF);

            byte[] bytes;
            using (ms)
            {
                bytes = ms.ToArray();
                File.WriteAllBytes("testPngRes2.pdf", bytes);
            }
           

PNGsize.png
PDFsize.png


If you encounter any issues, just feel free to contact us.


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Thu Apr 22, 2021 1:30 pm

Marcia.Zhou wrote:Hello,

You mean that you need to draw an image with 300 DPI to a PDF of the same size as it, and ensure that the final generated PDF is 300 DPI too, right?


Not exactly. I am using an image that had been saved from PhotoShop at 300 DPI. When I right-click on the image in Windows and look at the image properties, the VerticalResolution and HorizontalResolutions are 300. We then upload that image to our web application, which stores the image as a binary.

Using Spire, we then pull that image from the binary and attempt to draw the image. It's there that we're seeing Spire identify it as having a resolution of 96 instead of 300.

The image, saved at 300 DPI, is 3000x3600. That should equate on the print side of things to be 10 inches x 12 inches if saved at 300 DPI.

Instead, what happens is Spire seems to take the 96 and build out a PDF that is three times that width and height, while also still reporting 96 DPI.

I hope that helps clarify our issue.

mtceegee
 
Posts: 17
Joined: Wed Oct 28, 2020 4:32 pm

Fri Apr 23, 2021 6:19 am

Hello,

I tested the “code.txt” in my attached file with the latest Spire.PDF V7.4.5, the code can draw 300 DPI, 3000x3600 pixel image binary to the PDF and keep the DPI 300, 10 inches x 12 inches.

Please try with the code and the latest version first. If the issue still exists, please provide us with your input file and target framework, project type for reference. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.PDF

cron