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.

Fri Mar 24, 2023 11:05 am

I am trying to extract attachments from an existing PDF file.

And the following code does not see to work.

Any help would be appreciated.

Code: Select all
public static void ExtractAttachments()
        {
            Spire.License.LicenseProvider.SetLicenseKey(LICENSE_KEY);
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a PDF file that contains attachments
            doc.LoadFromFile("D:\\Trash\\PdfTest\\Report (26).pdf");

            //Get the attachment collection of the PDF document
            PdfAttachmentCollection attachments = doc.Attachments;

            //Specific output folder path
            string outputFolder = "d:\\Trash\\PdfTest\\";

            //Loop through the collection
            for (int i = 0; i < attachments.Count; i++)
            {
                //Write attachment to a file
                File.WriteAllBytes(outputFolder + attachments[i].FileName, attachments[i].Data);
            }
        }

dschultz_techdata
 
Posts: 2
Joined: Sun Apr 03, 2022 12:39 pm

Mon Mar 27, 2023 3:32 am

Hi,

Thanks for your feedback.
After testing, I reproduced your issue and logged it into our issue tracking system with the ticket number SPIREPDF-5892, our developers will investigate and fix it. Sorry for the inconvenience caused, once the issue is fixed, I will inform you asap.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Mar 27, 2023 8:18 am

Hi,

Thank you for bringing to our attention the issue you encountered when trying to extract PDF attachments using our product. After investigating the matter, our development team has determined that this is not a bug, but rather an issue related to the structure of the PDF document.

Attachments in PDF documents can be in two places: under the Category node at the root of the document, or within an attachment annotation on a specific page. To extract attachments from both locations, please refer to the following code:
Code: Select all
public void ExtractAttachments()
{
   
    //Create a PdfDocument object
    PdfDocument doc = new PdfDocument();

    //Load a PDF file that contains attachments
    doc.LoadFromFile("D:\\Trash\\PdfTest\\Report (26).pdf");

    //Get the attachment collection of the PDF document
    PdfAttachmentCollection attachments = doc.Attachments;

    //Specific output folder path
    string outputFolder = "d:\\Trash\\PdfTest\\";

    //Loop through the collection
    for (int i = 0; i < attachments.Count; i++)
    {
        //Write attachment to a file
        File.WriteAllBytes(outputFolder + attachments[i].FileName, attachments[i].Data);
    }

    //Loop through each page
    foreach(PdfPageBase page in pdf.Pages)
    {
        foreach(PdfAnnotation annotation in page.AnnotationsWidget)
        {
            if(annotation is PdfAttachmentAnnotationWidget)
            {
                PdfAttachmentAnnotationWidget attachmentAnnot = annotation as PdfAttachmentAnnotationWidget;
                File.WriteAllBytes(outputFolder + attachmentAnnot.FileName, attachmentAnnot.Data);
            }
        }
    }
}


Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Mar 27, 2023 9:33 am

Thank you for the response.

I was able to use the supplied code and get the attachments out.

dschultz_techdata
 
Posts: 2
Joined: Sun Apr 03, 2022 12:39 pm

Mon Mar 27, 2023 12:20 pm

Hi,

Thanks for your feedback.
Glad to hear that the code works for your issue, if you have any other questions, just feel free to contact us.
Have a nice day! :D

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.PDF