AddAnnotationFromXML

Discussions about annotation support.
Post Reply
KevinLK
Posts: 15
Joined: Wed Jan 13, 2016 5:08 pm

AddAnnotationFromXML

Post by KevinLK » Mon Nov 21, 2016 2:46 pm

Hi,

Im currently trying to read the annotations from a document, and burn them to a new transparent png image.

The problem is though that the annotations that have been loaded with XML aren't added.

If i try to add an annotation through method it works fine.

Is there something wrong with the XML data or is that a bug?

Code: Select all

  GdPicturePDF pdf = new GdPicturePDF();
            pdf.LoadFromFile(@"C:\Test\Test.pdf", false);
            Int32 iId = pdf.RenderPageToGdPictureImage(72, true, true);

            AnnotationManager am = new AnnotationManager();

            GdPictureImaging oGdPictureImaging = new GdPictureImaging();
            //Int32 iId = oGdPictureImaging.CreateGdPictureImageFromFile(@"T:\Temp\TSN\test.pdf");

            am.InitFromGdPictureImage(iId);

            am.AddAnnotationFromXML(arrow);
            am.BurnAnnotationsToPage(false);
            am.SaveAnnotationsToPage();
            oGdPictureImaging.SaveAsPNG(iId, @"C:\Test\test.png");

Code: Select all

<Annotation>
               <Type>AnnotationTypeLineArrow</Type>
               <ID>624781a1-5013-4d96-9fc1-baf679ce793a</ID>
               <Left>4.26917</Left>
               <Top>7.77454</Top>
               <Width>0.31844</Width>
               <Height>0.9842</Height>
               <Rotation>205.9388</Rotation>
               <Opacity>1</Opacity>
               <SquaredBox>False</SquaredBox>
               <StrokeColor>-16777216</StrokeColor>
               <FillColor>-16777216</FillColor>
               <BorderWidth>0.05</BorderWidth>
               <RadiusFactor>0</RadiusFactor>
               <CreationTime>24-09-2014 16:53:02</CreationTime>
               <ModificationTime>12-01-2016 14:48:25</ModificationTime>
               <CanMove>True</CanMove>
               <CanResize>True</CanResize>
               <CanRotate>True</CanRotate>
               <CanSelect>True</CanSelect>
               <Printable>True</Printable>
               <PreventBurning>False</PreventBurning>
               <Visible>True</Visible>
               <Fill>False</Fill>
               <Stroke>True</Stroke>
               <Zorder>4</Zorder>
               <FlipX>False</FlipX>
               <FlipY>True</FlipY>
               <ArrowWidth>5</ArrowWidth>
               <ArrowHeight>7</ArrowHeight>
               <ArrowFilled>True</ArrowFilled>
            </Annotation>
Kind regards,
Kevin

Cedric
Posts: 263
Joined: Sun Sep 02, 2012 7:30 pm

Re: AddAnnotationFromXML

Post by Cedric » Wed Nov 23, 2016 1:25 pm

I don't think this is a valid annotation XML, what is expected as an input is the result of the GetAnnotationXML method call, is that what you are doing?

KevinLK
Posts: 15
Joined: Wed Jan 13, 2016 5:08 pm

Re: AddAnnotationFromXML

Post by KevinLK » Wed Nov 23, 2016 3:41 pm

That is what im doing.

The XML i have added as a quote to this thread is one a pulled trough GetAnnotationXML

Cedric
Posts: 263
Joined: Sun Sep 02, 2012 7:30 pm

Re: AddAnnotationFromXML

Post by Cedric » Wed Nov 23, 2016 3:59 pm

You should monitor the returned status after each method call, especially the AddAnnotationFromXML one, it should return an arrow annotation and if not, call the GetStat method in order to find out why, that's the only way to find out what went wrong.

KevinLK
Posts: 15
Joined: Wed Jan 13, 2016 5:08 pm

Re: AddAnnotationFromXML

Post by KevinLK » Wed Nov 23, 2016 4:15 pm

I just tried that, and the returned status is an GenericError. Any idear how i can debug it to get a more specific error?

Cedric
Posts: 263
Joined: Sun Sep 02, 2012 7:30 pm

Re: AddAnnotationFromXML

Post by Cedric » Wed Nov 23, 2016 4:19 pm

Could you please provide all the required elements to reproduce the issue in the same conditions as you do (that includes the input document, source code to annotate and get the annotation in addition to the source code already provided) so we can replicate the issue on our side?
A standalone project that we can run as-is would be the best.
You can open a ticket on the support platform to send us the investigation material if you prefer not to expose those information on the public forum.

KevinLK
Posts: 15
Joined: Wed Jan 13, 2016 5:08 pm

Re: AddAnnotationFromXML

Post by KevinLK » Wed Jan 04, 2017 8:57 am

Sorry for the late answer.
I managed to figuere it out.

But thanks anyway

StevenH
Posts: 4
Joined: Mon Mar 20, 2017 3:28 pm

Re: AddAnnotationFromXML

Post by StevenH » Thu Mar 30, 2017 11:19 am

I'm actually having this same problem in my asp.net mvc project.

I'm saving annotations to xml files and attempting reload them later.

I call the annotationmanager.AddAnnotationFromXML(fileXmlContent) and annotationManager.SaveAnnotationsToPage() methods and the status is all 'OK'

When I call the GpPicture.GetAnnotationCount() method shows that the annotations have been added but they don't appear / display in my DocuVieware instance.

Was there ever a solution to this issue?

yuancn
Posts: 3
Joined: Mon Aug 06, 2018 5:18 pm

Re: AddAnnotationFromXML

Post by yuancn » Mon Aug 06, 2018 5:24 pm

Hi Kevin, I am having the same issue that the AddAnnotationFromXML does not add the annotation. Could you please share your solution here?

Thanks a lot in advance.

Cedric
Posts: 263
Joined: Sun Sep 02, 2012 7:30 pm

Re: AddAnnotationFromXML

Post by Cedric » Mon Aug 06, 2018 5:30 pm

When the annotations are loaded manually like this, it only happens server side so you won't have any visual feedback of the operation until you explicitly refresh the client side using the dedicated method: http://www.docuvieware.com/guides/aspne ... tions.html

yuancn
Posts: 3
Joined: Mon Aug 06, 2018 5:18 pm

Re: AddAnnotationFromXML

Post by yuancn » Mon Aug 06, 2018 6:01 pm

Hi Cedric,

Thanks for your quick response. What I am doing is on server side, which is a web API function. Here is how my code look like:

using (var pdfFile1 = new GdPicturePDF())
{
pdfFile1.LoadFromFile($"c:\\temp\\1 page II.pdf", true);
pdfFile1.SetOrigin(PdfOrigin.PdfOriginTopLeft);
pdfFile.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
pdfFile1.SelectPage(1);
using (var fileAnnoManager = new AnnotationManager())
{
fileAnnoManager.InitFromGdPicturePDF(pdfFile1);
fileAnnoManager.SelectPage(1);

var addedAnnByXml = fileAnnoManager.AddAnnotationFromXML(
@"<Annotation>
<Type>AnnotationTypeRectangle</Type>
<ID>2a05926a-228d-40bf-abd0-dc617a59b605</ID>
<Left>2.25</Left>
<Top>1.5</Top>
<Width>2.5</Width>
<Height>1</Height>
<Rotation>0</Rotation>
<Opacity>1</Opacity>
<SquaredBox>False</SquaredBox>
<Stroke>True</Stroke>
<StrokeColor>-65536</StrokeColor>
<Fill>True</Fill>
<FillColor>-8388652</FillColor>
<BorderWidth>0.01042</BorderWidth>
<RadiusFactor>0</RadiusFactor>
<CreationTime>2018-08-06T11:41:56.3031295-04:00</CreationTime>
<ModificationTime>2018-08-06T11:41:56.3031295-04:00</ModificationTime>
<CanEdit>True</CanEdit>
<CanDelete>True</CanDelete>
<CanMove>True</CanMove>
<CanResize>True</CanResize>
<CanRotate>True</CanRotate>
<CanSelect>True</CanSelect>
<Printable>True</Printable>
<PreventBurning>False</PreventBurning>
<Visible>True</Visible>
<Zorder>0</Zorder>
<FlipX>False</FlipX>
<FlipY>False</FlipY>
</Annotation>");

fileAnnoManager.SaveAnnotationsToPage();
fileAnnoManager.BurnAnnotationsToPage(false);
pdfFile1.SaveToFile("c:\\temp\\PDFWithAnnotation_1.pdf");
fileAnnoManager.SaveDocumentToPDF("c:\\temp\\PDFWithAnnotation_2.pdf");

fileAnnoManager.Close();

return pdfFile1.GetStat();
}
}

I load file to a GDPicturePDF file ("1 page II.pdf"), add annotation from an xml string, and save the file back to hard disk ("PDFWithAnnotation_1.pdf" and "PDFWithAnnotation_2.pdf") to verify it. Both files do not have the annotation. The state of the annotationManager is GenericError after I call the AddAnnotationFromXML function. Could you please tell me what I did wrong?

By the way, the annotation get added if I call AddRectangleAnnot (and the xml string is got by calling GetAnnotationXML function).

Thanks a lot for your help in advance!

Cedric
Posts: 263
Joined: Sun Sep 02, 2012 7:30 pm

Re: AddAnnotationFromXML

Post by Cedric » Tue Aug 07, 2018 11:51 am

I don't know what's wrong exactly with your code as it does not really make sense on several aspects, all I can say is that this code is perfectly working for me using the latest version, I got no GenericError from the AddAnnotationFromXML method call:

Code: Select all

using (var pdfFile1 = new GdPicturePDF())
{
    pdfFile1.LoadFromFile("input.pdf", false);
    using (var fileAnnoManager = new AnnotationManager())
    {
        fileAnnoManager.InitFromGdPicturePDF(pdfFile1);
        fileAnnoManager.SelectPage(1);
        var addedAnnByXml = fileAnnoManager.AddAnnotationFromXML(
            @"<Annotation>
            <Type>AnnotationTypeRectangle</Type>
            <ID>2a05926a-228d-40bf-abd0-dc617a59b605</ID>
            <Left>2.25</Left>
            <Top>1.5</Top>
            <Width>2.5</Width>
            <Height>1</Height>
            <Rotation>0</Rotation>
            <Opacity>1</Opacity>
            <SquaredBox>False</SquaredBox>
            <Stroke>True</Stroke>
            <StrokeColor>-65536</StrokeColor>
            <Fill>True</Fill>
            <FillColor>-8388652</FillColor>
            <BorderWidth>0.01042</BorderWidth>
            <RadiusFactor>0</RadiusFactor>
            <CreationTime>2018-08-06T11:41:56.3031295-04:00</CreationTime>
            <ModificationTime>2018-08-06T11:41:56.3031295-04:00</ModificationTime>
            <CanEdit>True</CanEdit>
            <CanDelete>True</CanDelete>
            <CanMove>True</CanMove>
            <CanResize>True</CanResize>
            <CanRotate>True</CanRotate>
            <CanSelect>True</CanSelect>
            <Printable>True</Printable>
            <PreventBurning>False</PreventBurning>
            <Visible>True</Visible>
            <Zorder>0</Zorder>
            <FlipX>False</FlipX>
            <FlipY>False</FlipY>
            </Annotation>");
        fileAnnoManager.SaveAnnotationsToPage();
        fileAnnoManager.BurnAnnotationsToPage(true);
        fileAnnoManager.Close();
    }
    pdfFile1.SaveToFile("ouput.pdf");
    pdfFile1.CloseDocument();
}
Since you're not doing anything but adding an annotation on the document, I would recommend to only use the AnnotationManager class as there is no need for the GdPicturePDF in that case which simplifies the operation:

Code: Select all

using (var fileAnnoManager = new AnnotationManager())
{
    fileAnnoManager.InitFromFile("input.pdf");
    fileAnnoManager.SelectPage(1);
    var addedAnnByXml = fileAnnoManager.AddAnnotationFromXML(
        @"<Annotation>
        <Type>AnnotationTypeRectangle</Type>
        <ID>2a05926a-228d-40bf-abd0-dc617a59b605</ID>
        <Left>2.25</Left>
        <Top>1.5</Top>
        <Width>2.5</Width>
        <Height>1</Height>
        <Rotation>0</Rotation>
        <Opacity>1</Opacity>
        <SquaredBox>False</SquaredBox>
        <Stroke>True</Stroke>
        <StrokeColor>-65536</StrokeColor>
        <Fill>True</Fill>
        <FillColor>-8388652</FillColor>
        <BorderWidth>0.01042</BorderWidth>
        <RadiusFactor>0</RadiusFactor>
        <CreationTime>2018-08-06T11:41:56.3031295-04:00</CreationTime>
        <ModificationTime>2018-08-06T11:41:56.3031295-04:00</ModificationTime>
        <CanEdit>True</CanEdit>
        <CanDelete>True</CanDelete>
        <CanMove>True</CanMove>
        <CanResize>True</CanResize>
        <CanRotate>True</CanRotate>
        <CanSelect>True</CanSelect>
        <Printable>True</Printable>
        <PreventBurning>False</PreventBurning>
        <Visible>True</Visible>
        <Zorder>0</Zorder>
        <FlipX>False</FlipX>
        <FlipY>False</FlipY>
        </Annotation>");
    fileAnnoManager.SaveAnnotationsToPage();
    fileAnnoManager.BurnAnnotationsToPage(true);
    fileAnnoManager.SaveDocumentToPDF("output.pdf");
    fileAnnoManager.Close();
}

yuancn
Posts: 3
Joined: Mon Aug 06, 2018 5:18 pm

Re: AddAnnotationFromXML

Post by yuancn » Thu Aug 09, 2018 3:16 pm

Cedric,

Thanks a lot for your help and your code works for me perfectly. The reason that I need a PDF File is that our API needs to return the byte[] of the PDF file with the annotations. Not sure if the AnnotationManager can do that.

Now, I need to set up the annotation properties, such as location (origin point, X, Y coordinates and their measurement unit), font name for text annotation, etc. I searched online document but could not find useful information.

There is a annotation manager function called SetAnnotationPropertyValue, not sure if I may use it to do the setup. Can you please give me some code samples about these setup, or provide some help documents so that I can figure it out myself?

By the way, what is the functionalities of AddCustomAnnot function? Could you please provide some documents for it to me as well?

Many Thanks!

Cedric
Posts: 263
Joined: Sun Sep 02, 2012 7:30 pm

Re: AddAnnotationFromXML

Post by Cedric » Thu Aug 09, 2018 3:36 pm

You don't need that method because, as explained in the documentation that you should read, it has been designed for COM interop users, there is no need for this method here.
The AddAnnotationFromXML method, still according to the documentation, returns an Annotation object so you can directly change its properties the way you want before saving the annotation to the document. You might need to cast this object to the proper type in case you need to access properties that are only available in your particular annotation type (i.e. an AnnotationRectangle for instance in your case).

Code: Select all

using (var fileAnnoManager = new AnnotationManager())
{
    fileAnnoManager.InitFromFile("input.pdf");
    fileAnnoManager.SelectPage(1);
    var addedAnnByXml = fileAnnoManager.AddAnnotationFromXML(
        @"<Annotation>
        <Type>AnnotationTypeRectangle</Type>
        <ID>2a05926a-228d-40bf-abd0-dc617a59b605</ID>
        <Left>2.25</Left>
        <Top>1.5</Top>
        <Width>2.5</Width>
        <Height>1</Height>
        <Rotation>0</Rotation>
        <Opacity>1</Opacity>
        <SquaredBox>False</SquaredBox>
        <Stroke>True</Stroke>
        <StrokeColor>-65536</StrokeColor>
        <Fill>True</Fill>
        <FillColor>-8388652</FillColor>
        <BorderWidth>0.01042</BorderWidth>
        <RadiusFactor>0</RadiusFactor>
        <CreationTime>2018-08-06T11:41:56.3031295-04:00</CreationTime>
        <ModificationTime>2018-08-06T11:41:56.3031295-04:00</ModificationTime>
        <CanEdit>True</CanEdit>
        <CanDelete>True</CanDelete>
        <CanMove>True</CanMove>
        <CanResize>True</CanResize>
        <CanRotate>True</CanRotate>
        <CanSelect>True</CanSelect>
        <Printable>True</Printable>
        <PreventBurning>False</PreventBurning>
        <Visible>True</Visible>
        <Zorder>0</Zorder>
        <FlipX>False</FlipX>
        <FlipY>False</FlipY>
        </Annotation>");
    AnnotationRectangle rectangle = (AnnotationRectangle)addedAnnByXml;
    rectangle.Author = "yuancn";
    rectangle.FillColor = Color.Aqua;
    rectangle.StrokeColor = Color.DeepPink;
    rectangle.Left = 0;
    rectangle.Top = 0;
    fileAnnoManager.SaveAnnotationsToPage();
    fileAnnoManager.BurnAnnotationsToPage(true);
    fileAnnoManager.SaveDocumentToPDF("output.pdf");
    fileAnnoManager.Close();
}
The reason that I need a PDF File is that our API needs to return the byte[] of the PDF file with the annotations. Not sure if the AnnotationManager can do that.
Again, you really need to read the documentation. The SaveDocumentToPDF method has an overload that saves the result in a Stream which is very easy to convert to a byte array.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest