C#利用itext實現PDF頁面處理與切分
一、itext
我要使用itext做一個pdf的頁面大小一致性處理,然后再根據數據切分出需要的pdf.
iText的官網有關于它的介紹, 然后在官網可以查找api文檔。
其中我要使用的是itext7+,主要在iText.Kernel.Pdf
命名空間下。
二、處理PDF頁面大小一致
由于原始PDF 是掃描圖片合成來的,有些頁面掃描的圖片規(guī)格不一致,導致pdf閱讀性很差。
對于這個pdf我進行處理,首先是在nuget 里面搜索 itext 進行安裝,使用itext7。
處理PDF大小方法:
public void RestPageSize(string sourcePdfPath, string outputPdfPath) { PdfReader pdfReader = null; PdfDocument pdfDocument = null; PdfWriter pdfWriter = null; PdfDocument outPDfDoc = null; try { pdfReader = new PdfReader(sourcePdfPath); pdfDocument = new PdfDocument(pdfReader); var outDir = System.IO.Path.GetDirectoryName(outputPdfPath); if (!Directory.Exists(outDir)) { Directory.CreateDirectory(outDir); } pdfWriter = new PdfWriter(outputPdfPath); outPDfDoc = new PdfDocument(pdfWriter); outPDfDoc.SetDefaultPageSize(PageSize.A3); for (int i = 1; i < pdfDocument.GetNumberOfPages() + 1; i++) { var page = pdfDocument.GetPage(i); var formXObject = page.CopyAsFormXObject(outPDfDoc); var xPercent = PageSize.A3.GetWidth() / page.GetPageSize().GetWidth(); var yPercent = PageSize.A3.GetHeight() / page.GetPageSize().GetHeight(); PdfCanvas pdfCanvas = new PdfCanvas(outPDfDoc.AddNewPage()); pdfCanvas.AddXObjectWithTransformationMatrix(formXObject, xPercent, 0, 0, yPercent, 0, 0); } pdfWriter.Flush(); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (pdfReader != null) { pdfReader.Close(); } if (pdfDocument != null) { pdfDocument.Close(); } if (outPDfDoc != null) { outPDfDoc.Close(); } if (pdfWriter != null) { pdfWriter.Close(); pdfWriter.Dispose(); } }
思路:遍歷原來的PDF頁碼,將原來的PDF頁碼對象拷貝PdfFormXObject
到要生成的PDF文檔中,首先要copy頁面對象才能使用,不然直接獲取的page對象是原來文檔的,我們無法操作。
var formXObject = page.CopyAsFormXObject(outPDfDoc);
然后對頁面進行縮放計算,我們新的PDF默認設置成A3大小,通過計算原始頁面和新頁面寬高比例進行縮放。
計算完成后,在新文檔中使用PdfCanvas
對象新添加一頁,然后將PdfFormXObject
寫入到新添加的頁中。
處理后的PDF:
三、切分PDF
切分PDF 就比較簡單了,直接從原始文件中拷貝頁面到新PDF文檔中就行了。
切分PDF 方法:
public void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage) { PdfReader pdfReader = null; PdfDocument pdfDocument = null; PdfWriter pdfWriter = null; PdfDocument outPDfDoc = null; try { pdfReader = new PdfReader(sourcePdfPath); pdfDocument = new PdfDocument(pdfReader); var outDir = Path.GetDirectoryName(outputPdfPath); if (!Directory.Exists(outDir)) { Directory.CreateDirectory(outDir); } pdfWriter = new PdfWriter(outputPdfPath); outPDfDoc = new PdfDocument(pdfWriter); pdfDocument.CopyPagesTo(startPage, endPage, outPDfDoc); pdfWriter.Flush(); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (pdfReader != null) { pdfReader.Close(); } if (pdfDocument != null) { pdfDocument.Close(); } if (outPDfDoc != null) { outPDfDoc.Close(); } if (pdfWriter != null) { pdfWriter.Close(); pdfWriter.Dispose(); } } }
注意:對寫入流要進行pdfWriter.Flush()
將緩沖區(qū)數據寫入PDF后再關。
以上就是C#利用itext實現PDF頁面處理與切分的詳細內容,更多關于C# PDF頁面處理 切分的資料請關注腳本之家其它相關文章!
相關文章
VS?Code里使用Debugger?for?Unity插件調試的方法(2023最新版)
Debugger for Unity是一個非正式支持的,官方推薦的,應用最廣的,Visual Studio Code上的Unity調試插件,這篇文章主要介紹了VS?Code里使用Debugger?for?Unity插件進行調試(2023最新版),需要的朋友可以參考下2023-02-02json格式數據分析工具PageElement類分享(仿Session寫法)
json格式數據分析工具PageElement類分享,可像Session一樣自由獲取Json元素的Key與Value。并可方便與ADO進行交互2013-12-12