Place Images

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.
Post Reply
mkellers
User
Posts: 8
Joined: Tue Dec 29, 2015 12:47 pm

Place Images

Post by mkellers »

Hi all,

i have a problem with placing images on an existing page. Referencing to your post in another topic https://forum.pdf-xchange.com/ ... 003#p95003, i build a small c# class to place an image on the first page.

The Problem is, after placing the image, the visible context of the page will not redraw and so i can't see the image. If i zoom in or zoom out i can see the image. If i return to the initial zoomlevel the image is invisible again. If i place the image outside the visible context and (e.g.) scroll down to the image location, the image will drawn. (On the other hand, if the image is placed half inside the visible context, the part outside the visible context will be drawn.) After using the "EditingContent-Tool" and moving the image in any direction, the image will drawn permanently.

I made a small Video for better understanding, but for some reasons i am not able to upload it (5.6MB) :(

How can i force redrawing the page?

And just two more "simple" Questions :)
1. What do i have to do if i want to select the image programatically after placing on the page?
2. For now i can't "undo" the operation, is it possible to do this in any way?

PS: I know, the easiest way ist to execute the command 3524 ("cmd.document.addImage") but that's to simple ;)

This is the PlaceImage-Class:

Code: Select all

using System.Drawing;
using AxPDFXEdit;
using PDFXEdit;

namespace vv5.PdfXchangeServices {
	public class PlaceImageService {
		private readonly AxPXV_Control _pdfEditControl;

		public PlaceImageService(AxPXV_Control pdfEditControl) {
			_pdfEditControl = pdfEditControl;
		}

		public void PlaceImage(string fileFullname, Point position) {
			var pages = _pdfEditControl.Inst.ActiveDoc.CoreDoc.Pages;
			var page = pages[0];
			var image = _pdfEditControl.Inst.ActiveDoc.CoreDoc.AddImageFromFile(fileFullname, 0, (uint)PXC_ImageToXObjectFlags.ITX_DonotModifySrc);
			PXC_Rect rect;
			rect.left = position.Y;
			rect.right = image.Width + rect.left;
			rect.bottom = position.X;
			rect.top = image.Height + rect.bottom;

			PlaceImage(page, image, rect);
		}

		private void PlaceImage(IPXC_Page page, IPXC_Image image, PXC_Rect rect) {
			var doc = page.Document;
			var content = page.GetContent(PXC_ContentAccessMode.CAccessMode_WeakClone);
			var contentCreator = doc.CreateContentCreator();
			contentCreator.Attach(content);
			
			PlaceImage(contentCreator, image, rect);
			page.PlaceContent(content, (uint)PXC_PlaceContentFlags.PlaceContent_Replace);
		}

		private void PlaceImage(IPXC_ContentCreator contentCreator, IPXC_Image image, PXC_Rect rect) {
			var im = new PXC_Matrix();
			im.a = rect.right - rect.left;
			im.d = rect.top - rect.bottom;
			im.e = rect.left;
			im.f = rect.bottom;

			contentCreator.SaveState(); 
			contentCreator.ConcatCS(ref im);
			contentCreator.PlaceImage(image);
			contentCreator.RestoreState();
		}
	}
}
Last edited by mkellers on Tue Jan 05, 2016 2:08 pm, edited 1 time in total.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Place Images

Post by Sasha - Tracker Dev Team »

Hello mkellers,

If you're using Editor SDK - there is an easier way of placing an image on the page - the op.document.addImage operation. It will update the Pages View itself and also all the needed places like Thumbnails View and also will give you the possibility to undo\redo the operation work. Tomorrow I will write you a small guide on how to use this operation.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
mkellers
User
Posts: 8
Joined: Tue Dec 29, 2015 12:47 pm

Re: Place Images

Post by mkellers »

Hi Alex,

the operation "op.document.addImage" sounds good :) But i fail already at generating the IOperation. CreateOp() returns NULL, what am i doing wrong?

Code: Select all

int id = _pdfEditControl.Inst.Str2ID("op.document.addImage", false);
IOperation op = _pdfEditControl.Inst.CreateOp(id);
// op == NULL
Regards
Kellers
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Place Images

Post by Sasha - Tracker Dev Team »

Hello mkellers,

I've checked the operation and the problem is that it was declared for the next release (316) so you will have to wait for another week or so and it would be available. Meanwhile I will write a short sample with comments on how to use it. ;)

HTH
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Place Images

Post by Sasha - Tracker Dev Team »

You can check the code sample here https://sdkhelp.pdf-xchange.com/vie ... t_addImage
It will be available from the next build (316).

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
mkellers
User
Posts: 8
Joined: Tue Dec 29, 2015 12:47 pm

Re: Place Images

Post by mkellers »

Thank you, then i will wait for the next build.

But just for your information (or anybody else's :) ): I found a workaround with the "op.addContent" operation. It will place the image on the page (without rendering problems) and you can undo/redo this operation.

This is the function (I tried to keep the style you tooked for the "op.document.addImage"-Example):

Code: Select all

		private void AddContentToFirstPage(PDFXEdit.IPXV_Document Doc, PDFXEdit.PXV_Inst Inst, PDFXEdit.IPXC_Content content) {

			if (Doc == null)
				return;

			int nID = Inst.Str2ID("op.addContent", false);
			PDFXEdit.IOperation pOp = Inst.CreateOp(nID);
			PDFXEdit.ICabNode input = pOp.Params.Root["Input"];
			input.Add().v = Doc;

			PDFXEdit.ICabNode options = pOp.Params.Root["Options"];
			options["Content"].v = content;
			options["InsertBefore"].v = false;
			options["InsertClone"].v = false;
			options["TargetPage"].v = 0; //First page

			pOp.Do();
		}
And this is the full class to place an Image on the Pdf:

Code: Select all

using System.Drawing;
using AxPDFXEdit;
using PDFXEdit;

	public class AddImageHybridOperationService {
		private readonly AxPXV_Control _pdfEditControl;

		public AddImageHybridOperationService(AxPXV_Control pdfEditControl) {
			_pdfEditControl = pdfEditControl;
		}

		public void PlaceImage(string fileFullname, Point position)
		{
			var pages = _pdfEditControl.Inst.ActiveDoc.CoreDoc.Pages;
			var page = pages[0];
			var image = _pdfEditControl.Inst.ActiveDoc.CoreDoc.AddImageFromFile(fileFullname, 0, (uint)PXC_ImageToXObjectFlags.ITX_DonotModifySrc);
			var rect = RectFromImageOnPosition(image, position);

			var contentCreator = page.Document.CreateContentCreator();
			PlaceImage(contentCreator, image, rect);
			var content = contentCreator.Detach();

			AddContentToFirstPage(_pdfEditControl.Doc, _pdfEditControl.Inst, content);
		}

		private void PlaceImage(IPXC_ContentCreator contentCreator, IPXC_Image image, PXC_Rect rect)
		{
			var im = MatrixFromImageRect(rect);
			contentCreator.SaveState();
			contentCreator.ConcatCS(ref im);
			contentCreator.PlaceImage(image);
			contentCreator.RestoreState();
		}

		private PXC_Rect RectFromImageOnPosition(IPXC_Image image, Point position)
		{
			PXC_Rect rect;
			rect.left = position.Y;
			rect.right = image.Width + rect.left;
			rect.bottom = position.X;
			rect.top = image.Height + rect.bottom;
			return rect;
		}

		private PXC_Matrix MatrixFromImageRect(PXC_Rect rect)
		{
			var matrix = new PXC_Matrix();
			matrix.a = rect.right - rect.left;
			matrix.d = rect.top - rect.bottom;
			matrix.e = rect.left;
			matrix.f = rect.bottom;
			return matrix;
		}
		
		private void AddContentToFirstPage(PDFXEdit.IPXV_Document Doc, PDFXEdit.PXV_Inst Inst, PDFXEdit.IPXC_Content content) {

			if (Doc == null)
				return;

			int nID = Inst.Str2ID("op.addContent", false);
			PDFXEdit.IOperation pOp = Inst.CreateOp(nID);
			PDFXEdit.ICabNode input = pOp.Params.Root["Input"];
			input.Add().v = Doc;

			PDFXEdit.ICabNode options = pOp.Params.Root["Options"];
			options["Content"].v = content;
			options["InsertBefore"].v = false;
			options["InsertClone"].v = false;
			options["TargetPage"].v = 0; //First page

			pOp.Do();
		}
	}
Last edited by mkellers on Tue Jan 05, 2016 2:49 pm, edited 1 time in total.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17824
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Place Images

Post by Tracker Supp-Stefan »

Hello mkellers,

Thanks for sharing your solution with the currently available tools!
I am sure it will be useful to other developers too!

Regards,
Stefan
Post Reply