Page 1 of 1

Drawing a rect with PDF-Tools

Posted: Thu Apr 26, 2012 10:24 am
by relapse
Hello!

I used to draw a rectangle of a certain color and thickness in PDF Viewer using annotations (with the JavaScript function addAnnot()).

Could I simply draw a rectangle with any function of the PDF-Tools Library or should I also create an Annotation with the PXCp_Add3DAnnotationW() function? The problem is I'm trying to use only the PDF-Tools in order to manipulate a PDF-Document.


Thanks for any answers!

Re: Drawing a rect with PDF-Tools

Posted: Thu Apr 26, 2012 4:55 pm
by Tracker Supp-Stefan
Hi relapse,

Have you tried the
PXCp_AddLineAnnotationW
method?

Cheers,
Stefan

Re: Drawing a rect with PDF-Tools

Posted: Fri Apr 27, 2012 5:57 am
by relapse
I'll do :D

Re: Drawing a rect with PDF-Tools

Posted: Fri Apr 27, 2012 10:53 am
by Tracker Supp-Stefan
:)

Re: Drawing a rect with PDF-Tools

Posted: Fri May 04, 2012 8:35 am
by relapse
Hi!

I have some questions on the PXCp_AddLineAnnotationW function:

1. The last parameter of the function is a pointer to a PXC_CommonAnnotInfo structure. It expects among other things an integer value for a color. Here is an extract from the docs with a pseudo code:

Code: Select all

AnnotInfo.m_Color = RGB(200, 0, 100);
I couldn't find any equivalent of that function in WPF. How is that value calculated?
RGB(255, 255, 255) = 16777215 ???
2. A comprehension question: should I draw four single lines in order to create a rectangle or can I directly draw a rectangle with that function (with the parameter LPCPXC_RectF rect that specifies the bounding rectangle of the annotation)?


This is an extract from my code:

Code: Select all

            var borderRect = new PdfXchangePro.PXC_RectF { left = selection.Left,
                                                           right = selection.Right,
                                                           top = selection.Top,
                                                           bottom = selection.Bottom };

            int color = 16777215; // RGB(255, 255, 255) ???
            var border = new PdfXchangePro.PXC_AnnotBorder { m_Width = StrToDouble(BorderThickness), 
                                                             m_Type = PdfXchangePro.PXC_AnnotBorderStyle.ABS_Solid };
            
            var borderInfo = new PdfXchangePro.PXC_CommonAnnotInfo{ m_Color = color,
                                                                    m_Flags = Convert.ToInt32(PdfXchangePro.PXC_AnnotsFlags.AF_ReadOnly),
                                                                    m_Opacity = _opacity,
                                                                    m_Border = border };

            var startPoint = new PdfXchangePro.PXC_PointF {x = selection.Left, y = selection.Top};
            var endPoint = new PdfXchangePro.PXC_PointF {x = selection.Right, y = selection.Bottom};
            
            int retval = PdfXchangePro.PXCp_AddLineAnnotationW(_handle, 
                                                               0, 
                                                               ref borderRect, 
                                                               "xy", 
                                                               "yx", 
                                                               ref startPoint, 
                                                               ref endPoint, 
                                                               PdfXchangePro.PXC_LineAnnotsType.LAType_None, 
                                                               PdfXchangePro.PXC_LineAnnotsType.LAType_None, 
                                                               color, 
                                                               ref borderInfo); // function returns 0
I didn't define the values for AnnotInfo.m_Border.m_DashArray. No annotation is created. I tested it with JavaScript command this.getAnnots(0); It returns null.

Thanks!

Re: Drawing a rect with PDF-Tools

Posted: Sat May 05, 2012 12:40 am
by Ivan - Tracker Software
Can you send me PDF generated by your code ?

P.S. RGB is 'macro' is equivalent to the following function

Code: Select all

// r, g, and b in range from 0 to 255
ULONG _RGB(int r, int g, int b)
{
  return (ULONG)(r + g * 256 + b * 65536);
}


Re: Drawing a rect with PDF-Tools

Posted: Mon May 07, 2012 10:58 am
by relapse
I've got it! I had to close the document in the PDF viewer before creating a line annotation with PDF Tools Library function PXCp_AddLineAnnotationW!

I still don't understand whether I can create a rectangle as one annotation or I have to construct it generating 4 single lines? The third parameter (LPCPXC_RectF rect) in this function is according to the documentation a bounding rectangle of the
annotation. What is the use of it?

One more question. Is it possible to suppress the pop-up annotation dialog that appears after a double-click on the annotation? Are there any parameter in the PXCp_AddLineAnnotationW function to manage it. I've only found the flag PXC_AnnotsFlags.AF_ReadOnly of the PXC_CommonAnnotInfo class, that makes the line annotation (or the annotation bounding rectangle?) read-only.

Re: Drawing a rect with PDF-Tools

Posted: Mon May 07, 2012 1:04 pm
by relapse
Another question: can I delete an annotation with PDF-Tools?

Re: Drawing a rect with PDF-Tools

Posted: Tue May 08, 2012 12:45 pm
by Tracker Supp-Stefan
Hi Relapse,

It is definitely possible with the low level functions, but unfortunately the only High Level ones are for adding annotations - not for deleting them.

Best,
Stefan

Re: Drawing a rect with PDF-Tools

Posted: Tue May 08, 2012 2:50 pm
by relapse
Could you please inform me which one I can use? In the documentation I've only found functions to add annotations, but nothing to delete them.

Re: Drawing a rect with PDF-Tools

Posted: Tue May 08, 2012 5:16 pm
by Tracker Supp-Stefan
Hello relapse,

As mentioned before - there are no high level functions that will allow you to delete annotations.

So you will need to read on annotations in the PDF Reference:
http://wwwimages.adobe.com/www.adobe.co ... ce_1-7.pdf
section 8.4 Annotations in the above document

or section 12.4 Annotations in the ISO version of the file:
http://wwwimages.adobe.com/www.adobe.co ... 0_2008.pdf

And then utilize the low level functions described in
3.2.5 PDF Dictionary Functions of our PDF Tools SDK manual to read and manipulate the annotations dictionary as neeed.

Alternatively - you could use JS while you have the files opened in the Viewer AX. This should be quite a lot easier to implement, and will still allow you to create/edit/delete annotations as needed.

Best,
Stefan
Tracker

Re: Drawing a rect with PDF-Tools

Posted: Wed May 09, 2012 9:01 am
by relapse
Thanks for your reply. Is there any sample program demonstrating the use of dictionaries?

Re: Drawing a rect with PDF-Tools

Posted: Wed May 09, 2012 9:34 am
by Tracker Supp-Stefan
Hi Relapse,

There are some snippets inside the manual, but there isn't anything more complex - as those are low level functions giving you access to the very structure of the PDF File and the way you would like to use such methods will greatly vary from case to case. You will need to get yourself acquainted with the PDF specification to be able to use those successfully.

Best,
Stefan

Re: Drawing a rect with PDF-Tools

Posted: Wed May 09, 2012 10:42 am
by relapse
I do read the pdf specification :D
I cannot understand how it is possible to access the Annotations-dictionary of a ceratin page.
I've found the function PXCp_ObjectGetDictionary, but it needs an object handle. Where can I get it?

Re: Drawing a rect with PDF-Tools

Posted: Wed May 09, 2012 1:36 pm
by Tracker Supp-Stefan
Hi relapse,

You might want to use functions like PXCp_llGetObjectByIndex to obtain an object first, and then here is the sample from the manual for using the PXCp_ObjectGetDictionary function:

Code: Select all

// Retrieve object's dictionary
       
       HPDFOBJECT hObject;
       
       ...
       
       HPDFDICTIONARY hDict;
       
       hr = PXCp_ObjectGetDictionary(hObject, &hDict);
       if (IS_DS_FAILED(hr))
       {
               // report error
               ...
       }
Best,
Stefan

Re: Drawing a rect with PDF-Tools

Posted: Wed May 09, 2012 3:18 pm
by relapse
I try to use the PXC_Rect function in order to draw a real rectangle and not an annotation.

HRESULT PXC_Rect(
_PXCContent* content,
double left,
double top,
double right,
double bottom
);

Parameters
content [in] Parameter content specifies the identifier for the page content to which the function will be applied.


What is this identifier for the page content and how can I get it?



Thanks!

Re: Drawing a rect with PDF-Tools

Posted: Wed May 09, 2012 4:20 pm
by Tracker Supp-Stefan
Hi Relapse,

This method is from the PXCLIB40 set of functions - those are aimed at creating new PDF document from scratch - so you can not use that to just add a rectangle to an already existing page I am afraid.

Otherwise - you can see how the content identifier is to be set up in the sample projects in
C:\Program Files\Tracker Software\PDF-XChange PRO 4 SDK\Examples\SDKExamples\<<YOUR Programming language>>\PDFXCDemo

Best,
Stefan

Re: Drawing a rect with PDF-Tools

Posted: Thu May 10, 2012 8:28 am
by relapse
Thanks, Stefan, your patience is honorable. :D

Is there any difference between

HRESULT PXCp_Init(PDFDocument* pObject, LPCSTR Key, LPCSTR DevCode);

and

HRESULT PXC_NewDocument(_PXCDocument** pdf, LPCSTR key, LPCSTR devCode);

? Are the both parameters PDFDocument* pObject and _PXCDocument** pdf identical?

I've tried to mix the use of the both libraries:

Code: Select all

int pageContentIdentifier;
int pdfHandle;
int pdfPage = 0;
PdfXchangePro.PXCp_Init(out pdfHandle, PdfXchangePro.SerialNumber, PdfXchangePro.DevelopmentCode);
PdfXchangePro.PXCp_ReadDocumentW(pdfHandle, _tempFile, 0);
PdfXchange.PXC_GetPage(pdfHandle, pdfPage, out pageContentIdentifier);
PdfXchange.PXC_Rect(pdfHandle, 20, 100, 100, 20);
but I've got an AccessViolationException executing the PXC_GetPage function.

I've also found no function to delete a new created (with PXC_Rect) graphical object or is it not possible at all?

Re: Drawing a rect with PDF-Tools

Posted: Thu May 10, 2012 4:41 pm
by Tracker Supp-Stefan
Hi Relapse,

I am afraid you can't mix methods from the two libraries. You will need to create and save a PDF file using the PXC_ methods, and then open it and modify it using the PCXp_ones.

As PCX_ methods are designed for building up PDF files - there are no delete methods - as you are creating a pdf file or page starting from an empty one and only adding the components you want.

Best,
Stefan

Re: Drawing a rect with PDF-Tools

Posted: Fri May 11, 2012 8:46 am
by relapse
Yesterday I managed to draw a rectangle I needed but the restriction is - it must be a new pdf document. It's a pity! :D

Now I'm trying to delete line annotations directly in dictionaries. By the way I can create a line annotation of any thickness with the function PXCp_AddLineAnnotationW, there is no such a limit of 20 points as in JS. But I miss very much any examples for handling the dictionaries. I've found an example in the forum https://forum.pdf-xchange.com/ ... nnotationW but it's in C++ and I'm fighting with translation of the of the low-level functions' declarations into C#.

Re: Drawing a rect with PDF-Tools

Posted: Fri May 11, 2012 12:14 pm
by Tracker Supp-Stefan
Hello Relapse,

Glad to hear that you got it working. And great to hear there are no width limitations with the PXCp_AddLineAnnotationW method.

As for samples for handling dictionaries - I am afraid that I can't help - any any samples would probably be in the PDF Specification itself.

Best,
Stefan

Re: Drawing a rect with PDF-Tools

Posted: Mon May 14, 2012 5:38 pm
by Walter-Tracker Supp
relapse wrote:Yesterday I managed to draw a rectangle I needed but the restriction is - it must be a new pdf document. It's a pity! :D

Now I'm trying to delete line annotations directly in dictionaries. By the way I can create a line annotation of any thickness with the function PXCp_AddLineAnnotationW, there is no such a limit of 20 points as in JS. But I miss very much any examples for handling the dictionaries. I've found an example in the forum https://forum.pdf-xchange.com/ ... nnotationW but it's in C++ and I'm fighting with translation of the of the low-level functions' declarations into C#.
The best advice here is to look at the C# wrappers for other projects. It is important to use the proper marshalling for types like BSTR and LPWSTR (from C# "string" types). If you look at function declarations for DLL imports in C# you'll often see a function argument prefixed by something like:

Code: Select all

[MarshalAs(UnmanagedType.LPWStr)]
e.g.:

Code: Select all

sometype somefunction([MarshalAs(UnmanagedType.LPWStr)] string InputLPWSTR);
This specifies that the string inputLPWSTR is to be marshalled as a LPWSTR type to the code in the DLL.

UnmanagedType has a lot of members (LPWStr, BStr, etc) that you can specify for different scenarios. Check MSDN for details or use autocomplete in Visual Studio to see a list.

Also note the use of "ref" and "out" keywords that are used when the API function takes a pointer. "ref" means C# will check to see if the value is initialized; "out" means it may be uninitialized and is expected to be set by the function.

Code: Select all

E.g. C++:

HRESULT calculate_property_of_mystruct(mystruct* input, int* output);

would be imported into C# with: 

... calculate_property_of_mystruct(ref mystruct input, out int output);



Lots of reading here:
http://msdn.microsoft.com/en-us/library/26thfadc.aspx
http://msdn.microsoft.com/en-us/library/fzhhdwae.aspx