LEADTOOLS 是一个综合工具包的集合,用于将识别、文档、医疗、成像和多媒体技术整合到桌面、服务器、平板电脑、 络和移动解决方案中,是一项企业级文档自动化解决方案,有捕捉,OCR,OMR,表单识别和处理,PDF,打印捕获,归档,注释和显示功能。利用业界领先的图像处理技术,能够智能识别文件,可以用来识别任何类型的扫描或传真形式的图像。
本教程展示了如何LEADDocument使用 LEADTOOLS SDK 在 C# .NET Core 应用程序中向 a 添加页面和从中删除页面。
概括 | 本教程介绍如何在 C# .NET Core 控制台应用程序中修改DocumentPagesa 。LEADDocument |
完成时间 | 30分钟 |
项目 | 下载教程项目 (2 KB) |
平台 | C# .NET Core 控制台应用程序 |
IDE | 视觉工作室 2019、2022 |
开发许可证 | 下载 LEADTOOLS |
在学习从 LEADDocument中添加和删除页面 – C# .NET Core 教程之前,通过查看添加引用和设置许可教程来熟悉创建项目的基本步骤。
创建项目并添加 LEADTOOLS 参考
从添加引用和设置许可证教程中创建的项目的副本开始。如果您没有该项目,请按照该教程中的步骤创建它。
所需的参考资料取决于项目的目的。可以通过 NuGet 包添加引用。
本教程需要以下 NuGet 包:
- Leadtools.Document.Sdk
有关您的应用程序需要哪些 DLL 文件的完整列表,请参阅您的应用程序中包含的文件。
设置许可文件
许可证解锁项目所需的功能。它必须在调用任何工具包函数之前设置。有关详细信息,包括针对不同平台的教程,请参阅设置运行时许可证。
有两种类型的运行时许可证:
- 评估许可证,在下载评估工具包时获得。它允许评估工具包。
- 部署许可证。如果需要部署许可证文件和开发人员密钥,请参阅获取许可证。
创建 LEADDocument 代码
创建项目、添加参考和许可证集后,就可以开始编码了。
在解决方案资源管理器中,打开Program.cs. 将以下语句添加using到Program.cs.
【C#】
// Using block at the topusing System;using Leadtools;using Leadtools.Document;using Leadtools.Document.Converter;
将以下全局变量添加到Program类中。
【C#】
static LEADDocument document = DocumentFactory.Create(new CreateDocumentOptions() { UseCache = false });
使用将创建一个虚拟文档,DocumentFactory.Create()允许修改Pages. LEADDocument相反,LEADDocument使用DocumentFactory.LoadFromFile()方法创建实例将导致只读实例。
Program.cs在namedInsertPageFromFile(string fileName, int pageNumber)和中添加两个方法RemovePageFromFile(int pageNumber)。Main()在里面调用这两种方法SetLicense()。
添加以下代码以添加以下功能:
- 从文件加载源文档并将这些页面添加到虚拟文档。
- 在pageFile字符串变量中指定的文件中插入来自不同文档的页面。
- 从虚拟文档中删除指定的页码。
- 通过LEADDocument使用DocumentConverter.
【C#】
static void Main(string[] args){try{string filename = @"PATH TO PDF FILE";string pageFile = @"PATH TO PAGE TO BE ADDED";string outputFile = @"PATH TO PDF OUTPUT";int insertPageNumber = 0;int removePageNumber = 1;if (!SetLicense())Console.WriteLine("Error setting license");elseConsole.WriteLine("License file set successfully");LEADDocument loadedDocument = DocumentFactory.LoadFromFile(filename, new LoadDocumentOptions { UseCache = false });for (int i = 0; i < loadedDocument.Pages.Count; i++){document.Pages.Add(loadedDocument.Pages[i]);}if (document != null){// Insert and remove pagesInsertPageFromFile(pageFile, insertPageNumber);RemovePageFromFile(removePageNumber);// Save modified resultDocumentConverter docConverter = new DocumentConverter();docConverter.SetDocumentWriterInstance(new Leadtools.Document.Writer.DocumentWriter());var jobData = new DocumentConverterJobData{Document = document,OutputDocumentFileName = outputFile,DocumentFormat = Leadtools.Document.Writer.DocumentFormat.Pdf};var job = docConverter.Jobs.CreateJob(jobData);docConverter.Jobs.RunJob(job);if (job.Errors.Count != 0)Console.WriteLine(String.Format("Modified document saved to {0}", outputFile));else foreach (var error in job.Errors)Console.WriteLine($"There was an error:{error.Error}");}}catch (Exception ex){Console.WriteLine(ex.Message);}}
虚拟文档是LEADDocument存在于内存中的对象,用于构造由来自各种文档文件的页面组成的文档。一旦您准备好将虚拟文档制作为物理文档文件,或“完成”它,您可以在 中设置LEADDocument,DocumentViewer或使用DocumentConverter该类。
添加插入页面代码
在InsertPageFromFile()方法内部,添加以下代码以加载“子”文档并将第一页添加到指定位置的虚拟文档中。
【C#】
static void InsertPageFromFile(string fileName, int pageNumber){// Check if pageNumber is validif (pageNumber < 0 || pageNumber > document.Pages.Count)return;LEADDocument childDocument = DocumentFactory.LoadFromFile(fileName, new LoadDocumentOptions { UseCache = false });document.Pages.Insert(pageNumber, childDocument.Pages[0]);Console.WriteLine(String.Format("Image {0} inserted as page {1} to loaded document", fileName, pageNumber));}
添加删除页面代码
将以下代码添加到RemovePageFromFile()方法中,以删除指定位置的虚拟文档内的页面。
【C#】
static void RemovePageFromFile(int pageNumber){// Check if pageNumber is validif (document.Pages.Count < 2 || pageNumber > document.Pages.Count)return;document.Pages.RemoveAt(pageNumber);Console.WriteLine(String.Format("Page {0} removed from loaded document", pageNumber));}
处理流
如果您的项目需要通过内存流读取和写入文档,则可以这样做:
【C#】
public void DocumentFactoryLoadFromStreamExample(){// Get a stream to anything, in this case a file// Note that the stream must be seekablevar fileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf");using (var stream = File.OpenRead(fileName)){// We must keep the stream alive as long as the document is alivevar options = new LoadDocumentOptions();using (var document = DocumentFactory.LoadFromStream(stream, options)){Console.WriteLine(document.DocumentId);}}}static class LEAD_VARS{public const string ImagesDir = @"C:LEADTOOLS22ResourcesImages";}
按F5或选择Debug -> Start Debugging运行项目。
如果正确执行了这些步骤,应用程序将加载文档,将不同文件中的页面插入指定位置,删除指定位置的页面,并将修改后的虚拟文档保存为 PDF。
以上便是如何添加引用并设置许可证 – C# .NET Core,如果您还有其他疑问,欢迎咨询我们或者加入我们官方技术交流群。
欢迎下载|体验更多LEADTOOL产品
您还可以加入产品:
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!