AddImageFromStream

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.
jeffp
User
Posts: 914
Joined: Wed Sep 30, 2009 6:53 pm

AddImageFromStream

Post by jeffp »

I need to add an image to my PDF page via a Delphi bitmap. AddImageFromStream seems to be the right call but I can't figure out how to get the bitmap stream into your IStream interface. Can you show me how? Or is there a better call?

Code: Select all


function TMyPDF.BitmapToImage(B: TBitmap): IPXC_Image;
var
  MS: TMemoryStream;
  AStream: IStream;
begin
  if Assigned(FDoc) then
  begin
    MS := TMemoryStream.Create;
    try
      B.SaveToStream(MS);

      //How do I assign MS to AStream????

      Result := FDoc.AddImageFromStream(AStream, 0, 0);
    finally
      MS.Free;
    end;
  end;
end;

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

Re: AddImageFromStream

Post by Sasha - Tracker Dev Team »

Hello Jeff,

The correct way to do that would be like this:

Code: Select all

private void bitmapToImageToolStripMenuItem_Click(object sender, EventArgs e)
{
	Bitmap i = (Bitmap)Bitmap.FromFile(System.IO.Directory.GetParent(System.Environment.CurrentDirectory).Parent.FullName + "\\Resources\\calculator.png");
	uint hHB = (uint)i.GetHbitmap();
	PDFXEdit.IIXC_Inst iInst = (PDFXEdit.IIXC_Inst)pdfCtl.Inst.GetExtension("IXC");
	PDFXEdit.IIXC_Page iPage = iInst.Page_CreateFromHBITMAP(hHB, 0);
	PDFXEdit.IPXC_Document doc = pxcInst.NewDocument();
	PDFXEdit.IPXC_Image img = doc.AddImageFromIXCPage(iPage);
	PDFXEdit.PXC_Rect rcMedia;
	rcMedia.left = 0;
	rcMedia.bottom = 0;
	rcMedia.top = img.Height;
	rcMedia.right = img.Width;
	PDFXEdit.IPXC_UndoRedoData urData;
	PDFXEdit.IPXC_Page page = doc.Pages.InsertPage(0, ref rcMedia, out urData);

	PDFXEdit.IPXC_ContentCreator CC = doc.CreateContentCreator();
	//We'll need to do matrix Rect2Rect from the rectangle with 1pt width and height (as it's interpreted in the PDF specification) into the page rect
	PDFXEdit.PXC_Matrix m;
	//The result of Rect2Rect will be
	m.a = rcMedia.right - rcMedia.left; //width
	m.b = 0;
	m.c = 0;
	m.d = rcMedia.top - rcMedia.bottom; //height
	m.e = rcMedia.left;
	m.f = rcMedia.bottom;
	CC.ConcatCS(m);
	CC.PlaceImage(img);
	page.PlaceContent(CC.Detach(), (uint)PDFXEdit.PXC_PlaceContentFlags.PlaceContent_Replace);

	pdfCtl.OpenDocFrom(doc);
}
I took this code from some other forum topic and tweaked it a little bit. It will add image on page by the page's size.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
jeffp
User
Posts: 914
Joined: Wed Sep 30, 2009 6:53 pm

Re: AddImageFromStream

Post by jeffp »

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

Re: AddImageFromStream

Post by Sasha - Tracker Dev Team »

Glad that helped Jeff.
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ