Programmatical text selection

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
quietstorm
User
Posts: 25
Joined: Wed Feb 10, 2010 11:37 am

Programmatical text selection

Post by quietstorm »

Hi all, I'm trying to achieve a simple task: set a text selection from code, but I can't find a solution like we had in old PDF XChange Viewer ActiveX SDK.

This is a snippet from old solution (C# .NET)

Code: Select all

public void SelectText(int docID, int page, int start, int length, bool ViewSelection = true, int Flags = 0)
        {
            int[] ar = new int[3];
            object vDataIn = null;
            object vDataOut = null;
            if (_PdfActiveX != null)
            {
                _PdfActiveX.GetActiveDocument(out DocId);
                ar[0] = start;
                ar[1] = length;
                if (ViewSelection)
                    ar[2] = 1;
                else
                    ar[2] = 0;
                vDataIn = ar;
                Execute("Documents[#" + docID + "].Pages[" + page + "].Text", "Select", vDataIn, out vDataOut, Flags);
            }
        }
Execute method is just a helper for error handling

Code: Select all

        public bool Execute(string obj, string cmd, object vDataIn, out object vDataOut, int flag)
        {
            try
            {
                _PdfActiveX.DoVerb(obj, cmd, vDataIn, out vDataOut, flag);
                return true;
            }
            catch (Exception ex)
            {
                LastError = System.Runtime.InteropServices.Marshal.GetHRForException(ex);
                log.Debug("PDF Execute " + cmd + " ERROR");
                vDataOut = null;
                return false;
            }
        }
Is there a way to set a custom selection based on character position start and length?

Another issue we're facing is that the documentation of core-level functions is, in most cases, a mere automated documentation with no clear referrals to COM objects. The most valuable information, in these cases, is variable naming... and that's not much to work with.
Top level editor functions, however, are better documented and accompanied with some code samples.

Is there another documentation source, or developer guide other than online docs?

Thanks in advance,
Fabrizio
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Programmatical text selection

Post by Sasha - Tracker Dev Team »

Hello Fabrizio,

Please try our FullDemo application - it has many things that could prove useful when developing your own application. If you don't find something on the sdkhelp wiki - try searching the Editor SDK / Core API forums - many questions are already answered there. For example check this topic:
https://forum.pdf-xchange.com/ ... 07#p102607
As for your question, you will have to use this method https://sdkhelp.pdf-xchange.com/vie ... ion_GetSel with the bAddIfNotExists flag set to true. This will create new or get an existing text selection on the page that you need. Then you can use this method to select the characters that you need https://sdkhelp.pdf-xchange.com/vie ... electChars

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
pHol
User
Posts: 30
Joined: Mon May 08, 2017 9:47 am

Re: Programmatical text selection

Post by pHol »

Hi!

Is it possible to get an example on how to use the GetSel method in IPXV_TextSelection for programmatical text selection. If there are no selections in the document, how do one get an instance of the IPXV_TextSelection to be able to use the method?

Thanks in advance,
Philip
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Programmatical text selection

Post by Sasha - Tracker Dev Team »

Hello Philip,

First you will have to create a IPXV_TextSelection. To do that use this method with the "selection.text" ID:
https://sdkhelp.pdf-xchange.com/vie ... eateStdSel
Then, when you have the IPXV_DocSelection cast it to the IPXV_TextSelection.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
pHol
User
Posts: 30
Joined: Mon May 08, 2017 9:47 am

Re: Programmatical text selection

Post by pHol »

Hi Alex!

Thanks for the quick response! (don't know what the time is where you are but here in Denmark it is almost 11 AM)

Don't know why I haven't thought of that method before, but it seems to work since I don't get an exception anymore. Now I just have to figure out why nothing is selected, maybe you can help me.
Here is the code I'm trying:

Code: Select all

int nSel = pxvInst.Str2ID("selection.text");
IPXV_TextSelection textSel = (IPXV_TextSelection)pxvInst.ActiveDoc.CreateStdSel((uint)nSel);
IPXV_PageTextSelection pts = textSel.GetSel(0, true);
pts.SelectChars(0, 10);
Thanks,
Philip
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Programmatical text selection

Post by Sasha - Tracker Dev Team »

Hello Philip,

Here's the code that you will need to write to show it:

Code: Select all

int nSel = pdfCtl.Inst.Str2ID("selection.text");
IPXV_TextSelection textSel = pdfCtl.Doc.CreateStdSel((uint)nSel) as IPXV_TextSelection;
IPXV_PageTextSelection pageTextSel = textSel.GetSel(0, true);
pageTextSel.SelectChars(0, 10, true);
textSel.OnAdd(pdfCtl.Doc);
textSel.Show(true);
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
pHol
User
Posts: 30
Joined: Mon May 08, 2017 9:47 am

Re: Programmatical text selection

Post by pHol »

Hi Alex!

I have tested all of that except the OnAdd() method I was missing, have tested the rest. Thank you very much!
However, if I click in the document or manually select some other code, the selection does not seem to deactivate/clear.
Is this type of selection not the same as if one selects by the select tool (manually)?



Cheers,
Philip
Last edited by pHol on Fri Aug 18, 2017 12:46 pm, edited 1 time in total.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Programmatical text selection

Post by Sasha - Tracker Dev Team »

Hello Philip,

To implement that behavior this should be used:

Code: Select all

pageTextSel.SelectChars(0, 10, true);
//textSel.OnAdd(pdfCtl.Doc); //this won't remove the selection with the select text tool
pdfCtl.Doc.ActiveSel = textSel; //this will remove the selection with the select text tool
textSel.Show(true);
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
pHol
User
Posts: 30
Joined: Mon May 08, 2017 9:47 am

Re: Programmatical text selection

Post by pHol »

Hi Alex!

Of course! I should have thought of that, sorry for being stupid!
Thank you very much for all your help!

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

Re: Programmatical text selection

Post by Sasha - Tracker Dev Team »

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