C# を介して PDF ドキュメントから添付ファイルを抽出

C# を使用してPDFから添付ファイルをプログラムで抽出する方法

.NET ライブラリを使用して添付ファイルを抽出する方法

添付ファイルをPDFファイルに抽出するために、net プラットフォーム用の機能豊富で強力で使いやすいドキュメント操作APIである Aspose.PDF for .NET APIを使用します。NuGet パッケージマネージャーを開き、aspose.pdf を検索してインストールします。パッケージマネージャーコンソールから次のコマンドを使用することもできます。

Package Manager Console

PM > Install-Package Aspose.PDF

PDF C# から添付ファイルを抽出


ご使用の環境でコードを試すには Aspose.PDF for .NET が必要です。

1。埋め込みファイルコレクションを取得します。 1。埋め込みファイルの数を取得します。 1。コレクションをループして、すべての添付ファイルを取得します。 1。パラメーターオブジェクトにパラメーターが含まれているかどうかを確認します。 1。添付ファイルを取得し、ファイルまたはストリームに書き込みます。

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;
    }