No annotation name with PXCp_AddLineAnnotationW?

This Forum is for the use of Software Developers requiring help and assistance for Tracker Software's PDF-Tools SDK of Library DLL functions(only) - Please use the PDF-XChange Drivers API SDK Forum for assistance with all PDF Print Driver related topics or PDF-XChange Viewer SDK if appropriate.

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

Post Reply
relapse
User
Posts: 167
Joined: Wed Jan 18, 2012 11:10 am

No annotation name with PXCp_AddLineAnnotationW?

Post by relapse »

Hello!

I couldn't find any possibility to assign a unique name for an annotation creating it with the PXCp_AddLineAnnotationW function. In the PDF reference (the table 8.15 "Entries common to all annotation dictionaries") that unique name is notified as the "NM" key. I need that name in order to delete only the certain annotations in the "Annots" dictionary of a page. At the moment I do it by deleting all annotations, but it's not precise enough.

I have a suspicion, it's possible only while creating an annotation directly in the "Annots" dictionary.


Thanks for your reply!
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17893
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by Tracker Supp-Stefan »

Hi Relapse,

PXCp_AddLineAnnotationW does not currently generate a /NM entry in the dictionary when creating an annotation. Ivan said that he could add generation of the name in the next build - but it will not be something that you will be able to control, but rather a random name generated on the fly.

We will need to know how you are planning to delete those annotations after that so that we can decide whether such a change in the PXCp_AddLineAnnotationW method would be of any benefit to you.

Best,
Stefan
relapse
User
Posts: 167
Joined: Wed Jan 18, 2012 11:10 am

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by relapse »

Hello, sorry for my silence, I had to fix some bugs yesterday and today.

Here is my code where I try to delete all annotations within a PDF:

Code: Select all

            int _handle;
            PdfXchangePro.PXCp_Init(out _handle, PdfXchangePro.SerialNumber, PdfXchangePro.DevelopmentCode);
            PdfXchangePro.PXCp_ReadDocumentW(_handle, _tempFile, 0);

            int objectsCount;
            PdfXchangePro.PXCp_llGetObjectsCount (_handle, out objectsCount);
            for (var i = 0; i < objectsCount; i++)
            {
                int objekt;
                PdfXchangePro.PXCp_llGetObjectByIndex(_handle, i, out objekt);
                int dictionary;
                PdfXchangePro.PXCp_ObjectGetDictionary(objekt, out dictionary);
                int dictionaryItemsCount;
                PdfXchangePro.PXCp_DictionaryGetCount(dictionary, out dictionaryItemsCount);
                if (dictionaryItemsCount == 0) continue;

                for (var j = 0; j < dictionaryItemsCount; j++)
                {
                    var itemKeyHandle = PdfXchangePro.PXCp_StringCreate();
                    int itemValueHandle;
                    PdfXchangePro.PXCp_DictionaryGetPair(dictionary, j, itemKeyHandle, out itemValueHandle);

                    int bufferLength;
                    PdfXchangePro.PXCp_StringGetB(itemKeyHandle, null, out bufferLength);

                    if (bufferLength == 0) continue;
                    var bufferItemKeyName = new byte[bufferLength];
                    PdfXchangePro.PXCp_StringGetB(itemKeyHandle, bufferItemKeyName, out bufferLength);
                    var itemKeyName = Encoding.ASCII.GetString(bufferItemKeyName);

                    if (itemKeyName.Equals("Annots"))
                    {
                        PdfXchangePro.PXCp_DictionaryDelete(itemValueHandle);
                        PdfXchangePro.PXCp_WriteDocumentW(_handle, _tempFile, PdfXchangePro.PXCp_CreationDisposition.PXCp_CreationDisposition_Overwrite,
                                                (int)PdfXchangePro.PXCp_WriteDocFlag.PXCp_Write_Release);
                        _handle = 0;
                    }
                    var retVal2 = PdfXchangePro.PXCp_StringDelete(itemKeyHandle);
                }                
            }
            if (_handle != 0)                
                PdfXchangePro.PXCp_Delete(_handle);
After executing that passage there is no border created with four calls of the PXCp_AddLineAnnotationW function (for four lines). The problem I've just noticed - I cannot add any new line annotation to the document after the upper code has been executed. I suppose I delete a false dictionary element.

What I plan to do is to go one level deeper and then retrieve the value of the "NM" key. And here I'm missing the possibility to assign that key with a certain name (no random name!) in order to distinguish the line annotations used as a border from other annotations.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17893
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by Tracker Supp-Stefan »

Hi relapse,

I will speak with Ivan again, but I am pretty sure that the only way we would consider to implement the addition of an /NM entry in an annotation's dictionary when it's created with PXCp_AddLineAnnotationW would be a random on-the-fly name, and your only option would be to use the low level API if you want to generate the border annotation with a name you can then recognize in your code later.

Best,
Stefan
relapse
User
Posts: 167
Joined: Wed Jan 18, 2012 11:10 am

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by relapse »

What is the reason for creating a random name and not a certain one?
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3549
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by Ivan - Tracker Software »

First of all, in your code there is mistake - it removes first /Annots entry, saves the document and closes it.

To remove all annots you have to modify it a little ....

Code: Select all

            int _handle;
            PdfXchangePro.PXCp_Init(out _handle, PdfXchangePro.SerialNumber, PdfXchangePro.DevelopmentCode);
            PdfXchangePro.PXCp_ReadDocumentW(_handle, _tempFile, 0);

            int objectsCount;
            PdfXchangePro.PXCp_llGetObjectsCount (_handle, out objectsCount);
            for (var i = 0; i < objectsCount; i++)
            {
                int objekt;
                PdfXchangePro.PXCp_llGetObjectByIndex(_handle, i, out objekt);
                int dictionary;
                PdfXchangePro.PXCp_ObjectGetDictionary(objekt, out dictionary);
                int dictionaryItemsCount;
                PdfXchangePro.PXCp_DictionaryGetCount(dictionary, out dictionaryItemsCount);
                if (dictionaryItemsCount == 0) continue;

                for (var j = 0; j < dictionaryItemsCount; j++)
                {
                    var itemKeyHandle = PdfXchangePro.PXCp_StringCreate();
                    int itemValueHandle;
                    PdfXchangePro.PXCp_DictionaryGetPair(dictionary, j, itemKeyHandle, out itemValueHandle);

                    int bufferLength;
                    PdfXchangePro.PXCp_StringGetB(itemKeyHandle, null, out bufferLength);

                    if (bufferLength == 0) continue;
                    var bufferItemKeyName = new byte[bufferLength];
                    PdfXchangePro.PXCp_StringGetB(itemKeyHandle, bufferItemKeyName, out bufferLength);
                    var itemKeyName = Encoding.ASCII.GetString(bufferItemKeyName);

                    if (itemKeyName.Equals("Annots"))
                    {
                        PdfXchangePro.PXCp_DictionaryDelete(itemValueHandle);
                        break; // for sure there will not be another /Annots in this dictionary, so, we can skip it
                    }
                    var retVal2 = PdfXchangePro.PXCp_StringDelete(itemKeyHandle);
                }               
            }
            // if you want to continue work with this document, you have to leave it open
            PdfXchangePro.PXCp_WriteDocumentW(_handle, _tempFile, PdfXchangePro.PXCp_CreationDisposition.PXCp_CreationDisposition_Overwrite,
                       (int)PdfXchangePro.PXCp_WriteDocFlag.PXCp_Write_Release);
            _handle = 0;
            if (_handle != 0)               
                PdfXchangePro.PXCp_Delete(_handle);
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3549
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by Ivan - Tracker Software »

Why can you not use : /T (szTitle parameter) or /Contents (szAnnot) entry for you 'special' line annotation ?
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
relapse
User
Posts: 167
Joined: Wed Jan 18, 2012 11:10 am

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by relapse »

Thanks for your proposal of code modification, but it worked also this way, the only issue is that I cannot add any new annotation after I've deleted them all one time :) I get an error each time I do it, that's why I was busy yesterday with integrating an error interpretation. But surely I'll also try your variant.

I've also already thought of the option you mentioned - to store a unique identifier in the title or in content, but actually I don't need that little pop-up window displayed after a double click on an annotation. It's my next question: can I suppress creating that window?

Thanks for your feedback!
relapse
User
Posts: 167
Joined: Wed Jan 18, 2012 11:10 am

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by relapse »

I guess I'm working with false objects. I've read the PDF reference one more time 8) and I've asserted that the item with the key "Annots" in the object catalog has value of an array filled with annotation dictionaries (each dictionary represents an annotation). If I want to delete this pair ["Annots"]-[Array of annotation dictionaries], what do I need to do? There is the method PXCp_ArrayDelete(), using it I could delete the array of dictionaries (or should I iterate within the array and delete each dictionary separately?), but how can I delete the "Annots" key itself? Or is there a certain method to delete the key/value pair?
relapse
User
Posts: 167
Joined: Wed Jan 18, 2012 11:10 am

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by relapse »

I've solved the problem: I should use the PXCp_DictionaryClearValue(dictionary, itemKeyHandle, true) instead of PdfXchangePro.PXCp_DictionaryDelete(itemValueHandle). That commando also deletes all annotations but after deleting them I can create new ones with the PXCp_AddLineAnnotationW() function. Deleting the whole "Annots" array seemed to be a problem. Now I'll try to create annotations with certain title or content (according to your proposal) in order to find them later and delete them.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17893
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by Tracker Supp-Stefan »

Glad to hear that Relapse!

We'd love to hear how it goes!

Cheers,
Stefan
promisekey
User
Posts: 30
Joined: Tue Apr 17, 2012 8:55 pm

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by promisekey »

I tried your code and found "hr = PXCp_DictionaryClearAll(hDict);" works to completely delete an annotation. The PXCp_DictionaryClearValue will clear one Dictionary Key and PXCp_DictionaryDelete causes an error when saving.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17893
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by Tracker Supp-Stefan »

Hi Promisekey,

While we do not officially provide support for the low level API - I've asked one of my colleagues that is really "into the PDF standard" to take a look, and if he has any comments - I would post them here.

Best,
Stefan
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17893
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: No annotation name with PXCp_AddLineAnnotationW?

Post by Tracker Supp-Stefan »

Hi Relapse,

I just got an update that To delete annotations you don't actually need to delete the annotation dictionary, it is enough to just remove the reference to it from Annots array in the page dictionary - this will remove the annotation from the page and it will not be saved.

Best,
Stefan
Post Reply