Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Sun Sep 17, 2017 3:14 am

Hello From Austria!

I prepared a PDF-file where users can add images (jpg, png) to buttons by js --> event.target.buttonImportIcon();
Same script adds an annotation attachment object to the document (the file, which was used to add as buttonIcon.

Both is working fine in Acrobat Reader DC!

Now I have to grab annotationAttachment out of PFD and saving image files to local folders ... all this by WinForm App.

Problem is, I do not find any way to loop though annotations for attachments, reading the name of attachment and get out the corresponding image file.

Please help to get this working.

Thank you,
Franz

ifs
 
Posts: 10
Joined: Wed Sep 13, 2017 6:52 am

Mon Sep 18, 2017 3:41 am

Dear Franz,

Thanks for your inquiry.
Please refer to the code below.
Code: Select all
            PdfDocument doc = new PdfDocument(@"sample.pdf");
            PdfAttachmentCollection collection = doc.Attachments;
            foreach (PdfAttachment attachment in collection)
            {
                string name = attachment.FileName;
                byte[] data = attachment.Data;
                File.WriteAllBytes(name, data);
            }

If there is still issue, please provide us the input file and the code you were using for further investigation.

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Sep 18, 2017 4:47 am

Dear Betsy,

thank you for the provided code sample!

Could I have found this in your documentation too? Because I didn't find this.
Hopefully this will work with Annotation-Attachments too, does it? I'll test this as soon as possible today.

Sorry, I forgot to ask, cause I do have to ADD file annotations while creating new PDFs too. Does this work similar as your provided code?

Could you be so kind and provide me a sample code for adding file attachments to new created PDFs. (or where in your documentation I can find a hint how to do that)

Thank you very much for this quick reply,
Franz

ifs
 
Posts: 10
Joined: Wed Sep 13, 2017 6:52 am

Mon Sep 18, 2017 5:32 am

Dear Betsy,

I immediatels tested you code sample and as I feared it is not working.

Dim collection As PdfAttachmentCollection = doc.Attachments <------ is empty (no objects in, List is empty)

I guess this is because of "attchment" is not a "normal file attachment" --> it is a annotationFileAttachment !

To clarify the situation / workflow:

An "empty" PDF is sent to someone ..... using Acrobat Reader DC only (with Reader it is not possible to add "normal" annotations
With reader only AnnotationAttachments can be added ...... so, people who insert images to PDF with Acrobat Reader DC in fact adding Annotations (as file). Please look at my attached sampe file. When you use Acrobat Reader DC and click on an image the process of adding ButtonIcon and attaching files is driven by javascript.

Anyway, after adding images the PDF could be sent back and then I have to get out files attached.

Vice versa I have to create PDFs where "normal" file attachments should be added when creating new PDFs. No need to be added as annotationAttachment, cause files should be added by my app using Spire.PDF

Please could you help me to get this up.

Thank you very much,
Franz

ifs
 
Posts: 10
Joined: Wed Sep 13, 2017 6:52 am

Mon Sep 18, 2017 7:38 am

Dear Franz,

Thanks for your prompt response.
Here is a tutorial for adding attachment.
https://www.e-iceblue.com/Tutorials/Spi ... -.NET.html
And for the document you provided, please use following code to get the annotation attachments.
Code: Select all
            PdfDocument doc = new PdfDocument(@"AUS-16.09.2017-138-1-2_.pdf");
            PdfPageBase page = doc.Pages[0];
            PdfAnnotationCollection annotation = page.AnnotationsWidget;
            for (int i = 0; i < annotation.List.Count; i++)
            {
                if (annotation.List[i] is PdfAttachmentAnnotationWidget)
                {
                    PdfAttachmentAnnotationWidget attachment = annotation.List[i] as PdfAttachmentAnnotationWidget;
                    string filename =attachment.FileName;
                    byte[] data = attachment.Data;
                    File.WriteAllBytes(filename, data);
                }
            }

If there is any question, please let me know.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Sep 18, 2017 8:34 am

Dear Betsy,

thank you for that quick help!!!

To get out annotation file attachments works perfect for my needs!!!

Creating new PDFs with file attachments will be no problem too, I guess.

Thank you again,
Franz

ifs
 
Posts: 10
Joined: Wed Sep 13, 2017 6:52 am

Mon Sep 18, 2017 8:38 am

Dear Franz,

Thanks for your feedback.
Please feel free to contact us if there is any question. We are here for help :) .

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Sep 19, 2017 5:43 pm

Dear Betsy,

it's me again :-)

Everything OK with getting annotation attachments out of customers PDFs, and also replacing images of PdfButtonWidgetFieldWidget works!

With my second task, adding attachemts to PFD, I'm running into prolems.

I did a very, very simpe trial, this is my code

Dim data As Byte() = File.ReadAllBytes("G:\Dropbox\90-VisualStudio\10-PCIplus\PCIplus13001\PDFAnhangOrdner\11-AVI-2-1.jpg")

Dim attach1 As New PdfAttachment("00-NoImage.jpg", data)

Dim data2 As Byte() = File.ReadAllBytes("G:\Dropbox\90-VisualStudio\10-PCIplus\PCIplus13001\PDFExportEigen\EIN-17.09.2017-138-8-1.pdf")

Dim attach2 As New PdfAttachment("test.pdf", data2)

doc.Attachments.Add(attach1)
doc.Attachments.Add(attach2)

I did various trials .... but nothin worked. Attachments not shown in any PDF viewer.

I inspected the "Attach1 and "Attach2" .... both do not have nothing in MimeType.
I tried to add MimeType, but still no success.

This is so simple, what I'm doing wrong?

Thank you for help,
Franz

ifs
 
Posts: 10
Joined: Wed Sep 13, 2017 6:52 am

Tue Sep 19, 2017 6:49 pm

Hi Betsy,

I figured out a interesting behavour:

I do have 8 buttons on my PDF, with the reset button I fill a "dummy" image called from a hidden button to all 8 visible buttons in oder to "clear" the image buttons. Each button has a unique name.

When I use Spire.PDF to replace images from each button, I get the same image (the last replaced one) into all 8 buttons.

It seams that Spire.PDF doesn't respect the unique name of buttons, instead Spire.PDF references to the ID / name of the image to replace, guess I'm right.

OK, I can resolve this by using 8 hidden buttons, with her "own" images (same look but different image-file-names)

I'll check this out now.

Why Spire.PDF isn't able to change images by button.name?

Better solution would be, not to "change" images .... instead implement the feature to "load" images to empty buttons too.

Thank you for response,
Franz

ifs
 
Posts: 10
Joined: Wed Sep 13, 2017 6:52 am

Wed Sep 20, 2017 2:56 am

Dear Franz,

Thanks for your feedback again.
For the attachment missing issue, I used the latest Spire.PDF Pack(Hot Fix) Version:3.9.285 and following code to add attachment.
Code: Select all
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();
            byte[] data = File.ReadAllBytes(@"F:\image\4.jpg");
            PdfAttachment attach1 = new PdfAttachment("test.jpg",data);
            byte[] data2 = File.ReadAllBytes("11541.pdf");
            PdfAttachment attach2 = new PdfAttachment("test.pdf",data2);
            doc.Attachments.Add(attach1);
            doc.Attachments.Add(attach2);
            doc.SaveToFile("11646.pdf");

I could see the attachment added in Adobe reader.
AttachmentsInReader.png


For replacing image, sorry that I am not sure how you replace the image on the button. And I used following code to replace the image. And I guess that is what you want.
Code: Select all
            PdfDocument doc = new PdfDocument(@"F:\AUS-16.09.2017-138-1-2_.pdf");           
            PdfFormWidget formWidget = doc.Form as PdfFormWidget;
            for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
            {
                PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
                //find the button by name
                if (field is PdfButtonWidgetFieldWidget && field.Name == "AUS1")
                {
                    PdfButtonWidgetFieldWidget botton = field as PdfButtonWidgetFieldWidget;
                    //load the image
                    PdfImage image = PdfImage.FromFile(@"F:\image\4.jpg");
                    //replace the image
                    botton.SetButtonImage(image);
                }
            }
            doc.SaveToFile("ReplaceImage11646.pdf");

If I misunderstanding, please let me know.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Sep 20, 2017 4:15 am

Dear Betsy,

thank you for your quick reply.
Sorry, I'm in hurry cause I have to go to exactely that customer having to declare why I'm late with my solution :-(

Anyway, I checked out your code with attachment files and I figure out, that you didn't use my PDF file (attached to previous post) in order to test the situation. (guess with doc created by Spier.PDF there will be no problem)

But please try the following (as I'm trying all time long):

Dim doc As PdfDocument = New PdfDocument()
doc.LoadFromFile(My.Settings.FileTemplate & "\" & PdfTemplate, FileFormat.PDF) '<--- this is file, attached previous post

Dim form As PdfFormWidget '<--- need for read/write form data

Dim data As Byte() = File.ReadAllBytes("G:\Dropbox\90-VisualStudio\10-PCIplus\PCIplus13001\PDFAnhangOrdner\11-AVI-2-1.jpg")
Dim attach1 As New PdfAttachment("00-NoImage.jpg", data)
Dim data2 As Byte() = File.ReadAllBytes("G:\Dropbox\90-VisualStudio\10-PCIplus\PCIplus13001\PDFExportEigen\EIN-17.09.2017-138-8-1.pdf")
Dim attach2 As New PdfAttachment("test.pdf", data2)
doc.Attachments.Add(attach1)
doc.Attachments.Add(attach2)

Please try adding attachments to my PDF which is done as template in Acrobat DC.

I'll investigate this better when I'll be back from customer today evening my time.

Thank you very much!
Franz

ifs
 
Posts: 10
Joined: Wed Sep 13, 2017 6:52 am

Wed Sep 20, 2017 4:28 am

Dear Betsy,

I did a very very quick trial, not opening customers template file for adding an attachment, I did a nes doc, and of course, attaching files no problem.

But with template file from customer, created im Adobe Acrobat DC .... attaching files not possible, using same code ....

Anyway, pease help to resolve this problem, tank you!
Franz

ifs
 
Posts: 10
Joined: Wed Sep 13, 2017 6:52 am

Wed Sep 20, 2017 6:07 am

Dear Franz,

Thanks for your prompt response.
I have reproduced the issue using the template you sent before, and posted it to our Dev team. We will let you know as soon as there is any progress.
Sorry for the inconvenience caused.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Sep 22, 2017 1:49 pm

Dear Betsy,

do you have any news for me about the problem, adding annotiations to PDF created originally by Acrobat DC?

Thank you for reply,
Franz

ifs
 
Posts: 10
Joined: Wed Sep 13, 2017 6:52 am

Mon Sep 25, 2017 1:40 am

Dear Franz,

Sorry for late reply as weekend.
I got information from our Dev team that the attachment issue has been solved. We will let you know when the hotfix is available.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF