1

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);
DennisVA
  • 2,068
  • 1
  • 25
  • 35
  • Is it possible to post the image in question. In particular I am curious how you know what the DPI is. Also, what version of PDFNet are you using? You can call PDFNet.GetVersion at runtime. – Ryan Jan 31 '18 at 19:44
  • In debug mode its possible to see vertical and horizontal resolution (dpi) of the bitmap. In my case it will always be one at 300 or 150 dpi. I'm not at work right now so can't post the image sorry – DennisVA Jan 31 '18 at 21:27

1 Answers1

0

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.

Ryan
  • 2,473
  • 1
  • 11
  • 14
  • Thanks Ryan, but why do i need to divide by 250? – DennisVA Jan 31 '18 at 21:19
  • shouldn't this be the dpi of the bitmap (150 in my example described in the question)? – DennisVA Jan 31 '18 at 21:32
  • 1
    Yes, that was a typo. corrected to 150. But yes it would be the image DPI. The image resolution + image DPI tells you the physical dimensions of the photo, and we scale that to match PDF DPI of 72. – Ryan Feb 02 '18 at 17:37