C#利用itext實現(xiàn)PDF頁面處理與切分
一、itext
我要使用itext做一個pdf的頁面大小一致性處理,然后再根據(jù)數(shù)據(jù)切分出需要的pdf.
iText的官網(wǎng)有關(guān)于它的介紹, 然后在官網(wǎng)可以查找api文檔。
其中我要使用的是itext7+,主要在iText.Kernel.Pdf 命名空間下。

二、處理PDF頁面大小一致
由于原始PDF 是掃描圖片合成來的,有些頁面掃描的圖片規(guī)格不一致,導(dǎo)致pdf閱讀性很差。

對于這個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();
}
}
思路:遍歷原來的PDF頁碼,將原來的PDF頁碼對象拷貝PdfFormXObject到要生成的PDF文檔中,首先要copy頁面對象才能使用,不然直接獲取的page對象是原來文檔的,我們無法操作。
var formXObject = page.CopyAsFormXObject(outPDfDoc);
然后對頁面進(jìn)行縮放計算,我們新的PDF默認(rèn)設(shè)置成A3大小,通過計算原始頁面和新頁面寬高比例進(jìn)行縮放。
計算完成后,在新文檔中使用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();
}
}
}
注意:對寫入流要進(jìn)行pdfWriter.Flush()將緩沖區(qū)數(shù)據(jù)寫入PDF后再關(guān)。
以上就是C#利用itext實現(xiàn)PDF頁面處理與切分的詳細(xì)內(nèi)容,更多關(guān)于C# PDF頁面處理 切分的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
VS?Code里使用Debugger?for?Unity插件調(diào)試的方法(2023最新版)
Debugger for Unity是一個非正式支持的,官方推薦的,應(yīng)用最廣的,Visual Studio Code上的Unity調(diào)試插件,這篇文章主要介紹了VS?Code里使用Debugger?for?Unity插件進(jìn)行調(diào)試(2023最新版),需要的朋友可以參考下2023-02-02
C#中調(diào)用Windows API的技術(shù)要點說明
本篇文章主要是對C#中調(diào)用Windows API的技術(shù)要點進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
json格式數(shù)據(jù)分析工具PageElement類分享(仿Session寫法)
json格式數(shù)據(jù)分析工具PageElement類分享,可像Session一樣自由獲取Json元素的Key與Value。并可方便與ADO進(jìn)行交互2013-12-12

