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: 1
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: 33
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