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 寫入到新添加的頁(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ì)寫入流要進(jìn)行pdfWriter.Flush()將緩沖區(qū)數(shù)據(jù)寫入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)文章
C#生成互不相同隨機(jī)數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C#生成互不相同隨機(jī)數(shù)的實(shí)現(xiàn)方法,文中詳細(xì)描述了C#生成互不相同隨機(jī)數(shù)的各個(gè)步驟及所用到的函數(shù),非常具有借鑒價(jià)值,需要的朋友可以參考下2014-09-09
C# 獲取硬盤號(hào),CPU信息,加密解密技術(shù)的步驟
這篇文章主要介紹了C# 獲取硬盤號(hào),CPU信息,加密解密技術(shù)的步驟,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2021-01-01
C#對(duì)文件進(jìn)行批量重命名或者對(duì)某單個(gè)文件進(jìn)行改名的示例代碼
這篇文章主要介紹了C#對(duì)文件進(jìn)行批量重命名或者對(duì)某個(gè)單獨(dú)的文件進(jìn)行改名的實(shí)現(xiàn)方法,文中有相關(guān)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-05-05

