我在處理圖像PDF文件時遇到問題(PDF文件僅包含圖像,沒有文本)有兩個PDF文件img1,img2,我想將它們中的兩個合併合併成一個A4頁面的PDF文件。合併PDF文件並用ITextSharp刪除空格
我試過下面的代碼。
string Img1 = "C:/temp/image1.pdf";
string Img2 = "C:/temp/image2.pdf";
string MergedFile = "C:/temp/Combo.pdf";
//Create our PDF readers
PdfReader r1 = new PdfReader(Img1);
PdfReader r2 = new PdfReader(Img2);
//Our new page size, an A3 in landscape mode
iTextSharp.text.Rectangle NewPageSize = PageSize.A3.Rotate();
using (FileStream fs = new FileStream(MergedFile, FileMode.Create,
FileAccess.Write, FileShare.None))
{
//Create our document without margins
using (Document doc = new Document(NewPageSize, 0, 0, 0, 0))
{
using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
{
doc.Open();
//Get our imported pages
PdfImportedPage imp1 = w.GetImportedPage(r1, 1);
PdfImportedPage imp2 = w.GetImportedPage(r2, 1);
//Add them to our merged document at specific X/Y coords
**w.DirectContent.AddTemplate(imp1, 0, 0);
w.DirectContent.AddTemplate(imp2, 0, -350);**
doc.Close();
}
}
}
r1.Close();
r2.Close();
所以,當我執行上面的代碼,因爲我已經提到在y座標,它會結合PDF和兩個圖像將只有一個頁面上。
但我並不想這樣做,
我在這裏只是給例如兩個圖像,但在實際有超過20幅圖像(轉換成PDF文件)。
因此,根據圖像大小,它應該結合文件。我不能給每個文件的每個n修復y座標
任何人都可以請幫助我將多個PDF合併成單個沒有空白空間..?
那麼原始PDF中是否有空格,並且您希望僅裁剪圖像然後合併? –
@ChrisHaas是的,正好 – User5590
* BUt我不想這樣做* - 你只是不想這樣做,因爲你不知道** y **座標?或者還有其他原因?如果僅僅是因爲** y **座標,則可以將iTextSharp解析器類應用於源頁面以確定每個頁面的** y **座標範圍,並從中計算出** y ** 'AddTemplate'調用。 – mkl