Add Link for text selection
Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange
-
- User
- Posts: 10
- Joined: Fri Apr 08, 2011 7:54 am
Add Link for text selection
Hello,
I want to do the following programmatic:
1. find all occurrences of some given text (can be a string containing multiple words)
2. add a link to all these occurrences (e.g. go to a given url)
I can find the text and select it and I am able to add a link to a given region (through javascript addLink). But I can't find the region of the selected text so I don't know where to place the link through javascript.
The AddLink command is actually what I want to implement completely programmatically: on a given selection add a link with some action. How can I do that without user interaction?
And an additional question: when the user clicks on a link, how can I get information about that link (e.g. get the target URL) and do some custom action instead of opening the default browser?
Regards and thanks,
Cantemir
I want to do the following programmatic:
1. find all occurrences of some given text (can be a string containing multiple words)
2. add a link to all these occurrences (e.g. go to a given url)
I can find the text and select it and I am able to add a link to a given region (through javascript addLink). But I can't find the region of the selected text so I don't know where to place the link through javascript.
The AddLink command is actually what I want to implement completely programmatically: on a given selection add a link with some action. How can I do that without user interaction?
And an additional question: when the user clicks on a link, how can I get information about that link (e.g. get the target URL) and do some custom action instead of opening the default browser?
Regards and thanks,
Cantemir
-
- Site Admin
- Posts: 19885
- Joined: Mon Jan 12, 2009 8:07 am
Re: Add Link for text selection
Hello Centemir,
You will need to use our PDF Tools SDK, to analyze the text in your PDF document, get the coordinates of elements that match your search criteria, and add the links at the coordinates which you will obtain from the text elements.
This can be all done without user interaction and without any UI.
As for intercepting link clicks, please have a look at the Viewer AX SDK manual section
2.2.2.15.6 ConfirmOpenSite
Best,
Stefan
You will need to use our PDF Tools SDK, to analyze the text in your PDF document, get the coordinates of elements that match your search criteria, and add the links at the coordinates which you will obtain from the text elements.
This can be all done without user interaction and without any UI.
As for intercepting link clicks, please have a look at the Viewer AX SDK manual section
2.2.2.15.6 ConfirmOpenSite
Best,
Stefan
-
- User
- Posts: 10
- Joined: Fri Apr 08, 2011 7:54 am
Re: Add Link for text selection
Hi,
the hint to ConfirmOpenSite was perfect, thanks.
Could you give me some starting points for the automatic search and link creation too? what functions from the PDF Tools SDK would I use to find the text, get its coordinates and add a link to those coordinates?
Regards,
Cantemir
the hint to ConfirmOpenSite was perfect, thanks.
Could you give me some starting points for the automatic search and link creation too? what functions from the PDF Tools SDK would I use to find the text, get its coordinates and add a link to those coordinates?
Regards,
Cantemir
-
- Site Admin
- Posts: 19885
- Joined: Mon Jan 12, 2009 8:07 am
Re: Add Link for text selection
Hello Cantemir,
I checked with the devs and the proper way to do it would be something along the lines of:
1) Use the PXCp_ET_Prepare and PXCp_ET_AnalyzePageContent functions to get all objects on a page enumerated.
2) Using PXCp_ET_GetElement - browse through the objects you prepared with the Analyze function above, and then check the PXP_TextElement structure for matching strings and their coordinates.
3) Once you have the coordinates use PXCp_AddLink to add your links on the needed locations.
All of the above functions and structures are listed in the PDF Tools Manual under
XCHPRO40 Lib -> Text Extraction and
XCHPRO40 Lib -> Common Structures
Best,
Stefan
I checked with the devs and the proper way to do it would be something along the lines of:
1) Use the PXCp_ET_Prepare and PXCp_ET_AnalyzePageContent functions to get all objects on a page enumerated.
2) Using PXCp_ET_GetElement - browse through the objects you prepared with the Analyze function above, and then check the PXP_TextElement structure for matching strings and their coordinates.
3) Once you have the coordinates use PXCp_AddLink to add your links on the needed locations.
All of the above functions and structures are listed in the PDF Tools Manual under
XCHPRO40 Lib -> Text Extraction and
XCHPRO40 Lib -> Common Structures
Best,
Stefan
-
- User
- Posts: 10
- Joined: Fri Apr 08, 2011 7:54 am
Re: Add Link for text selection
Hello again,
I managed to do everything as you suggested, except the offsets for the characters.
The code is written in C#.
If TextElement.mask contains PTEM_Offsets, then I get an exception: SafeArrayTypeMismatchException. Mismatch has occurred between the runtime type of the array and the sub type recorded in the metadata.
The type of the Offsets member in PXP_TextElement is double[]. Is this correct? Do I have to initialize this member? Does it need a marshaling attribute?
Regards,
Cantemir
I managed to do everything as you suggested, except the offsets for the characters.
The code is written in C#.
If TextElement.mask contains PTEM_Offsets, then I get an exception: SafeArrayTypeMismatchException. Mismatch has occurred between the runtime type of the array and the sub type recorded in the metadata.
The type of the Offsets member in PXP_TextElement is double[]. Is this correct? Do I have to initialize this member? Does it need a marshaling attribute?
Regards,
Cantemir
-
- Site Admin
- Posts: 19885
- Joined: Mon Jan 12, 2009 8:07 am
Re: Add Link for text selection
Hello Cantemir,
We have asked one of the guys in the dev team (Corwin in these forums) to take a look at this case and we should be able to advise further on this later/tomorrow.
Best,
Stefan
We have asked one of the guys in the dev team (Corwin in these forums) to take a look at this case and we should be able to advise further on this later/tomorrow.
Best,
Stefan
-
- User
- Posts: 664
- Joined: Tue Nov 14, 2006 12:23 pm
Re: Add Link for text selection
Hello Cantemir,
Please send us sample project (with source and any supporting files required), so we can reproduce your problem.
Please remove any license keys and zip all files if posting here.
Thanks
Please send us sample project (with source and any supporting files required), so we can reproduce your problem.
Please remove any license keys and zip all files if posting here.
Thanks
-
- Site Admin
- Posts: 2445
- Joined: Thu Jun 30, 2005 4:11 pm
Re: Add Link for text selection
Hi, Cantemir.
Look to attached example. Here is demo - how to obtain positions of selected (and any) text and add link(s) to the selected text.
Also in the new build (196) you will be able to use new simple feature for adding links:
HTH
Look to attached example. Here is demo - how to obtain positions of selected (and any) text and add link(s) to the selected text.
Also in the new build (196) you will be able to use new simple feature for adding links:
Code: Select all
PDFXCviewAxLib.IPDFXCargs args;
object dataOut, dataIn;
...
PDFXCviewAxLib.IPDFXCargs linkParams;
axCoPDFXCview1.DoVerb("", ".ARGS", dataIn, out dataOut, 0);
linkParams = (PDFXCviewAxLib.IPDFXCargs)dataOut;
dataOut = null;
linkParams.Add(nFirstCharIndex);
linkParams.Add(nCountChars);
linkParams.Add("https://www.pdf-xchange.com"); // or zero-based target-page index in this document
dataIn = linkParams;
axCoPDFXCview1.DoDocumentVerb(docID, str, "AddLink", dataIn, out dataOut);
You do not have the required permissions to view the files attached to this post.
PDF-XChange Co. LTD (Project Developer)
Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
-
- User
- Posts: 10
- Joined: Fri Apr 08, 2011 7:54 am
Re: Add Link for text selection
Hello,
thanks for the tip, Vasyl! I can see that your solution it's much easier and it doesn't involve PDF Tools SDK! I will try it out asap!
Regarding my previous question, I managed to get the Offsets by changing the Offsets member in the structure to be IntPtr Offsets and then
Now we have the Offsets alright. Maybe this will help others too.
Regards,
Cantemir
thanks for the tip, Vasyl! I can see that your solution it's much easier and it doesn't involve PDF Tools SDK! I will try it out asap!
Regarding my previous question, I managed to get the Offsets by changing the Offsets member in the structure to be IntPtr Offsets and then
Code: Select all
var offsets = new double[textElement.Count];
Marshal.Copy(textElement.Offsets, offsets, 0, textElement.Count);
Regards,
Cantemir
-
- Site Admin
- Posts: 5223
- Joined: Tue Jun 29, 2004 10:34 am
Re: Add Link for text selection
Excellent news Cantemir and thanks for the info about offsets 

If posting files to this forum - you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded - thank you.
Best regards
Tracker Support
http://www.tracker-software.com
Best regards
Tracker Support
http://www.tracker-software.com
-
- User
- Posts: 12
- Joined: Mon May 09, 2011 10:56 am
Re: Add Link for text selection
Hello,
Is there a way to also get the properties of a link (added to the pdf with the code sample you provided) when i right click it?
For example i have inserted a LinkRectangle with a custom URL format that contains some machine generated parameters, which might not be correct so i want to be able to right click and edit said parameters. I know how to suppress the default context menu but i also found information in these forums that custom context menus are not yet supported
. All i would need is to get and modify the link.Action property of the right clicked link. Is that possible?
Regards and thanks,
Ciprian.
Is there a way to also get the properties of a link (added to the pdf with the code sample you provided) when i right click it?
For example i have inserted a LinkRectangle with a custom URL format that contains some machine generated parameters, which might not be correct so i want to be able to right click and edit said parameters. I know how to suppress the default context menu but i also found information in these forums that custom context menus are not yet supported

Regards and thanks,
Ciprian.
-
- Site Admin
- Posts: 19885
- Joined: Mon Jan 12, 2009 8:07 am
Re: Add Link for text selection
Hello Ciprian,
You are correct that there are still no custom context menus, and as for whether or not you can change the Link.Action with the Viewer SDK - I will now speak with one of our devs and get an answer for you.
Best,
Stefan
You are correct that there are still no custom context menus, and as for whether or not you can change the Link.Action with the Viewer SDK - I will now speak with one of our devs and get an answer for you.
Best,
Stefan
-
- User
- Posts: 12
- Joined: Mon May 09, 2011 10:56 am
Re: Add Link for text selection
Hello,
Sorry to be asking the same question again, but i couldn't find a solution on my own
. Is there no way to get the link i right-click on? Or at least to get it as a text selection so i can alter the link properties?
Thanks & regards,
Ciprian.
Sorry to be asking the same question again, but i couldn't find a solution on my own

Thanks & regards,
Ciprian.
-
- Site Admin
- Posts: 19885
- Joined: Mon Jan 12, 2009 8:07 am
Re: Add Link for text selection
Hello Ciprian,
And apologies for not following up on this one.
I have now requested an update on this question from our dev guys and will make sure to chase it for you and get you an answer as soon as possible.
Best,
Stefan
And apologies for not following up on this one.
I have now requested an update on this question from our dev guys and will make sure to chase it for you and get you an answer as soon as possible.
Best,
Stefan
-
- Site Admin
- Posts: 2445
- Joined: Thu Jun 30, 2005 4:11 pm
Re: Add Link for text selection
Hi, Ciprian.
Best
Regards.
Sorry, no way for this in current implementation. Please wait for new version V3 (under construction).Is there no way to get the link i right-click on? Or at least to get it as a text selection so i can alter the link properties?
Best
Regards.
PDF-XChange Co. LTD (Project Developer)
Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
-
- User
- Posts: 12
- Joined: Mon May 09, 2011 10:56 am
Re: Add Link for text selection
Hi Vasyl,
Thanks for your answer. I'll try to use Javascript & mouse coordinates to achieve this.
Thanks.
Thanks for your answer. I'll try to use Javascript & mouse coordinates to achieve this.
Thanks.
-
- Site Admin
- Posts: 19885
- Joined: Mon Jan 12, 2009 8:07 am