網站首頁 編程語言 正文
一、itext
我要使用itext做一個pdf的頁面大小一致性處理,然后再根據數據切分出需要的pdf.
iText的官網有關于它的介紹,?然后在官網可以查找api文檔。
其中我要使用的是itext7+,主要在iText.Kernel.Pdf
?命名空間下。
二、處理PDF頁面大小一致
由于原始PDF 是掃描圖片合成來的,有些頁面掃描的圖片規格不一致,導致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()
將緩沖區數據寫入PDF后再關。
原文鏈接:https://www.cnblogs.com/SunSpring/p/16193835.html
相關推薦
- 2024-03-21 SpringBoot +MyBatis批量插入數據
- 2022-08-22 探究C#訪問null字段會拋異常原因_C#教程
- 2022-03-15 has been blocked by CORS policy: Response to prefl
- 2022-09-24 python實現字母閃爍效果的示例代碼_python
- 2022-01-05 實體類[notmapped]特殊 “The specified type member ‘‘ is
- 2022-12-05 Golang中的錯誤處理的示例詳解_Golang
- 2023-04-12 Blazor實現組件嵌套傳遞值的示例詳解_其它綜合
- 2022-06-20 詳解Go?將在下個版本支持新型排序算法pdqsort_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支