extract image from PDF files

PDF-XChange Editor SDK for Developers

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

extract image from PDF files

Post by chavas »

Hi,

I have to convert some pdf files to tiff. These are scanned pdf files which contain a single image per page. I need to get that image out and save it as tiff. Is this possible with PDFXchange SDK? If yes, then pls guide me how to do so.

Rgds

PS. I do not want to rasterise the PDF page.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: extract image from PDF files

Post by Sasha - Tracker Dev Team »

Hello chavas,

To extract the image content item, you should:
1) Get to the needed IPXC_Content item that is an Image.
2) Use the https://sdkhelp.pdf-xchange.com/vi ... ateIXCPage method to create the IIXC_Page.
3) Use the IIXC_Page to create the IIXC_Image that you can save to the needed format. The CoreAPIDemo holds some samples on how to use the IIXC interfaces.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: extract image from PDF files

Post by chavas »

Hi Alex,

Using your guidance I was able to get to the images inside the files. But when I am writing it to tiff, it is saving in 96 dpi and not maintaining the original dpi of the image. Here is my code-

IPXC_Content content = pdfCtl.Doc.CoreDoc.Pages[0].GetContent(PXC_ContentAccessMode.CAccessMode_Readonly);
for (uint i = 0; i < content.Items.Count; i++)
{
IPXC_ContentItem item = content.Items;
if (item.Type == PXC_CIType.CIT_Image)
{
IPXC_Image imgobj= item.Image_Object;
IIXC_Page iPage=imgobj.CreateIXCPage();
Bitmap b = new Bitmap((int)iPage.Width, (int)iPage.Height);
Graphics g = Graphics.FromImage(b);
g.Clear(Color.White);
IntPtr hdc = g.GetHdc();
iPage.DrawToDC((uint)hdc, 0, 0, iPage.Rect.right - iPage.Rect.left, iPage.Rect.bottom - iPage.Rect.top, 0, 0);
g.ReleaseHdc(hdc);
b.Save("I://Image_" + i + ".TIFF");
}

}
}

Pls let me know where I am going wrong and how to fix it.

Rgds
Charu
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: extract image from PDF files

Post by Sasha - Tracker Dev Team »

Hello chavas,

Please check the UsefulResouces topic - there is a gist sample link on how to work with IIXC_Image there:
https://forum.pdf-xchange.com/viewtopic.php?f=66&t=25943

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: extract image from PDF files

Post by chavas »

Hi Alex, Sasha,

The gist sample link that you showed me writes images to a jpg file with a custom dpi. It is useful if I want to render the image. But I want to retain its original dpi and color scheme and save the same image object.
We are using your licensed product and had written to your support team too. They asked me to contact you and forwarded my email to you.

Pls guide me how to achieve the above.

Rgds
Charu
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: extract image from PDF files

Post by Sasha - Tracker Dev Team »

Hello chavas,

Can you achieve what you need with this functionality?
image.png
Cheers,
Alex
You do not have the required permissions to view the files attached to this post.
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: extract image from PDF files

Post by chavas »

Hi Alex,

This functionality allows me to save the selected image in png format. I want to save it as tiff and maintain the dpi and color scheme of the image.

Rgds
Charu
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: extract image from PDF files

Post by Sasha - Tracker Dev Team »

Hello chavas,

Here's how the Save Image as Works:
1) Obtain the needed IPXC_ContentItem for the image that you want to save.
2) Get Image's handle from it and use the IPXC_Document::GetImageByHandle to obtain the IPXC_Image from it.
3) Use the CreateIXCPage method of the IPXC_Image to create the IIXC_Page
4) Create new IIXC_Image by using the IIXC_Inst method.
5) Now, set the needed format of the IIXC_Image via the FmtInt property and then use the IIXC_Image::AddPage to add the previously created IIXC_Page to it.
6) Use IIXC_Image;:SaveToFile method to save it where you need.


Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: extract image from PDF files

Post by chavas »

Hi Alex,

How do I get the DPI and color format of the IPXC_Image?
I need to maintain the original DPI and color format of the image.
See the attached image

Rgds
You do not have the required permissions to view the files attached to this post.
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: extract image from PDF files

Post by chavas »

Hi Alex, Sasha,

We are your licensed users. We are in final stages of our module. It is extremely urgent for us to get this module done. So pls reply at the earliest. Your help in this regard will be highly appreciated

Rgds
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: extract image from PDF files

Post by Sasha - Tracker Dev Team »

Hello chavas,

For starters, you will have to calculate a cascade matrix for the current IPXC_Image.
First, you should have to obtain a PXC_Matrix from page and then dive deep into the content (and contents of possible XForms until you get to your IPXC_Image content item.
For each of the parent XForms you will have to call this method while passing the XForm to it

Code: Select all

void MultiplyMatrix(IAUX_Inst* pAUX, IPXC_ContentItem* pCI, PXC_Matrix& mCascadeMatrix, IPXC_XForm* pXForm = nullptr)
{
	CComPtr<IMathHelper> pMathHelper;
	pAUX->get_MathHelper(&pMathHelper);
	PXC_Matrix mCI;
	pCI->GetCTM(&mCI);
	PXC_Matrix mX;
	pMathHelper->Matrix_Reset(&mX);
	if (pXForm)
		pXForm->get_Matrix(&mX);
	
	pMathHelper->Matrix_Multiply(&mX, &mCI, &mX);
}
When you get to the IPXC_Image that you need, call the MultiplyMatrix method with pXForm = nullptr.

After that, you can calculate the DPI (that is being shown in the dialog):

Code: Select all

void GetImageDPI(IAUX_Inst* pAUX, IPXC_Image* pImage, PXC_Matrix& mCascadeMatrix, DWORD& nMaxDPIX, DWORD& nMaxDPIY, DWORD& nMinDPIX, DWORD& nMinDPIY)
{
	double nWidthIn = 0;
	double nHeightIn = 0;
	CComPtr<IMathHelper> pMathHelper;
	pAUX->get_MathHelper(&pMathHelper);
	// Calculate image size in pt
	PXC_Point pt0 { 0, 0 };
	pMathHelper->Point_Transform(&mIM, &pt0);
	PXC_Point pt1{ 0, 1 };
	pMathHelper->Point_Transform(&mIM, &pt1);
	nHeightIn = sqrt(pow((pt1.x - pt0.x), 2) + pow((pt1.y - pt0.y), 2));
	pt1.x = 1.0;
	pt1.y = 0.0;
	pMathHelper->Point_Transform(&mIM, &pt1);
	nWidthIn = sqrt(pow((pt1.x - pt0.x), 2) + pow((pt1.y - pt0.y), 2));

	ULONG_T nWidth = 0;
	ULONG_T nHeight = 0;
	pImage->get_Width(&nWidth);
	pImage->get_Height(&nHeight);
	nMaxDPIX = (DWORD)(nWidth / (nWidthIn / 72) + 0.5);
	nMaxDPIY = (DWORD)(nHeight / (nHeightIn / 72) + 0.5);
	double dMinDPIX = 1 / (nWidthIn / 72);
	double dMinDPIY = 1 / (nHeightIn / 72);
	nMinDPIX = (DWORD)((dMinDPIX > 1.0) ? dMinDPIX : 1);
	nMinDPIY = (DWORD)((dMinDPIY > 1.0) ? dMinDPIY : 1);
}
Note, that the same image can be used several times on the same page with different DPI.

Also, we only give the possibility to write via the CreateIXCPage method that changes the color profile.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ