How to Replace the image of a IPXC_ContentItem  SOLVED

PDF-XChange Editor SDK for Developers

Moderators: Tracker Support, TrackerSupp-Daniel, Paul - Tracker Supp, Chris - Tracker Supp, Vasyl-Tracker Dev Team, Sean - Tracker, Tracker Supp-Stefan, Ivan - Tracker Software

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: 854
Joined: Wed Sep 30, 2009 6:53 pm

How to Replace the image of a IPXC_ContentItem

Post by jeffp » Fri Mar 24, 2017 6:52 pm

I'm writing a clean up routine that will pull out the images inside a PDF page, clean them up with an outside image clean up routine (things like despeckle, deskew, etc.), and then replace the original image with the cleaned up image.

In looking at IPXC_ContentItem, it can get the images by calling:

AItem.Image_CreateIXCPage(False, 0, ixcPage);

Then I can clean up the ixcPage to my liking.

However, I can't seem to find any calls associated with IPXC_ContentItem that will allow me to replace the image with the cleaned up image keeping everything thing else about the IPXC_ContentItem like positiioning, etc., the same.

Can you point me in the right direction?

Thanks.

--Jeff

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Sat Mar 25, 2017 7:39 am

Hello Jeff,

This task is not that trivial at all, as you may see it. The good news is that we are developing a feature, that would do precisely what you need - the Edit image feature. From the developer point of view, the operation will be available, that would take the file with the image and the selected content item that should be replaced. Also, as I see it now, we probably will have to make the export image operation, that would allow to save the image from the content item into a file. Though that one is also not so trivial - a small example - rotated image in a rotated XForm on a page that has been rotated.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Mon Mar 27, 2017 9:25 am

Hello Jeff,

On the second thought, that code that you have will work, though you will modify the image as it is inside of the PDF (meaning not visually). If you need to despeckle, deskew the image then probably this will work just fine.
After you have modified the image, you can update the content item by using the Image's handle:
http://sdkhelp.tracker-software.com/vie ... age_Handle
You can try that now - it should work.
If you will need to see the image as you see it in the PDF, modify it and then save it into the content item - then you will need to wait for our operations. Though from what I see - you won't need this.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Wed Mar 29, 2017 4:44 pm

Ok. I'm trying to follow your suggestion but I can't figure out how to set the image handle of the content item using the IIXC_Page I rotated.

So, the following code seems to get and rotate the IIXC_Page from the content item. I just need some help is resetting the content item image.

Also, do I need to use CAccessMode_FullClone with IPXC_Page.GetContent since I am editing the content?

See code below.

Thanks.

Code: Select all

procedure RotatePDFImages(APage: IPXC_Page);
var
  i, AIndex: Integer;
  AItemsCount, AHandle: Cardinal;
  AContent: IPXC_Content;
  AItems: IPXC_ContentItems;
  AItem: IPXC_ContentItem;
  AType: PXC_CIType;
  ixcPage: IIXC_Page;
  AImage: IPXC_Image;
begin
  APage.GetContent(CAccessMode_Readonly, AContent); //Does this need to be CAccessMode_FullClone?
  AContent.Get_Items(AItems);
  AItems.Get_Count(AItemsCount);
  for i := 0 to AItemsCount - 1  do
  begin
    AItems.Get_Item(i, AItem);
    AItem.Get_type_(AType);
    if (AType = CIT_Image) or (AType = CIT_InlineImage) then
    begin
      AItem.Image_CreateIXCPage(False, 0, ixcPage);
      if Assigned(ixcPage) then
      begin
        ixcPage.Rotate(Rotate_180);
      end;
      //How do I get the Handle to ixcPage??
      //AHandle := ????
      AItem.Set_Image_Handle(AHandle);
    end;
    AItem := nil;
  end;
end;

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Thu Mar 30, 2017 9:09 am

Hello Jeff,

WeakClone is enough, though you will need to use the GetItemForEditing not the Get_Item method of the IPXC_ContentItems interface.
Also, to get the handle - you will need to use this method:
http://sdkhelp.tracker-software.com/vie ... romIXCPage
This will give you the IPXC_Image - from it you can take the property Handle and modify the original item's handle with it. http://sdkhelp.tracker-software.com/vie ... age_Handle

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Thu Mar 30, 2017 12:41 pm

I'm not following how you want me to use

IPXC_Document::AddImageFromIXCPage

In my code sample, I can get the IIXC_Page from the Image Item and I can rotate it.

Using IPXC_Document::AddImageFromIXCPage would get me a new IPXC_Image from the IIXC_Page, but then what? I'm not trying to add a new IPXC_Image, I'm just trying to edit an existing one. If I assign the handle of the new IPXC_Image to the old Image Item, will that replace it?

Perhaps you can complete my Delphi example to show me what you mean.

Thanks.

--Jeff

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Thu Mar 30, 2017 12:56 pm

Hello Jeff,

You will need to create a new IPXC_Image from the IIXC_Page and then use the handle of the new IPXC_Image to replace the handle of the old IPXC_Content item.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Thu Mar 30, 2017 2:01 pm

Here's my code on with your suggestions, but it's still not working. I must still be missing something.

Code: Select all

procedure RotatePDFImages(APage: IPXC_Page);
var
  i, AIndex: Integer;
  AItemsCount, AHandle, W: Cardinal;
  AContent: IPXC_Content;
  AItems: IPXC_ContentItems;
  AItem: IPXC_ContentItem;
  AType: PXC_CIType;
  ixcPage: IIXC_Page;
  AImage: IPXC_Image;
  ADoc: IPXC_Document;
begin
  APage.GetContent(CAccessMode_WeakClone, AContent);
  AContent.Get_Items(AItems);
  AItems.Get_Count(AItemsCount);
  for i := 0 to AItemsCount - 1  do
  begin
    AItems.GetItemForEditing(i, AItem);
    AItem.Get_type_(AType);
    if (AType = CIT_Image) or (AType = CIT_InlineImage) then
    begin
      AItem.Image_CreateIXCPage(False, 0, ixcPage);

      if Assigned(ixcPage) then
      begin
        ixcPage.Rotate(Rotate_180);
      end;

      APage.Get_Document(ADoc);
      AImage := ADoc.AddImageFromIXCPage(ixcPage, 0);

      AImage.Get_Handle(AHandle);
      AItem.Set_Image_Handle(AHandle);
    end;
    AItem := nil;
  end;
end;

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Thu Mar 30, 2017 2:08 pm

Hello Jeff,

You forgot to set the content that you have modified. Use this method:
http://sdkhelp.tracker-software.com/vie ... aceContent
With the PlaceContent_Replace flag.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem  SOLVED

Post by jeffp » Thu Mar 30, 2017 4:18 pm

Perfect.That did it. Many thanks on this one.

Here's the complete code:

Code: Select all

procedure RotatePDFImages(APage: IPXC_Page);
var
  i, AIndex: Integer;
  AItemsCount, AHandle, W: Cardinal;
  AContent: IPXC_Content;
  AItems: IPXC_ContentItems;
  AItem: IPXC_ContentItem;
  AType: PXC_CIType;
  ixcPage: IIXC_Page;
  AImage: IPXC_Image;
  ADoc: IPXC_Document;
begin
  APage.GetContent(CAccessMode_WeakClone, AContent);
  AContent.Get_Items(AItems);
  AItems.Get_Count(AItemsCount);
  for i := 0 to AItemsCount - 1  do
  begin
    AItems.GetItemForEditing(i, AItem);
    AItem.Get_type_(AType);
    if (AType = CIT_Image) or (AType = CIT_InlineImage) then
    begin
      AItem.Image_CreateIXCPage(False, 0, ixcPage);

      if Assigned(ixcPage) then
      begin
        ixcPage.Rotate(Rotate_180);
      end;

      APage.Get_Document(ADoc);
      AImage := ADoc.AddImageFromIXCPage(ixcPage, 0);

      AImage.Get_Handle(AHandle);
      AItem.Set_Image_Handle(AHandle);
      
      APage.PlaceContent(AContent, PlaceContent_Replace); //This was the missing call
      
      AImage := nil;
      ixcPage := nil;
    end;
    AItem := nil;
  end;
end;

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Thu Mar 30, 2017 5:30 pm

:)
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Fri Apr 07, 2017 8:40 pm

I've got two follow ups on this code:

[1]
This code now allow me to grab an image out of the PDF page change it and then replace it. However, this is all being done at the Core level. I need to also run this code on a PDF that I have open in the Editor. In that case, I'm dealing with a IPXV_Document and then get the core from it. The issue is that if I modify the image on a page at the core level with the code in this thread, the IPXV_Document visible in the Editor does not refresh itself to show the changed image on the page.

Is there a way to do this?

[2]
I think I've found an issue/bug in your Image_CreateIXCPage. Specifically, this line for the routing in this thread.

Code: Select all

AItem.Image_CreateIXCPage(False, 0, ixcPage);
If you take the attached zip file and look at the PDF file. There are two images contained on the PDF page. When I call Image_CreateIXCPage on the first image and the save it to file it is fine. See Image1.tif in the attached zip file.

However, take a look at what happens when I call .Image_CreateIXCPage on the second image in the PDF page and then save it out. See Image2.tiff in the attached .zip file. The image gets distorted.

I'm hoping you can help me find out what's going on. Again, I'm just finding the images on the PDF page, turning them into a IXC_Page, and then saving that page to a .tif file on disk.

I've already tried using all the PXC_RenderingIntent enum for the second paramter of Image_CreateIXCPage. Nothing changes.
Test.zip
(1.32 MiB) Downloaded 62 times
--Jeff

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Mon Apr 10, 2017 12:08 pm

Hello Jeff,

Remember my post where I was talking about non-triviality of these operations. One of the most important things is to give a correct matrix when editing the image and also when replacing the resulting image Content Item. That is why you have wrong results - because you have specified a wrong transformation matrix. You will have to calculate the transformation matrix by yourself.
I suggest waiting for the next release and use the export/replace operations - then all of your problems will disappear. Probably also we can arrange a custom dev. build in a couple of days so you can try these operations before the release.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Mon Apr 10, 2017 12:37 pm

I'd be happy to try out the export/replace operations in an early build.

But in the meantime, I really need to learn how to correctly calculate the transformation matrix. Can you point me the right direction? What I don't understand is that all I'm doing is finding the Item and then calling

AItem.Image_CreateIXCPage(False, 0, ixcPage);

To create the IXC_Page. Then I'm just saving the page to file.

Where would the tranformation matrix be calculated here? On the Item? Once I have the IXC_Page?

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Mon Apr 10, 2017 12:53 pm

Basically what you will need to do is to get the Current Transformation Matrix of the Content Item:
http://sdkhelp.tracker-software.com/vie ... tem_GetCTM
And then modify the IIXC_Page with it's values (translate, rotate, scale etc). Then you will have the picture as you see it.
You can read the PDF Specification to understand the matrices more:
8.3 Coordinate systems
8.9 Images

But also, you will need to calculate the reverse matrix, using which you can paste the image correctly inside of the content item.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Mon Apr 10, 2017 12:59 pm

I'll give this a try.

But you say your new export/rename operations here will do this for me?

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Mon Apr 10, 2017 1:01 pm

Yup - basically you will spend unneeded time for this.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Mon Apr 10, 2017 1:38 pm

Ok. But I do want to better understand this so I'm going to take a stab at it. I'm transforming matrices in other places in my code.

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Mon Apr 10, 2017 1:49 pm

:)
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Mon Apr 10, 2017 1:58 pm

Ok.

AItem.GetCTM(ctm)

ctm produces this

a=1003
b=0
c=0
d=610
e=0
f=0

Looks like it is just telling me the Width and Height in points of the item.

How does the above tell me how to modify the IIXC_Page with it's values (translate, rotate, scale etc)? And which IIXC_Page calls would I use?

Don't mean to be a pain, just looking to understand this stuff, and I've got a beta release of my own to get out this week.Plus, I'll need this on the Core level for some of the things I do.

--Jeff

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Mon Apr 10, 2017 2:41 pm

Hello Jeff,

Please read the PDF Specification first, analyze it and think whether all of this is worth the time. I say wait for a couple of days and we will publish a Dev Build for you.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Mon Apr 10, 2017 2:59 pm

Ok. But will the dev build allow me to grab the image from the content item at the Core level like I am doing now? Or will it be an operation at the Editor level? I need it at the core level since I'll be calling this only at the IPXC_Document level and in many cases I would not have initialized the Editor but called the DLL directly.

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Tue Apr 11, 2017 12:10 pm

Hello Jeff,

You will need an IPXV_Document for this one - the input parameter is IPXV_ContentItemEntry that can be obtained only from the selection. Here's the sample:
http://sdkhelp.tracker-software.com/vie ... tItemEntry
We can't pass the IPXC_ContentItem as an input item because it will only work in simple cases. The IPXV_ContentItemEntry has all the information that we will need for correct operation work. I'll describe a simple example where the trouble hides. Take a XForm with the image in it. Then copy that XForm several times and rotate it. If you would modify the content item that is inside the XForm it would result in all XForms modification not just a single one that you thought you were modifying. If the IPXV_ContentItemEntry is passed that we have an entire way from top to bottom of what and where we should modify. Thus the IPXV_ContentItemEntry is the only correct way of dealing with the content items if we are talking about the Editor Level. On the Core level you have to think all of this logic by yourself.
Basically to use these new operation you will have to open the document in the Editor Level (whether the Control will be hidden or not does not matter) and create a needed IPXV_ContentItemEntry.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Tue Apr 11, 2017 12:44 pm

I would really prefer to stay at the Core level on this one. My sample code in this post works in almost all cases.

Could I please ask you to give me some more help by showing me how to manipulate the item's CTM and subsequent IIXC_Page so the my code will work in all cases.

I've got a new edition of our product I'm releasing and this is the only item that is holding me up. We have added a new cleanup routine that pulls out all the images in a PDF document, cleans then up, and puts them back replacing the original.

-Jeff

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Tue Apr 11, 2017 12:52 pm

Hello Jeff,

The thing is that this task is not trivial at all - the code that does this is massive and is scattered throughout the project. So sadly, we can't help on this matter because this is a very time consuming task - telling someone how our rendering works (with details). Basically the only correct way would be to use the operations and forget about all of the problems and save tons of your time that you will spend implementing a correct rendering from your side.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Tue Apr 11, 2017 12:57 pm

Ok. I'll give the operations a try. When will the new dev build be ready that contains the new calls to do this?

In the meantime, I'll study and get setup what I can the IPXV_ContentItemEntry example you just posted.

--Jeff

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Tue Apr 11, 2017 1:02 pm

Hello Jeff,

Just launched the deploy procedure - the build itself should be available in a couple of hours - I will notify you when it's done.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Tue Apr 11, 2017 1:42 pm

Great. Can you provide me with a small code snippet to illustrate the new calls.

Again, I need to be able to get each image in a format I can work with like IIXC_Page, modify it, and then put it back.

Thanks again.

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Tue Apr 11, 2017 3:28 pm

Hello Jeff,

I will prepare samples and will reply with the results.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Tue Apr 11, 2017 6:31 pm

Sounds good, but in looking again at the problem PDF file I sent over, it still may be an issue with the PDF. I opened that PDF and then printed it to PDF using your PDF Printer driver. Then I did my clean up routine on it and the images are fine, no distortions at.

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Wed Apr 12, 2017 9:47 am

Hello Jeff,

When you printed the PDF you've "killed" all of the editable text on pages and converted the images that resulted in the quality change. When you've done this the matrices were also negated and recalculated so the image appearance in the PDF became like you see it visually. Shortly speaking, this way is not correct.
The good news is that the Dev. Build is out and available to download from here:
http://www.tracker-software.com/forum3/ ... 66&t=25943
Also, I've prepared a full sample on the two operations and also a sample on how to form the IPXV_ContentItemEntry correctly.

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];
		//For example we will replace first image content item (it can be withing XForm)
		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 ReplaceImage(PDFXEdit.IPXV_Document Doc, PDFXEdit.IPXV_Inst Inst)
{
	int nID = Inst.Str2ID("op.document.replaceImage", 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"];
	int nSelID = Inst.Str2ID("selection.contentItems", false);
	uint nPage = 0; //Page number that will have it's image content item edited
	//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;
	//Adding selected page's root entry with added image content item to the operation
	options["Entry"].v = itEntry; //If it's used in pair with the op.document.extractImage the original IPXV_ContentItemEntry should be used
	//Input image file
	PDFXEdit.IAFS_Inst fsInst = (PDFXEdit.IAFS_Inst)pdfCtl.Inst.GetExtension("AFS");
	PDFXEdit.IAFS_Name name = fsInst.DefaultFileSys.StringToName(@"D:\TestImage_res.png");
	//Src can be of the next types: IAFS_File, IAFS_Name, IString, IStream, IIXC_Image, IIXC_Page
	options["Src"].v = name;
	//First page of the image (in case of multipage images)
	options["SrcFrameIndex"].v = 0;
	//Setting whether the reversed matrix should be used for correct image resulting appearance
	//If the image was extracted visually then the UseReverseMatrix = true must be supplied for the reverse op.document.replaceImage operation
	options["UseReverseMatrix"].v = true;
	//Replacing image content item
	Op.Do();
	//Clearing the selection
	itSel.Clear();
}

Code: Select all

private void ExportImage(PDFXEdit.IPXV_Document Doc, PDFXEdit.IPXV_Inst Inst)
{
	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"];
	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;
	//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:\TestImage_res.png";
	//Setting whether the image should be extracted visually or as it is in the PDF content
	//If the image was extracted visually then the UseReverseMatrix = true must be supplied for the reverse op.document.replaceImage operation
	options["ExtractVisually"].v = true;
	//Extracting image content item
	Op.Do();
	//Clearing the selection
	itSel.Clear();
}
Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Wed Apr 12, 2017 3:24 pm

Ok. I've implemented your code. However, there must be an issue with the PDF sample file I sent over earlier on this thread because the second image contained on the page still comes out all distorted, even with this new code.

Can you try running your code on the PDF sample I sent over and see what results you get when you extract the two images on the first page of the PDF. I'll attached it here again.

By the way, that sample PDF was produces by an HP scanner.
Test.zip
(1.32 MiB) Downloaded 55 times

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Thu Apr 13, 2017 10:11 am

Hello Jeff,

Please try using latest dev build with my original unmodified code. I just tried it on your file - everything worked correctly. Also, I am saving the image as PNG, not tiff, so try my original code with your file.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Thu Apr 13, 2017 1:33 pm

Is PNG the only output option?

But even so, there is still something wrong with that second image even in PNG format. Take the PNG and load it into a IXC_Page and save it out as JPG or BMP and it will turn black.

Also, load the PNG in IrfanView and it will display as black.

--Jeff

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Thu Apr 13, 2017 1:56 pm

Well that image has transparency and you saved it without the transparency thus the black image result.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Thu Apr 13, 2017 3:39 pm

How do I save it with transparency?

Also, is it possible to save it out of your code as a TIF or something other than PNG?

I ultimately need to get that embedded image saved as a TIF file without any distortions.

Alternatively, can you give me a way to detect if the embedded image has a transparency and I'll just skip that image from my clean up routine.

Also, does this new dev build have that new image Editor feature you mentioned. If so, how do I access it in the Editor UI?

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Fri Apr 14, 2017 9:55 am

Hello Jeff,

1) IrfanView does open our png as a transparent one - you have the background set as black in your settings - you will need to change that:
Image
2) IrfanView does not correctly support tiff with transparent backgrounds. I converted the PNG file that was generated by the operation with our IXC library and opened in several popular programs that support transparent tiff - everything went smoothly. Basically you can do the conversion by yourself. You will need to use the ImagesTest utility and convert the resulting PNG file into TIFF. Try opening the PNG with the ImagesTest utility and save it to tiff. There you can get the needed parameters. The sample and the program inself can be found here:
http://www.tracker-software.com/forum3/ ... 66&t=25943
3) We will not change the output format of the operation because then we would need to answer questions "Where is my transparency?" when saving for example into JPEG.
4) To check whether the IIXC_Page has transparency - use the Format property:
http://sdkhelp.tracker-software.com/vie ... age_Format
If it is AlphaGray or ARGB - then it contains the Alpha channel (transparency).
5) In your case it's probably better to modify the images as they are in PDF structure not visually. For that you will need to set options["UseReverseMatrix"].v = false; and options["ExtractVisually"].v = false; in both operations.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Fri Apr 14, 2017 12:50 pm

If I load the extracted PNG from your new code into a IIXC_Page the Format property does in fact show ARGB.

Can you make is so the IIXC_Page created from (cored level)

AItem.Image_CreateIXCPage(False, 0, ixcPage);

Shows the same. Currenlty, it shows just RGB.

This way I can know to skip these images for my clean up routine.

Thanks for all your help on this one. I know it was a pain.

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Fri Apr 14, 2017 1:13 pm

Hello Jeff,

Try setting options["UseReverseMatrix"].v = false; and options["ExtractVisually"].v = false; in both operations and compare the results with your Core code. This should give you the same results. If you are setting these flags as true then the images always will convert to ARGB if not - the image will appear as it is in PDF (transparent or not). Also, if you have images of mask types - when editing them like you are doing on the Core level, the result will probably appear different from what you see in the PDF.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Fri Apr 14, 2017 2:17 pm

Ok. I follow you.

So at the core level by default the quivalent of options["UseReverseMatrix"].v = false; and options["ExtractVisually"].v = false are set. That's why the ixcPage.Format below shows RGB and not ARGB.

AItem.Image_CreateIXCPage(False, 0, ixcPage);

I guess what I'm wondering is if there is any way to set those options to true in the Core code so that ixcPage.Format would show ARGB?

Do you follow me?

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Fri Apr 14, 2017 2:34 pm

jeffp wrote:I guess what I'm wondering is if there is any way to set those options to true in the Core code so that ixcPage.Format would show ARGB?
There are no options on the Core Level - these are the options of the Operations that are being used at the Editor's Level.
To sum up this topic:
1) If you are using the operations with those flags set to true - that will give you a visual representation of the image content item (as you see it rendered on the end-user Editor's page). This is being done by our inner rendering code that is vast and complex. In this case you will get a Transparent image as a result.
2) If you are using the operations with those flags set to false (which relates to your Core Level code that basically should give the same results) - that will give you an image as it is in PDF Structure in many cases not as you see it rendered on the end-user Editor's page. In this case you will get images with or without transparency depending on the image content item type (for example Mask types are Transparent).
3) If you want to achieve visual representation with Core Level code alone and always have transparent images as a result - do as being written in 1) or implement all of the logic for yourself.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Fri Apr 14, 2017 4:05 pm

You alluded to the masks in your last thread.

Does that mean there is a way to analyze the Image Item's mask to determine if I have a transparency situation or not?

AItem.Get_Image_Handle(AImageHandle);
AItem.Get_Image_Object(AImage);
AImage.Get_MaskHandle(AMaskHandle);

Also, this seem to hold true, but I really have no idea what an EState is.

AContent.GetEStateByIndex(i, ES);
if Assigned(ES) then
begin
MsgBox('No Tranparency');
end else
begin
MsgBox('Has Tranparency');
end;

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Tue Apr 18, 2017 6:07 am

Hello Jeff,
Does that mean there is a way to analyze the Image Item's mask to determine if I have a transparency situation or not?
No, the transparency can be set differently - you will need to take the original image and look at it's format - that's the only way.
Also, this seem to hold true, but I really have no idea what an EState is.
This is the PDF Specification part - you will have to read that information there.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Wed May 03, 2017 3:14 am

I believe you mentioned above, or in one of my other threads, that the build was going to have a new Image Editor to edit an embedded image in the PDF.

Is this still the case? And when will build .322 be released? Is there a dev build with this there for me to test out?

Thanks.

--Jeff

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Wed May 03, 2017 5:45 am

Hello Jeff,

I did mention the Edit Image feature, though the editing will be held in the 3rd party application (MS Paint for example). Also, we plan to release the new build in a week or so.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Wed May 03, 2017 12:41 pm

So how will that Edit image feature work? Will you press a button and then the image will be extracted and opened in MS Paint? Then how does it get inserted back into the PDF once the image editing is done?

--Jeff

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Wed May 03, 2017 1:09 pm

Hello Jeff,

The modification will be held in any 3rd party program - that would be up for the user to choose from. It uses those two operation that I mentioned before. It listens to the file modifications and when you make some changes it will apply them to the edited content item.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Wed May 03, 2017 1:54 pm

I look forward to trying it out.

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

Re: How to Replace the image of a IPXC_ContentItem

Post by Sasha - Tracker Dev Team » Wed May 03, 2017 2:03 pm

Hello Jeff,

It's already included in the dev. build that you have. See this video for further usage:
https://www.youtube.com/watch?v=MG_M71vCZZ8

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

jeffp
User
Posts: 854
Joined: Wed Sep 30, 2009 6:53 pm

Re: How to Replace the image of a IPXC_ContentItem

Post by jeffp » Wed May 03, 2017 4:09 pm

Ok. Now you really have me excited.

You say next week or so, do you have a more specific date than that?

Post Reply