Change text background color

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
alexandrescoelho
User
Posts: 11
Joined: Wed Apr 25, 2018 12:50 pm

Change text background color

Post by alexandrescoelho »

Hi all,

Has anyone tried to change the text background color in the background? When I say background I mean that I don't have any user interface so I'm not using this IPXV_Document.
Most of examples I've seen here need this interface to work and also the operations samples (https://sdkhelp.pdf-xchange.com/vi ... s_setProps)
It's been very painful to understand how to use editor sdk in the background since we have almost nothing in the documentation.
Any help and pieces of codes are very appreciated!

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

Re: Change text background color

Post by Sasha - Tracker Dev Team »

Hello Alexandre,

I don't understand what is:
the text background color in the background
An illustration (with a sample file prefferrably) would be helpful to understand this one.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
alexandrescoelho
User
Posts: 11
Joined: Wed Apr 25, 2018 12:50 pm

Re: Change text background color

Post by alexandrescoelho »

Hi,

what I have meant with backgorund color in the background is to highlight the text without using the IPXV_Document interface
This is what I want to see in the end
d1.png
As you told me couple days ago, I can use Operations to achieve same result in non-UI application
So, after a heavy search on this forum I'm suspecting the key to achieve that result would be using op.document.highlightText.
The documentation regarding this operation is poor, and I couldn't find any post on the forum about it.
Can you please provide an example or sample code presenting how to use it?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Change text background color

Post by Sasha - Tracker Dev Team »

Hello Alexandre,

A little more clarification is needed before we'll get to the coding part - by highlighting you mean you want to add the Highlight comments to the text? Like described here:
https://help.pdf-xchange.com/pdfxe ... ol_ed.html

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
alexandrescoelho
User
Posts: 11
Joined: Wed Apr 25, 2018 12:50 pm

Re: Change text background color

Post by alexandrescoelho »

Hi Alex,

Yes, I want to find few texts and highlight them but keep them higlighted after closing the pdf. The most important thing is that I can't use UI's to my application, there will be no user actions to get the job done while its execution.
As I'm getting closer to my due date and I couldn't produce anything with this sdk, I've been testing everything single piece of code that could help me to understand how to achieve my goal.
I also have tried this https://sdkhelp.pdf-xchange.com/view/PXV:op_search but it didn't work.
If you run the attached code you will check it never hits the line 45 because m_Inst.FindDocByCoreDoc(doc) is always null

Code: Select all

public class SearchCallback : PDFXEdit.IPXV_SearchCallback
{
	// omitted code
	public void OnNewEntry(PDFXEdit.IPXV_SearchEntry pEntry)
	{        
        // omitted code
		vDoc = m_Inst.FindDocByCoreDoc(doc);
		//Now we'll need to check whether the document is opened so that we can highlight it
		if (vDoc != null)
		{
			//This code is never executed because  m_Inst.FindDocByCoreDoc(doc) == null 
			highlighter = vDoc.AddNewHighlighter(PDFXEdit.PXV_DocHighlightType.PXV_DocHighlight_Page);
		}
	}
}	                        
Attachments
SearchAndHighlight.rar
(144.51 KiB) Downloaded 73 times
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Change text background color

Post by Sasha - Tracker Dev Team »

Hello Alexandre,

It seems that you are misinterpreting a search highlight and the highlight annotations. A search highlight ( the AddNewHighlighter method) will add the highlight to a searched results on a UI Control page. This won't be physically saved in the document - just the UI marker of the found elements. What you need is to add the highlight annotations to the needed parts of text - they will be saved and available when reopening the document. I will write you a small sample on how to combine the search operation with the comment insertion.


Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
alexandrescoelho
User
Posts: 11
Joined: Wed Apr 25, 2018 12:50 pm

Re: Change text background color

Post by alexandrescoelho »

Hi Alex,

Thank you very much, this wil be very helpful!
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Change text background color

Post by Sasha - Tracker Dev Team »

Hello Alexandre,

Here's the code sample that searches the document for the needed text and adds highlight annotations to it:

Code: Select all

public class AddHighlightSearchCallback : PDFXEdit.IPXV_SearchCallback
{
	PDFXEdit.IPXV_Inst m_Inst = null;
	public AddHighlightSearchCallback(PDFXEdit.IPXV_Inst Inst)
	{
		m_Inst = Inst;
	}
	public void OnFinish(int nResCode)
	{

	}

	public void OnNewEntry(PDFXEdit.IPXV_SearchEntry pEntry)
	{
		//Highlighting items in the document
		uint i = 0;
		IPXC_Document doc = null;
		IPXC_Page page = null;
		while (i < pEntry.Count)
		{
			IPXV_SearchEntryItem item = pEntry[i];
			IPXV_SearchPtr ptr = item.Ptr;
			for (uint j = 0; j < ptr.Count; j++)
			{
				PXV_SearchPtrChunk chunk = ptr[j];
				//First we get the document
				if (chunk.nType == (uint)PXV_SearchPtrChunkType.PXV_SearchPtrChunk_Document)
				{
					doc = ptr.Doc[chunk.nValue];
				}
				else if (chunk.nType == (uint)PXV_SearchPtrChunkType.PXV_SearchPtrChunk_Page)
				{
					if (doc != null)
						page = doc.Pages[chunk.nValue];
					if (page != null)
					{
						//Now we have the page where the text is found
						for (uint k = 0; k < item.TextRangesCount; k++)
						{
							//Now we need to get the coordinates of the highlight boxes
							IPXC_QuadsF quads = item.GetTextRangeQuads(k);
							PDFXEdit.IPXS_Inst SInt = (PDFXEdit.IPXS_Inst)m_Inst.GetExtension("PXS");
							uint nTextBox = SInt.StrToAtom("Highlight");
							PXC_Rect rc = page.get_Box(PXC_BoxType.PBox_BBox);
							PDFXEdit.IPXC_Annotation pAnnot = page.InsertNewAnnot(nTextBox, ref rc, 0);
							PDFXEdit.IPXC_AnnotData_TextMarkup SQData = (IPXC_AnnotData_TextMarkup)pAnnot.Data;
							SQData.Quads = quads;
							pAnnot.Data = SQData;
							//Executing the operation so that the annotation will be updated from structure
							int nID = m_Inst.Str2ID("op.annots.addNew", false);
							PDFXEdit.IOperation pOp = m_Inst.CreateOp(nID);
							PDFXEdit.ICabNode input = pOp.Params.Root["Input"];
							input.Add().v = pAnnot;
							pOp.Do();
						}
					}

				}
			}
			i++;
		}
	}

	public void OnStart()
	{

	}

	public void OnStartPtr(PDFXEdit.IPXV_SearchPtr pPtr)
	{

	}

	public void OnStopPtr(PDFXEdit.IPXV_SearchPtr pPtr, bool bIncomplete)
	{

	}
}

private void SearchAndHighlightWords(string sText, IPXV_Inst Inst, IPXC_Document Doc)
{
	int nID = Inst.Str2ID("op.search", false);
	PDFXEdit.IOperation Op = Inst.CreateOp(nID);
	PDFXEdit.ICabNode input = Op.Params.Root["Input"];
	input.Add().v = Doc;
	PDFXEdit.ICabNode options = Op.Params.Root["Options"];
	options["AND"].Add().v = sText;
	AddHighlightSearchCallback clbk = new AddHighlightSearchCallback(Inst);
	options["Callback"].v = clbk;
	options["Flags"].v = (int)PDFXEdit.PXV_SearchFlags.PXV_SearchFlag_GetTextQuads | (int)PDFXEdit.PXV_SearchFlags.PXV_SearchFlag_IncludePageText;
	Op.Do();
}
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
alexandrescoelho
User
Posts: 11
Joined: Wed Apr 25, 2018 12:50 pm

Re: Change text background color

Post by alexandrescoelho »

Thanks Alex,

I can't see the result so far because I guess my save logic is not working properly. Does this code should work?

Code: Select all

static void Main(string[] args)
        {
            IPXV_Inst pxvInst = new PXV_Inst();
            IPXC_Inst pxcInst;

            pxvInst = new PDFXEdit.PXV_Inst();
            pxvInst.Init();
            pxcInst = (PDFXEdit.IPXC_Inst)pxvInst.GetExtension("PXC");
            IPXC_Document doc = null;
            var file = @"c:\temp\Lorem Ipsum.pdf";
            doc = pxcInst.OpenDocumentFromFile(file, null);
            SearchAndHighlightWords("Lorem", (PXV_Inst)pxvInst, doc);
            doc.Close(4);
            pxcInst.Finalize();
        }
My second question: How can I add a external url link to the highlighted text, should I use annotation as well?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Change text background color

Post by Sasha - Tracker Dev Team »

You forgot the https://sdkhelp.pdf-xchange.com/vi ... nt_WriteTo method.

As for the second question - you should add the link annotation on top of the highlight annots. For that use the IPXC_AnnotData_Link interface as data.

Cheers,
Alex
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: Change text background color

Post by Sasha - Tracker Dev Team »

Also, do not call the pxcInst.Finalize(); method if you are using the IPXV_Inst. You should call the IPXV_Inst::Shutdown
https://sdkhelp.pdf-xchange.com/view/PXV:PXV_Inst

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
alexandrescoelho
User
Posts: 11
Joined: Wed Apr 25, 2018 12:50 pm

Re: Change text background color

Post by alexandrescoelho »

Thnaks again Alex

The highligh text is finally working.
Regarding the url link I added this code to the callback:

Code: Select all

//...
var nLinkBox = SInt.StrToAtom("Link");
PDFXEdit.IPXC_Annotation pAnnotLink = page.InsertNewAnnot(nLinkBox, ref rc, 0);
PDFXEdit.IPXC_AnnotData_Link SQDataLink = (IPXC_AnnotData_Link)pAnnotLink.Data;
SQDataLink.Contents = "http://www.google.com";
SQDataLink.Quads = quads;
pAnnotLink.Data = SQDataLink;
input.Add().v = pAnnotLink;
//...
This code places a box on the text but it doesn't have the actual url, so when I click over the box nothing happens. Any idea?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Change text background color

Post by Sasha - Tracker Dev Team »

Hello Alexandre,

If you open the End-User Editor or a PDF Specification, you can see that the link annotation has an Action List (like Bookmarks for example). There, you can add all of the actions that you need (in your case that would be the URI action).

Code: Select all

PDFXEdit.IPXC_AnnotData_Link FTData = (PDFXEdit.IPXC_AnnotData_Link)pAnnot.Data;
FTData.Contents = "https://www.google.com";
PDFXEdit.IPXC_ActionsList AL = pdfCtl.Doc.CoreDoc.CreateActionsList();
AL.AddURI("https://www.google.com");
pAnnot.set_Actions(PDFXEdit.PXC_TriggerType.Trigger_Up, AL);
pAnnot.Data = FTData;
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply