医疗影像工具LEADTOOLS 入门教程: 使用文档编写器创建文档 – 控制台 C#

本教程展示了如何在 C# Windows 控制台应用程序中使用 LEADTOOLS SDK 创建一个新的 PDF 文档并将 PDF 文件的第一页添加到其中。

本教程展示了如何在 C# Windows 控制台应用程序中使用 LEADTOOLS SDK 创建一个新的 PDF 文档并将 PDF 文件的第一页添加到其中。

概述
概括 本教程介绍如何在 C# Windows 控制台应用程序中使用 SVG 创建新的 PDF 文档并向其添加页面
完成时间 30分钟
视觉工作室项目 下载教程项目 (3 KB)
平台 C# Windows 控制台应用程序
集成开发环境 视觉工作室 2017、2019
开发许可 LEADTOOLS
用另一种语言试试
  • C# :.NET 框架(控制台)
  • 苹果:macOS
  • C API:CDLL (Windows)
  • java
所需知识

在使用文件观察器转换文件 – C# .NET Core教程之前,通过查看添加引用和设置许可证教程熟悉创建项目的基本步骤。

创建项目并添加 LEADTOOLS 引用

从添加引用和设置许可证教程中创建的项目副本开始。如果您没有该项目,请按照该教程中的步骤创建它。

所需的参考取决于项目的目的。可以通过本地 DLL 引用添加引用。

本教程需要以下本地 DLL,它们位于<INSTALL_DIR>LEADTOOLS22BinDotnet4x64:

  • Interop.LMDSKernelLib2.dll
  • Interop.LMVResizeLib.dll
  • Leadtools.dll
  • Leadtools.Codecs.dll
  • Leadtools.Drawing.dll
  • Leadtools.ImageProcessing.Color.dll
  • Leadtools.ImageProcessing.Core.dll
  • Leadtools.Multimedia.dll

还需要以下非 LEADTOOLS DLL:

  • System.Deployment.dll
  • System.Drawing.dll
  • System.Windows.Forms
  • stdole.dll

有关特定功能所需的 DLL 的完整列表,请参阅要包含在您的应用程序中的文件。

不同的 SDK 功能需要不同的引用。有关完整列表,请参阅您的申请中必须包括的多媒体文件。

设置许可证文件

许可证解锁项目所需的功能。它必须在调用任何工具包函数之前设置。有关详细信息,包括针对不同平台的教程,请参阅设置运行时许可证。

有两种类型的运行时许可证:

  • 评估许可证,在下载评估工具包时获得。它允许评估工具包。
  • 部署许可证。如果需要部署许可证文件和开发人员密钥,请参阅获取许可证。

笔记

添加 LEADTOOLS NuGet 引用和设置许可证在添加引用和设置许可证教程 中有更详细的介绍。

创建一个新的 PDF 文档并添加页面代码

创建项目、添加参考和设置许可证后,就可以开始编码了。

在解决方案资源管理器中,打开Program.cs。添加一个新方法 called并在underCreatePdfDocument()的方法内部调用它。添加以下代码以创建新的 PDF 文件,并将给定目录中每个 PDF 的第一页添加到新 PDF 中。Main()SetLicense();

【C#】

// Add to using blockusing System;using System.IO;using Leadtools;using Leadtools.Codecs;using Leadtools.Document.Writer;

【C#】

static void CreatePdfDocument(){using (RasterCodecs codecs = new RasterCodecs()){string dir = @"C:LEADTOOLS22ResourcesImages";int pageNumber = 1;string[] pdfFiles = Directory.GetFiles( dir, "*.pdf");DocumentFormat format = DocumentFormat.Pdf;string outFile = Path.Combine(dir, "DocumentWriters." + DocumentWriter.GetFormatFileExtension(format));codecs.Options.RasterizeDocument.Load.Resolution = 300;DocumentWriter docWriter = new DocumentWriter();PdfDocumentOptions pdfOptions = docWriter.GetOptions(format) as PdfDocumentOptions;pdfOptions.DocumentType = PdfDocumentType.PdfA;pdfOptions.ImageOverText = true;docWriter.SetOptions(format, pdfOptions);// Begin a new PDF documentdocWriter.BeginDocument(outFile, format);// Add the pagesforeach (string file in pdfFiles){DocumentWriterSvgPage page = new DocumentWriterSvgPage();page.SvgDocument = codecs.LoadSvg(file, pageNumber, null);if (pdfOptions.ImageOverText){// If we are using image/text, then load the overlay raster imagepage.Image = codecs.Load(file, pageNumber);}// Add the page to the created PDF documentdocWriter.AddPage(page);Console.WriteLine($"Added page {pageNumber} from {Path.GetFileNameWithoutExtension(file)}n");// Dispose of resourcesif (page.SvgDocument != null)page.SvgDocument.Dispose();if (page.Image != null)page.Image.Dispose();}// Finalized document to diskdocWriter.EndDocument();Console.WriteLine("PDF document saved successfully!");}}
运行项目

按F5或选择Debug -> Start Debugging运行项目。

如果正确执行了这些步骤,应用程序将运行并创建一个新的 PDF 文件,并使用 SVG 和 Document Writers 在给定目录中添加每个 PDF 文件的第一页。

以上便是使用文档编写器创建文档,如果您还有其他疑问,欢迎咨询我们或者加入我们官方技术交流群。


欢迎下载|体验更多LEADTOOL产品

您还可以加入产品:


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年11月19日
下一篇 2022年11月19日

相关推荐

发表回复

登录后才能评论