Reputation: 15
I have a requirement to create pdf with images for printing. I am adding a high resolution image to pdf using iText. It's reducing the quality to 72 DPI. Resulting in poor quality of image after printing.
The original image resolution is 2549*3304 and DPI (300).
I tried below options
image.scaleAbsolute(2549*.24 ,3304*.24 );
image.setDpi(300,300);
image.scaleToFit(2549*.24 ,3304*.24 );
current code looks like this
Document document = new Document(PageSize.LETTER);
document.open();
Image image = Image.getInstance("C:/Project/bg.png");
image.setAbsolutePosition(0,0);
image.scalePercent(24);
document.add(image);
document.close();
I went through some threads (Adding an image to a PDF using iTextSharp and scale it properly) still not able to solve the problem
Can anyone please help me on this to get better image quality while printing?
Upvotes: 0
Views: 2683
Reputation: 77528
The setDpi()
method is irrelevant in your code, so is the DPI of your original image. Pixels are treated as points in iText. This means that adding an image as-is will result in having 72 pixels per inch.
You scale the image to 24 percent of the original size. This means that you increase the resolution: you are showing the same number of pixels using only 24% of the space. In this case, you are showing 2549 pixels distributed over 611.76 points. This is about 8.5 inch, which means you indeed have a resolution of 300 DPI.
I think the problem isn't caused by the resolution of the image inside the PDF (but it's hard to tell because we can't inspect the PDF). I think the problem is caused by the printing process that prints the document using a resolution that is different from the resolution in the PDF.
In any case: iText does not reduce the number of pixels if you use the methods scalePercent()
, scaleAbsolute()
or scaleToFit()
.
Extra info based on Comments:
Asking a PDF for its "resolution" doesn't make sense, because a PDF doesn't have any resolution (although images inside a PDF may have one). I have no idea why Photoshop tells you the resolution is 72 DPI. Maybe that's a default value because the measurment unit in PDF corresponds with a point and there are 72 points in one inch.
I have examined the PDF that you shared. I don't see any loss of resolution when I look at it on my screen. I can see that the document measures 8.5 by 11 inch. As for the image, please take a look at the report generated by Acrobat:
It says width/height: 2550/3300 in pixel.
2550 / 8.5 = 300
3300 / 11 = 300
Hence the resolution is 300 pixels per inch. Or: the PDF is created exactly the way you want it.
However: you say that the resolution is worse when you print the document. This can be caused by many different things: maybe you're printing on a page that is larger than 8.5 by 11 inch, maybe the printer can't print at that resolution, maybe the PDF viewer can only print using "degraded printing",...
My advice is that you test this PDF on different printers using different viewers to find the culprit.
Upvotes: 1