AddImageFromMemory using only Core API

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, 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.
Post Reply
numerobis
User
Posts: 7
Joined: Wed Aug 24, 2016 3:22 pm

AddImageFromMemory using only Core API

Post by numerobis »

Hi,

I'm trying to insert an image from memory into a page (I have a C# Bitmap/Image object). I found the following code:
viewtopic.php?f=66&t=25859&p=101045&hil ... am#p101050

It uses the PDFXEdit API with the AddImageFromIXCPage() function to achieve what I'm looking for. Is there a way to use pure PDFXCoreAPI functionality to achieve the same? I see the two methods IPXC_Document.AddImageFromMemory(..) and IPXC_Document.AddImageFromStream(..), but haven't found a working example how to use them without the PDFXEdit API.

Any help greatly appreciated!
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: AddImageFromMemory using only Core API

Post by Sasha - Tracker Dev Team »

Hello numerobis,

The AddImageFromIXCPage method is of the IPXC_Document interface - that sample uses the IPXV_Inst only to obtain the IIXC_Inst. Thus you can freely use that sample for your needs in the CoreAPI. Also, be sure to check out this sample project:
https://github.com/tracker-software/PDF ... oreAPIDemo

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
numerobis
User
Posts: 7
Joined: Wed Aug 24, 2016 3:22 pm

Re: AddImageFromMemory using only Core API

Post by numerobis »

Hello Alex,

I was able to extract what I need from the example you sent me. Works like a charm, thanks! Below a working example for anybody else who might stumble upon the same requirement.

Code: Select all

// You are responsible for calling the GDI DeleteObject method to free the memory used by 
// the GDI bitmap object. When you call bmp.GetHbitmap() a copy of the bitmap is created. 
// You'll need to keep a reference to the pointer to that object and call DeleteObject(...)
// From: https://stackoverflow.com/questions/1714841/image-loading-memory-leak-with-c-sharp
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

IPXC_Image addImageFromBitmap(IPXC_Document doc, Bitmap bmp, PXC_Inst inst)
{
	IntPtr bHandle = bmp.GetHbitmap();
	IIXC_Inst ixcInst = inst.GetExtension("IXC");
	IIXC_Page ixcPage = ixcInst.Page_CreateFromHBITMAP((uint)bHandle, 0);
	IPXC_Image img = doc.AddImageFromIXCPage(ixcPage);

	// Free up unmanaged memory
	DeleteObject(bHandle);
	return img;
}

void addImage(IPXC_Document doc, PXC_Rect rect, uint page, Bitmap bmp, PXC_Inst inst)
{
	IPXC_Image image = addImageFromBitmap(doc, bmp, inst);
	PXC_Matrix matrix = new PXC_Matrix();
	matrix.a = Math.Abs(rect.right - rect.left);
	matrix.d = Math.Abs(rect.bottom - rect.top);
	matrix.e = rect.left;
	matrix.f = Math.Min(rect.top, rect.bottom);
	IPXC_ContentCreator cc = doc.CreateContentCreator();
	cc.ConcatCS(matrix);
	cc.PlaceImage(image);
	doc.Pages[page].PlaceContent(cc.Detach());
}

void createPdfFromBitmap(Bitmap bmp)
{
	var inst = new PXC_Inst();
	inst.Init("<Your Product Key>");
	string outputFile = "MyFancyBitmap.pdf";
	
	// Create Docuemnt
	IPXC_Document doc = inst.NewDocument();

	// Add page
	var rect = new PXC_Rect();
	rect.right = 1000;
	rect.bottom = 1410;
	IPXC_UndoRedoData urd;
	doc.Pages.AddEmptyPages(0, 1, ref rect, null, out urd);

	// Add image to page
	addImage(doc, rect, 0, bmp, inst);

	// Save & close document
	doc.WriteToFile(outputFile);
	doc.Close();
}

// Don't forget to call bmp.Dispose().
Last edited by numerobis on Fri May 17, 2019 11:54 am, edited 2 times in total.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: AddImageFromMemory using only Core API

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply