สารสกัดจากเอกสารแนบ PDF ผ่าน C#

วิธีการแยกไฟล์แนบจาก PDF โดยทางโปรแกรมด้วย C#

วิธีการแยกสิ่งที่แนบมาโดยใช้ .NET ห้องสมุด

เพื่อที่จะดึงสิ่งที่แนบมาในไฟล์ PDF เราจะใช้ Aspose.PDF for .NET API ซึ่งเป็นคุณลักษณะที่อุดมไปด้วยที่มีประสิทธิภาพและง่ายต่อการใช้ API การจัดการเอกสาร net แพลตฟอร์มเปิดตัวจัดการแพคเกจ 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;
    }