Export Image from pdf

PDF-XChange Editor SDK for Developers

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

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.
mscdp
User
Posts: 3
Joined: Mon Aug 03, 2026 1:22 pm

Export Image from pdf

Post by mscdp »

I'm trying to extract images without rerendering, from an pdf document, where each page contains just one image. The idea is just save the image (which is possible in the pdfXChangeEditor using the content bar) and not using ""op.document.exportToImages" which does (from what I saw) some new rendering and so some quality lossesw.

Code: Select all

               IPXC_Page actPage = doc.Pages[actPageIdx];
               IPXC_Content content = actPage.GetContent(PDFXEdit.PXC_ContentAccessMode.CAccessMode_Readonly);
               for (uint j = 0; (j < content.Items.Count); j++)
               {
                   PDFXEdit.IPXC_ContentItem ci = content.Items[j];
                   if ((ci.Type == PDFXEdit.PXC_CIType.CIT_Image) || (ci.Type == PDFXEdit.PXC_CIType.CIT_InlineImage))
                   {
                           IPXC_Image ipxcImage = ci.Image_Object;
                           uint hight = ipxcImage.Height;
                           uint width = ipxcImage.Width;
                           IMemBlock mem = null;
                           mem = ipxcImage.GetImageData(PXC_ImageDataType.kIDT_Raw);
                    }
                 }
But that did not work, I got an exception for every defined PXC_ImageDataType. From the ImageData I hope it ios possiple to create a bitmap which could be exported.
User avatar
MishaH
User
Posts: 37
Joined: Wed Sep 11, 2024 1:43 pm

Re: Export Image from pdf

Post by MishaH »

Hello, mscdp

Welcome to our forums!

It seems that, in your case, using the `op.document.extractImage` operation would help.

Please try this:

Code: Select all

private bool GetFirstImageOnPage(PDFXEdit.IPXC_Content content, ref PDFXEdit.IPXV_ContentItemEntry itEntry)
{
	for (uint i = 0; i < content.Items.Count; i++)
	{
		PDFXEdit.IPXC_ContentItem it = content.Items[i];
		if (it.Type == PDFXEdit.PXC_CIType.CIT_Image)
		{
			itEntry.Insert(i);
			return true;
		}
		else if (it.Type == PDFXEdit.PXC_CIType.CIT_XForm)
		{
			PDFXEdit.IPXC_XForm xf = content.Document.GetXFormByHandle(it.XForm_Handle);
			if (xf != null)
			{
				PDFXEdit.IPXV_ContentItemEntry ent = itEntry.Insert(i);
				bool b = GetFirstImageOnPage(xf.GetContent(PXC_ContentAccessMode.CAccessMode_Readonly), ref ent);
				if (b == false)
					itEntry.Remove(i, 1);
				else
					return true;
			}
		}
	}
	return false;
} 

Code: Select all

private void ExportImage(PDFXEdit.IPXV_Inst Inst, PDFXEdit.IPXV_Document Doc)
{
	int nSelID = Inst.Str2ID("selection.contentItems", false);
	uint nPage = 0; //Page number that will have it's image content item exported
	//First we need to create content items selection
	PDFXEdit.IPXV_ContentItemsSelection itSel = (PDFXEdit.IPXV_ContentItemsSelection)Doc.CreateStdSel((uint)nSelID);
	//Then we need to get root item entry for the desired page (we'll create it if needed)
	PDFXEdit.IPXV_ContentItemEntry itEntry = itSel.GetSel(nPage, true);
	//Inserting needed content items from the page
	var content = Doc.CoreDoc.Pages[nPage].GetContent(PDFXEdit.PXC_ContentAccessMode.CAccessMode_Readonly);
	if (GetFirstImageOnPage(content, ref itEntry) == false)
		return;

	int nID = Inst.Str2ID("op.document.extractImage", false);
	PDFXEdit.IOperation Op = Inst.CreateOp(nID);
	var input = Op.Params.Root["Input"];
	input.Add().v = Doc.CoreDoc;
	PDFXEdit.ICabNode options = Op.Params.Root["Options"];

	//Adding selected page's root entry with added image content item to the operation
	options["Entry"].v = itEntry;
	//Setting resulting png file name
	options["FileName"].v = @"D:\Image_res.png";
	//Setting whether the image should be extracted visually or as it is in the PDF content
	options["ExtractVisually"].v = false;
	//Extracting image content item
	Op.Do();
	//Clearing the selection
	itSel.Clear();
} 
Please let me know if this works for you.

Kind regards,
Misha
mscdp
User
Posts: 3
Joined: Mon Aug 03, 2026 1:22 pm

Re: Export Image from pdf

Post by mscdp »

Hi Misha,

thanks, that is mainly, what I'm searching for.

It took me some time, to test, because I normally worked with the IPXC_Document in the core level. But there I was not able to create the PDFXEdit.IPXV_ContentItemsSelection I need for the PDFXEdit.IPXV_ContentItemEntry. Or is there a way to do it with a IPXC_ContentItem.
I think the Exract ha the same restriction as the PDFXChange Editor so there is only the option to export png or jpg. So I have to convert afterward to tif.

Kind regards,

Michael
User avatar
MishaH
User
Posts: 37
Joined: Wed Sep 11, 2024 1:43 pm

Re: Export Image from pdf

Post by MishaH »

Hi Michael,

Your clarifications are important. Indeed, this can be done entirely at the Core level (IPXC) utilizing the IXC (Image Core) interfaces, which allows you to set the resulting TIFF format immediately without any additional conversions.

The steps are as follows:
1. Obtain the needed `IPXC_ContentItem` for the image that you want to save.
2. Create the `IIXC_Page` directly from the content item using the `IPXC_ContentItem::Image_CreateIXCPage` method.
3. Configure the file format (TIFF) via the `FmtInt` property of the `IIXC_Page` object.
4. Create a new empty `IIXC_Image` using the `IIXC_Inst::CreateEmptyImage` method.
5. Add the previously created `IIXC_Page` to the `IIXC_Image` object via the `InsertPage` method.
6. Use the `IIXC_Image::Save` method to save it as a TIFF.

Here is a code example showing how to do this:

Code: Select all

IIXC_Inst icInst = (IIXC_Inst)pxcInst.GetExtension("IXC");

IPXC_ContentItem cItem = ...; 

IIXC_Page imgPage = cItem.Image_CreateIXCPage();
imgPage.FmtInt[(uint)IXC_FormatParametersIDS.FP_ID_FORMAT] = (uint)IXC_ImageFileFormatIDs.FMT_TIFF_ID;
IIXC_Image img = icInst.CreateEmptyImage();
img.InsertPage(imgPage);
img.Save("D:\\TestFile.tiff", IXC_CreationDisposition.CreationDisposition_Overwrite);
I hope this helps.
Please let me know if this works for you.

Kind regards,
Misha
mscdp
User
Posts: 3
Joined: Mon Aug 03, 2026 1:22 pm

Re: Export Image from pdf

Post by mscdp »

Hi Misha,

thanks a lot, that is exactly the functionality I was searching for. Works perfect also in a multi threading setup.
There is only one detail, I was not able to fix and I did not find any example or detailed information in the sdk doku.
I want to set some options in the tif export (like resolution in dpi) . Is there a possibility to do that?

Best regards

Michael
User avatar
MishaH
User
Posts: 37
Joined: Wed Sep 11, 2024 1:43 pm

Re: Export Image from pdf

Post by MishaH »

Hi Michael,

I'm glad to hear that it works perfectly for you!

To configure additional export options, you can use the same `IXC_FormatParametersIDS` enum that we used for setting the TIFF format. You can check the full list of available flags here: https://sdkhelp.pdf-xchange.com/view/PX ... ametersIDS

Regarding your specific example with DPI, you should use the `FP_ID_XDPI` and `FP_ID_YDPI` flags. Here is a code example:

Code: Select all

const uint dpiX = 300;
const uint dpiY = 500;

imgPage.FmtInt[(uint)IXC_FormatParametersIDS.FP_ID_XDPI] = dpiX;
imgPage.FmtInt[(uint)IXC_FormatParametersIDS.FP_ID_YDPI] = dpiY;
Let me know if this does the trick for you.

Best regards,
Misha