How to get/set caret position?  SOLVED

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
Woodgnome
User
Posts: 27
Joined: Tue Oct 10, 2017 11:25 am

How to get/set caret position?

Post by Woodgnome »

When the text selection tool is enabled it is possible to place a caret in the document view.

Is it possible to get/set the position of this caret?

Just to avoid potential confusion, I'm not talking about text selection here - which means IPXV_TextSelection / IPXV_PageTextSelection aren't applicable (at least as far as I'm aware).
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17941
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: How to get/set caret position?

Post by Tracker Supp-Stefan »

Hello Woodgnome,

Is it this caret that you are referring to:
caret.png
If yes - this is placed when the text selection tool is active, and a click is done over a text area.
Then selection with e.g. Shift + arrow keys is possible.

Regards,
Stefan
Woodgnome
User
Posts: 27
Joined: Tue Oct 10, 2017 11:25 am

Re: How to get/set caret position?

Post by Woodgnome »

Hello Stefan,

Yes, that is the caret I am referring to - but I was hoping for a programmatic approach :D
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get/set caret position?

Post by Sasha - Tracker Dev Team »

Hello Woodgnome,

Please check this topic:
viewtopic.php?f=66&t=28988&p=116014&hil ... on#p116014

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Woodgnome
User
Posts: 27
Joined: Tue Oct 10, 2017 11:25 am

Re: How to get/set caret position?

Post by Woodgnome »

Unfortunately, that does not work with zero-width selection (which is how many application often treat a caret). That's what I meant when I said IPXV_TextSelection / IPXV_PageTextSelection are not applicable.

Here's my sample code if you would like to reproduce:

Code: Select all

public static void SelectText(IPXV_Document doc, uint pageIndex, uint charIndex, uint selectionLength)
{
    int nSel = _pxvInst.Str2ID("selection.text");
    IPXV_TextSelection textSel = (IPXV_TextSelection)doc.CreateStdSel((uint)nSel);
    IPXV_PageTextSelection pts = textSel.GetSel(pageIndex, true);
    pts.SelectChars(charIndex, selectionLength, true);
    textSel.OnAdd(doc);
    textSel.Show(true);
}
Calling SelectText(doc, 0, 1, 1) correctly selects the second character on the first page of the document.
Calling SelectText(doc, 0, 1, 0) does absolutely nothing.

As for getting the caret position, same problem with zero-width selection. Sample code:

Code: Select all

public CaretPosition GetCaretPosition(IPXV_Document doc)
{
    IPXV_DocSelection docSel = doc.ActiveSel;
    if (docSel is IPXV_TextSelection textSel)
    {
      if (textSel.Count > 0)
      {
            IPXV_PageTextSelection pts = textSel[0]; // Get the first text selection cluster
            pts.GetRange(0, out uint charIndex, out uint selectionLength); // Get firstCharIndex of the selction cluster

            return new CaretPosition(pts.PageIndex, charIndex, selectionLength); // charIndex + selectionLength if end of selection is wanted
      }
    }

    return null;
}
If there is a zero-width selection textSel.Count is 0 and thus it's not possible to get the IPXV_PageTextSelection object.

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

Re: How to get/set caret position?

Post by Sasha - Tracker Dev Team »

Hello Woodgnome,

I've forwarded that question to the appropriate developer - he'll look into this when he has time.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Woodgnome
User
Posts: 27
Joined: Tue Oct 10, 2017 11:25 am

Re: How to get/set caret position?

Post by Woodgnome »

Any update on this?

Would be nice to know, even if it's just not possible with the current SDK.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get/set caret position?

Post by Sasha - Tracker Dev Team »

Hello Woodgnome,

There are methods like that, though they are not in the SDK. I have added them to the IPXV_TextSelection interface, though this needs to be approved by our lead dev. Once he says that it's OK - I can give you a dev. build dll for testing. For the reference, I've added SetCaretPos and HideCaret methods, so that you would be able to show and hide the caret if needed.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Woodgnome
User
Posts: 27
Joined: Tue Oct 10, 2017 11:25 am

Re: How to get/set caret position?

Post by Woodgnome »

Sounds good, I'll keep an eye out.

Will there be a GetCaretPos method as well?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get/set caret position?

Post by Sasha - Tracker Dev Team »

Hello Woodgnome,

Yes there also will be a method like that.

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: How to get/set caret position?  SOLVED

Post by Sasha - Tracker Dev Team »

Hello Woodgnome,

I've prepared a dev build for you with these methods (note that dev builds are not stable and should not be used for production):
DevBuild.zip
(20.62 MiB) Downloaded 70 times
Also, here's a code sample on how to use it:

Code: Select all

int nSel = pdfCtl.Inst.Str2ID("selection.text");
IPXV_TextSelection textSel = (IPXV_TextSelection)pdfCtl.Doc.CreateStdSel((uint)nSel);
uint p, l, c = 0;
textSel.GetCaretPos(out p, out l, out c); //all values will be -1
textSel.SetCaretPos(0, 0, 2); //the caret position will be put on 0 page, on 0 line before the 2 character
textSel.GetCaretPos(out p, out l, out c); //this will give 0,0,2 accordingly
textSel.HideCaret();//this will hide the caret
textSel.GetCaretPos(out p, out l, out c);//all values will be -1
pdfCtl.Doc.ActiveSel = textSel;//the active selection will be replaced with new caret pos
textSel.Show(true);

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Woodgnome
User
Posts: 27
Joined: Tue Oct 10, 2017 11:25 am

Re: How to get/set caret position?

Post by Woodgnome »

Just tested this - a few questions:
  1. Is it correctly understood that it is not possible to get caret position ("all values will be -1") unless caret position has been set programmatically first?
    If so, it's sort of handicapped in that I would like to be able to get the caret position at an arbitrary time.
  2. When working with ranges (IPXV_PageTextSelection.GetRange()) these always use character offsets relative to the entire page. Is it possible to have GetCaretPos()/SetCaretPos() do the same as opposed to line + character offset in the line?
Just to elaborate on #2:

It would make more sense that these methods (GetRange() / GetCaretPos() / SetCaretPos()) are consistent with regard to their values/parameters because they are closely related.

Out of the two possibilities (page + character offset / page + line + line character offset) I believe page + character offset is the simpler, because it's hard to define what a line is and how it works (how many lines are there? What is the length of each line? What constitues a new line? A new PDF text command? A line break? Visual position in the document?).

All of those questions would be irrelevant if working with only page + character offset.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get/set caret position?

Post by Sasha - Tracker Dev Team »

Hello Woodgnome,

1) You will have to obtain the existing IPXV_TextSelection, not create new one - then yes, you can get the existing caret position.
2) Well, you would have to write a small conversion method if you need that representation.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Woodgnome
User
Posts: 27
Joined: Tue Oct 10, 2017 11:25 am

Re: How to get/set caret position?

Post by Woodgnome »

1) Ah, I can now call GetCaretPos() on the IPXV_TextSelection I obtain in my GetCaretPosition() code sample - great!

2) Ok, so to confirm:

The line/character indices and counts that GetCaretPos()/SetCaretPos() work with are identical to the ones that can be obtained in:

Code: Select all

var pageText =
doc             // IPXV_Document
.CoreDoc        // IPXC_Document
.Pages[i]       // IPXC_Page
.GetText(...);  // IPXC_PageText

var lineInfo =
pageText        // IPXC_PageText
.LineInfo[j]    // PXC_TextLineInfo
If yes, a few questions for clarification:
  1. What is the difference between IPXC_PageText.CharCount and IPXC_PageText.CharsCount?
  2. When creating a IPXC_GetPageTextOptions for use with IPXC_Page.GetText(...), what is the difference between setting nVersion to 1 or 2 (the explanations in the documentation make no sense to me whatsoever)?
  3. Is the order of IPXC_PageText.LineInfo[lineIndex] the same as when selecting text using the mouse cursor?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get/set caret position?

Post by Sasha - Tracker Dev Team »

Hello Woodgnome,

1) There are several duplicates for that interface - basically the old ones weren't removed because of the policy that someone could have already used them and that could cause some potential errors in project. Basically, some argument types were changed and also the method names - you can use either of them - they all should work correctly.
2) These are two method of the text gathering - first one will be more like the PDF structure is written, the second one will have more visual oriented generation - these can vary from file to file. Basically, you can stick to the second version - should give the needed results for you.
3) Yes, those should be the same lines.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Woodgnome
User
Posts: 27
Joined: Tue Oct 10, 2017 11:25 am

Re: How to get/set caret position?

Post by Woodgnome »

Alright, sounds good.

When can we expect the changes to be released in a production build?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get/set caret position?

Post by Sasha - Tracker Dev Team »

Hello Woodgnome,

If all goes as planned we will hold the release in a few weeks or so.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Woodgnome
User
Posts: 27
Joined: Tue Oct 10, 2017 11:25 am

Re: How to get/set caret position?

Post by Woodgnome »

Cool, looking forward to that :)
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: How to get/set caret position?

Post by Sasha - Tracker Dev Team »

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