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