Search code examples
bitmappdftronpdfnet

Retrieving pdf page bitmap with its original size and dpi


I'm wondering why i'm getting a bitmap from a page that has a different size/dpi than the bitmap i used to create the page.

example: bmp is a Bitmap with a Width of 1275 and Height 1651 at a dpi of 150. I use this bitmap to create the page. When i use PDFDraw to retrieve the Bitmap at the end of my code, Bitmap b has a Width of 2657 and height of 3440 at 150 dpi. Why has this changed and how can i get my original bitmap back?

//create a page
var imgRect = new Rect(0, 0, bmp.Width, bmp.Height);
pdftron.PDF.Page _currentPage = convertedPdf.PageCreate(imgRect);
imgRect.Dispose();

//start writing to a page                                               
elementWriter.Begin(_currentPage, ElementWriter.WriteMode.e_underlay, false);

//write the image   
var element = elementBuilder.CreateImage(_currentImg, new pdftron.Common.Matrix2D(bmp.Width, 0, 0, bmp.Height, 0, 0));                       
elementWriter.WritePlacedElement(element);

//cleanup
_currentImg.Dispose();

//add the page to the pdf
convertedPdf.PagePushBack(_currentPage);

//just a test for retrieving the page later on
PDFDraw drawer = new PDFDraw();
Bitmap b = drawer.GetBitmap(_currentPage);

Solution

  • You can call Convert.ToPDF(pdfdoc, path_to_image) and simply pass in the path to the image, and PDFNet will do all the calculations for you, plus auto rotation in the latest version.

    In particular your page size calculation is off. It "should" be

    var imgRect = new Rect(0, 0, bmp.Width / 150.0 * 72.0, bmp.Height / 150.0 * 72.0);

    So you get a PDF page with the true physical dimension of the image.