本教程展示如何提取附件公司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 |
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.
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:
Leadtools.Document.Sdk
If using local DLL references, the following DLLs are needed.
The DLLs are located at
:
Leadtools.dll
Leadtools.Caching.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Codecs.Png.dll
Leadtools.Document.dll
Leadtools.Document.Converter.dll
Leadtools.Document.Pdf.dll
Leadtools.Document.Raster.dll
Leadtools.Document.Writer.dll
Leadtools.Pdf.dll
For a complete list of which DLL files are required for your application, refer toFiles to be Included in your Application.
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.
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
:
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 theProgram
class.
staticFileCache cache;
staticstringOutputDir ="Output";
Create a new method insideProgram.cs
namedExtractPDFAttachments()
. Call the method in theMain()
method, under the set license call, as shown below.
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.
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 theProgram
class, add a new method namedConvertDocuments(IEnumerable
. 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.
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”);
else
Console.WriteLine($"Successfully Converted to {outputFile}...\n");
}
}
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.
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
, andLoadAttachmentOptions
classes.