However, this is an interesting problem because I KNOW the image in the PDF is a TIFF - I put it there! ;)
qp.UnlockKey("<license>");
bool _error = false;
int _id = 0;
const string TifFile = "C:\\1.tif";
const string PDFFile = "C:\\1.pdf";
const string NewImageFile = "C:\\image.tif";
int _dpiX = 0;
int _dpiY = 0;
_id = qp.NewDocument();
if (_id == 0)
{
_error = true;
return;
}
// now, add the image from the temp. location
_id = qp.AddImageFromFile(TifFile, 1);
// select this as the current image
qp.SelectImage(_id);
if (_id == 0)
{
_error = true;
return;
}
qp.SelectImage(_id);
// Draw image on the current page
_dpiX = qp.ImageHorizontalResolution();
if (_dpiX == 0) _dpiX = 72;
_dpiY = qp.ImageVerticalResolution();
if (_dpiY == 0) _dpiY = 72;
// check the original pagesize
double ImageWidthInPoints = (double)qp.ImageWidth() / _dpiX * 72.0;
double ImageHeightInPoints = (double)qp.ImageHeight() / _dpiY * 72.0;
qp.SetPageDimensions(ImageWidthInPoints, ImageHeightInPoints);
qp.SetOrigin(1);
qp.DrawImage(0, 0, ImageWidthInPoints, ImageHeightInPoints);
if (qp.SaveToFile(PDFFile) != 1)
{
_error = true;
return;
}
/*
* Now we have a PDF with the TIF in it.
* The resolution is correct so we know QPDF is reading the image correctly
* The file exists on the disk and is a little larger than the source TIF. That's
* okay because we expect an overhead from the PDF wrapper
*
* Okay, so now reverse the process. Let's extract the same file and see what
* happens.
*
* We can make some assumptions: the PDF only has one image, only one page.
* This makes the selection logic easy. In the real world,
* we would be passing parameters that change these values.
*/
int _DocRef = qp.LoadFromFile(PDFFile, "");
if (_DocRef == 0)
{
_error = true;
return;
}
if (qp.SelectPage(1) == 0)
{
_error = true;
return;
}
int imageList = qp.GetPageImageList(0);
if (imageList == 0)
{
_error = true;
return;
}
int ImageListCounter = qp.GetImageListCount(imageList);
int FindImages = qp.FindImages();
// for reasons best known to PDF, my file has 37 items in the FindImages list...
// so, let's check them *all* for resolution and hope one matches the '200'
// we know our original TIF had...
int p = 0;
int[] _set = new int[FindImages];
for (int j = 0; j <= FindImages-1; j++)
{
p = qp.GetImageID(j+1);
if (p > 0)
{
_set[j] = p;
}
}
int[,] imageids = new int[36,2];
for (int j=1; j<=36; j++)
{
imageids[j - 1, 0] = qp.SelectImage(_set[j - 1]);
imageids[j - 1, 1] = qp.ImageHorizontalResolution();
}
int ImageItem = qp.GetImageListItemIntProperty(imageList, 1, 400);
// now we can read (to file) the first image on the current page
if (qp.SaveImageListItemDataToFile(imageList, 1, 0, NewImageFile) == 0)
{
_error = true;
return;
}
Now, if you run this you'll find that I don't get a HorizontalResolution in any one of the 37 image entries! So: where the hell's my image gone?? ;)
I'm confused: the PDF is 40Kb..so it MUST have a well-compressed copy of my TIF in there - so why the heck can't I get it out in that format?