Extract Attachments from a PDF - Console C#

本教程展示如何提取附件公司luded inside a PDF file in a C# Windows Console application using the LEADTOOLS SDK.

Overview
Summary This tutorial covers how to extract PDF attachments and convert them to PNG files in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing theAdd References and Set a Licensetutorial, before working on theExtract Attachments from a PDF - Console C#tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in theAdd References and Set a Licensetutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed.

The DLLs are located at\LEADTOOLS21\Bin\Dotnet4\x64:

For a complete list of which DLL files are required for your application, refer toFiles to be Included in your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer toSetting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in theAdd References and Set a Licensetutorial.

Add the PDF Attachment Extraction and Conversion Code

With the project created, the references added, and the license set, coding can begin.

In theSolution Explorer, openProgram.cs. Add the following statements to the using block at the top ofProgram.cs:

C#
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingLeadtools;usingLeadtools.Caching;usingLeadtools.Codecs;usingLeadtools.Document;usingLeadtools.Document.Converter;usingLeadtools.Document.Writer;

Add the following global variable to theProgramclass.

C#
staticFileCache cache;staticstringOutputDir ="Output";

Create a new method insideProgram.csnamedExtractPDFAttachments(). Call the method in theMain()method, under the set license call, as shown below.

C#
staticvoidMain(string[] args){try{SetLicense();ExtractPDFAttachments();}catch(Exception ex){Console.WriteLine(ex.ToString());}Console.WriteLine("Press any key to exit...");Console.ReadKey(true);}

Add the code below to theExtractPDFAttachments()method to extract the attachments from the given PDF.

C#
staticvoidExtractPDFAttachments(){cache =newFileCache { CacheDirectory ="\\cache"};List documents =newList();if(!Directory.Exists(OutputDir))Directory.CreateDirectory(OutputDir);LoadDocumentOptions options =newLoadDocumentOptions{Cache = cache,LoadAttachmentsMode = DocumentLoadAttachmentsMode.AsAttachments};LEADDocument document = DocumentFactory.LoadFromFile(@"FILE PATH TO PDF WITH ATTACHMENTS", options);if(document.Pages.Count > 0)documents.Add(document);foreach(DocumentAttachment attachmentindocument.Attachments){LoadAttachmentOptions attachmentOptions =newLoadAttachmentOptions { AttachmentNumber = attachment.AttachmentNumber, };LEADDocument loadDocument = document.LoadDocumentAttachment(attachmentOptions);documents.Add(loadDocument);}ConvertDocuments(documents, RasterImageFormat.Png);}

Inside theProgramclass, add a new method namedConvertDocuments(IEnumerable documents, RasterImageFormat imageFormat). This method will be called inside theExtractPDFAttachments()method, as shown above. Add the code below to theConvertDocuments()method to convert the PDF attachments to PNG files.

C#
staticvoidConvertDocuments(IEnumerable documents, RasterImageFormat imageFormat){DocumentConverter converter =newDocumentConverter();foreach(LEADDocument documentindocuments){stringname =string.IsNullOrEmpty(document.Name) ?"DocumentAttachment": document.Name;stringoutputFile = Path.Combine(OutputDir, $"{name}.{RasterCodecs.GetExtension(imageFormat)}");intcount = 1;while(File.Exists(outputFile))outputFile = Path.Combine(OutputDir, $"{name}({count++}).{RasterCodecs.GetExtension(imageFormat)}");DocumentConverterJobData jobData =newDocumentConverterJobData{Document = document,Cache = cache,DocumentFormat = DocumentFormat.User,RasterImageFormat = imageFormat,RasterImageBitsPerPixel = 0,OutputDocumentFileName = outputFile,};DocumentConverterJob job = converter.Jobs.CreateJob(jobData);converter.Jobs.RunJob(job);if(job.Errors.Count > 0)foreach(varerrorinjob.Errors)Console.WriteLine($在转换”的错误:{error.Error.Message} \ n”);elseConsole.WriteLine($"Successfully Converted to {outputFile}...\n");}}

Run the Project

Run the project by pressingF5, or by selectingDebug -> Start Debugging.

If the steps were followed correctly, the application runs and converts all the attachments in the given PDF file to separate PNG files.

Wrap-up

This tutorial showed how to extract PDF attachments using theLoadDocumentAttachment()method and convert them to raster images. Also, it covered how to use theLEADDocument,DocumentConverter, andLoadAttachmentOptionsclasses.

See Also

iOS
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2021LEAD Technologies, Inc.All Rights Reserved.