C#利用itext實(shí)現(xiàn)PDF頁(yè)面處理與切分
一、itext
我要使用itext做一個(gè)pdf的頁(yè)面大小一致性處理,然后再根據(jù)數(shù)據(jù)切分出需要的pdf.
iText的官網(wǎng)有關(guān)于它的介紹, 然后在官網(wǎng)可以查找api文檔。
其中我要使用的是itext7+,主要在iText.Kernel.Pdf
命名空間下。
二、處理PDF頁(yè)面大小一致
由于原始PDF 是掃描圖片合成來(lái)的,有些頁(yè)面掃描的圖片規(guī)格不一致,導(dǎo)致pdf閱讀性很差。
對(duì)于這個(gè)pdf我進(jìn)行處理,首先是在nuget 里面搜索 itext 進(jìn)行安裝,使用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(); } }
思路:遍歷原來(lái)的PDF頁(yè)碼,將原來(lái)的PDF頁(yè)碼對(duì)象拷貝PdfFormXObject
到要生成的PDF文檔中,首先要copy頁(yè)面對(duì)象才能使用,不然直接獲取的page對(duì)象是原來(lái)文檔的,我們無(wú)法操作。
var formXObject = page.CopyAsFormXObject(outPDfDoc);
然后對(duì)頁(yè)面進(jìn)行縮放計(jì)算,我們新的PDF默認(rèn)設(shè)置成A3大小,通過(guò)計(jì)算原始頁(yè)面和新頁(yè)面寬高比例進(jìn)行縮放。
計(jì)算完成后,在新文檔中使用PdfCanvas
對(duì)象新添加一頁(yè),然后將PdfFormXObject
寫(xiě)入到新添加的頁(yè)中。
處理后的PDF:
三、切分PDF
切分PDF 就比較簡(jiǎn)單了,直接從原始文件中拷貝頁(yè)面到新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(); } } }
注意:對(duì)寫(xiě)入流要進(jìn)行pdfWriter.Flush()
將緩沖區(qū)數(shù)據(jù)寫(xiě)入PDF后再關(guān)。
以上就是C#利用itext實(shí)現(xiàn)PDF頁(yè)面處理與切分的詳細(xì)內(nèi)容,更多關(guān)于C# PDF頁(yè)面處理 切分的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
VS?Code里使用Debugger?for?Unity插件調(diào)試的方法(2023最新版)
Debugger for Unity是一個(gè)非正式支持的,官方推薦的,應(yīng)用最廣的,Visual Studio Code上的Unity調(diào)試插件,這篇文章主要介紹了VS?Code里使用Debugger?for?Unity插件進(jìn)行調(diào)試(2023最新版),需要的朋友可以參考下2023-02-02C#中File和FileStream的簡(jiǎn)單介紹和用法
這篇文章主要給大家介紹了關(guān)于C#中File和FileStream用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01C#中調(diào)用Windows API的技術(shù)要點(diǎn)說(shuō)明
本篇文章主要是對(duì)C#中調(diào)用Windows API的技術(shù)要點(diǎn)進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01json格式數(shù)據(jù)分析工具PageElement類分享(仿Session寫(xiě)法)
json格式數(shù)據(jù)分析工具PageElement類分享,可像Session一樣自由獲取Json元素的Key與Value。并可方便與ADO進(jìn)行交互2013-12-12C#實(shí)現(xiàn)的AES加密解密完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的AES加密解密,結(jié)合完整實(shí)例形式分析了C#實(shí)現(xiàn)的AES算法進(jìn)行加密與解密的相關(guān)技巧,需要的朋友可以參考下2016-07-07