Извлечение вложений из PDF-документа с помощью C#

Как программно извлекать вложения из PDF с помощью C#

Как извлечь вложения с помощью библиотеки .NET

Чтобы извлечь вложения в PDF-файл, мы будем использовать Aspose.PDF для.NET API, который представляет собой многофункциональный, мощный и простой в использовании API для работы с документами для платформы net. Откройте менеджер пакетов NuGet, найдите Aspose.pdf и установите. Вы также можете использовать следующую команду из консоли Package Manager.

Package Manager Console

PM > Install-Package Aspose.PDF

Извлечение вложений из PDF C#


Вам нужен Aspose.PDF for .NET, чтобы опробовать код в своей среде.

  1. Получите коллекцию встроенных файлов.
  2. Получите количество встроенных файлов.
  3. Прокрутите коллекцию, чтобы получить все вложения.
  4. Проверьте, содержит ли объект parameter параметры.
  5. Получите вложение и запишите его в файл или поток.

Извлечь вложение из PDF-документа


    // Open document
    Document pdfDocument = new Document(dataDir + "GetAlltheAttachments.pdf");

    // Get embedded files collection
    EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles;

    // Get count of the embedded files
    Console.WriteLine("Total files : {0}", embeddedFiles.Count);

    int count = 1;

    // Loop through the collection to get all the attachments
    foreach (FileSpecification fileSpecification in embeddedFiles)
    {
        Console.WriteLine("Name: {0}", fileSpecification.Name);
        Console.WriteLine("Description: {0}",
        fileSpecification.Description);
        Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType);

        // Check if parameter object contains the parameters
        if (fileSpecification.Params != null)
        {
            Console.WriteLine("CheckSum: {0}",
            fileSpecification.Params.CheckSum);
            Console.WriteLine("Creation Date: {0}",
            fileSpecification.Params.CreationDate);
            Console.WriteLine("Modification Date: {0}",
            fileSpecification.Params.ModDate);
            Console.WriteLine("Size: {0}", fileSpecification.Params.Size);
        }

        // Get the attachment and write to file or stream
        byte[] fileContent = new byte[fileSpecification.Contents.Length];
        fileSpecification.Contents.Read(fileContent, 0,
        fileContent.Length);
        FileStream fileStream = new FileStream(dataDir + count + "_out" + ".txt",
        FileMode.Create);
        fileStream.Write(fileContent, 0, fileContent.Length);
        fileStream.Close();
        count+=1;
    }