翻譯|使用教程|編輯:胡濤|2022-12-08 14:54:47.363|閱讀 159 次
概述:本教程介紹如何使用 LEADTOOLS SDK 在 C# .NET Core 應用程序中創建新的空白 PDF 文檔并從現有 PDF 文件向其添加頁面。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
LEADTOOLS是一個綜合工具包的集合,用于將識別、文檔、醫療、成像和多媒體技術整合到桌面、服務器、平板電腦、網絡和移動解決方案中,是一項企業級文檔自動化解決方案,有捕捉,OCR,OMR,表單識別和處理,PDF,打印捕獲,歸檔,注釋和顯示功能。利用業界領先的圖像處理技術,能夠智能識別文件,可以用來識別任何類型的掃描或傳真形式的圖像。
本教程介紹如何使用 LEADTOOLS SDK 在 C# .NET Core 應用程序中創建新的空白 PDF 文檔并從現有 PDF 文件向其添加頁面。
概述 | |
---|---|
概括 | 本教程介紹如何在 C# .NET Core 控制臺應用程序中使用 LEADTOOLS 文檔編寫器創建新的 PDF 文檔并向其添加頁面。 |
完成時間 | 30分鐘 |
視覺工作室項目 | |
平臺 | C# .NET Core 控制臺應用程序 |
集成開發環境 | 視覺工作室 2019、2022 |
開發許可 | LEADTOOLS |
用另一種語言試試 |
|
在使用文件觀察器轉換文件 - C# .NET Core教程之前,通過查看添加引用和設置許可證教程熟悉創建項目的基本步驟。
從添加引用和設置許可證教程中創建的項目副本開始。如果您沒有該項目,請按照該教程中的步驟創建它。
所需的參考取決于項目的目的。可以通過 NuGet 包添加引用。
本教程需要以下 NuGet 包:
有關您的應用程序需要哪些 DLL 文件的完整列表,請參閱中的文件。
設置許可證文件許可證解鎖項目所需的功能。它必須在調用任何工具包函數之前設置。有關詳細信息,包括針對不同平臺的教程,請參閱設置運行時許可證。
有兩種類型的運行時許可證:
筆記
添加 LEADTOOLS NuGet 引用和設置許可證在添加引用和設置許可證教程 中有更詳細的介紹。
創建項目、添加參考和設置許可證后,就可以開始編碼了。
在解決方案資源管理器中,打開Program.cs。將以下語句添加到using頂部的塊中Program.cs。
【C?!?
using System; using System.IO; using Leadtools; using Leadtools.Codecs; using Leadtools.Document; using Leadtools.Document.Writer;
添加一個名為 的新方法CreatePdfDocument()。Main()在設置的許可證代碼下的方法中調用這個新方法。添加以下代碼以創建一個新的 PDF 文件,并將給定目錄中每個 PDF 的第一頁添加到其中。
【C#】
static void CreatePdfDocument() { using (RasterCodecs codecs = new RasterCodecs()) { string dir = @"C:\LEADTOOLS22\Resources\Images"; 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 document docWriter.BeginDocument(outFile, format); // Add the pages foreach (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 image page.Image = codecs.Load(file, pageNumber); } // Add the page to the created PDF document docWriter.AddPage(page); Console.WriteLine($"Added page {pageNumber} from {Path.GetFileNameWithoutExtension(file)}\n"); // Dispose of resources if (page.SvgDocument != null) page.SvgDocument.Dispose(); if (page.Image != null) page.Image.Dispose(); } // Finalized document to disk docWriter.EndDocument(); Console.WriteLine("PDF document saved successfully!"); } }
如果您想使用內存流加載文檔,請將以下代碼添加到CreatePdfDocument()下面的方法中string[] pdfFiles = Directory.GetFiles( dir, "*.pdf");:
【C?!?
var fileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf"); using(var stream = File.OpenRead(fileName)) { var options = new LoadDocumentOptions(); using (var document = DocumentFactory.LoadFromStream(stream, options)) { Console.WriteLine(document.DocumentId); Console.WriteLine("Document loaded"); } }
您需要引用該類LEAD_VARS才能訪問ImagesDir. 為此,將以下類添加到程序中:
【C#】
static class LEAD_VARS { public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; }
按F5或選擇Debug -> Start Debugging運行項目。
如果正確執行了這些步驟,則會出現控制臺,應用程序會創建一個新的 PDF 文件,并使用 SVG 和 Document Writers 在給定目錄中添加每個 PDF 文件的第一頁。
以上便是從多個圖像創建多頁文件 - C# .NET Core ,如果您還有其他疑問,歡迎咨詢我們或者加入我們官方技術交流群。
歡迎下載|體驗更多LEADTOOL產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn