[英]ITextSharp - merge two pdfs in a single page
我將簡單地提出這個問題。
我有這個pdf:
_____
|abcd |
| |
| |
|_____|
還有這個:
_____
|1234 |
|4567 |
| |
|_____|
我想將它們合並以獲得:
_____
|abcd |
|1234 |
|4567 |
|_____|
可以使用iTextSharp或任何其他免費工具嗎?
提前致謝
這是一個老問題...但如果有人再次進入這里,我的解決方案就是這個...我做了這個硬編碼將兩頁翻譯成一頁,所以這是基礎知識首先我旋轉了兩個PDF,之后我將它們合並在一起
旋轉這兩個頁面使用這個:
public static void RotatePDF(string inputFile, string outputFile)
{
using (FileStream outStream = new FileStream(outputFile, FileMode.Create))
{
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(inputFile);
iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outStream);
iTextSharp.text.pdf.PdfDictionary pageDict = reader.GetPageN(1);
int desiredRot = 90; // 90 degrees clockwise from what it is now
iTextSharp.text.pdf.PdfNumber rotation = pageDict.GetAsNumber(iTextSharp.text.pdf.PdfName.ROTATE);
if (rotation != null)
{
desiredRot += rotation.IntValue;
desiredRot %= 360; // must be 0, 90, 180, or 270
}
pageDict.Put(iTextSharp.text.pdf.PdfName.ROTATE, new iTextSharp.text.pdf.PdfNumber(desiredRot));
stamper.Close();
}
}
現在你可以將它們合並在一起:
public static void MergeTwoPdfsToSingle(string inputFile1, string inputFile2, string outputFile)
{
//Step 1: Create a Docuement-Object
Document document = new Document();
try
{
//Step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFile, FileMode.Create));
//Step 3: Open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page1;
PdfImportedPage page2;
// we create a reader for the document
PdfReader reader1 = new PdfReader(inputFile1);
PdfReader reader2 = new PdfReader(inputFile2);
document.SetPageSize(reader1.GetPageSizeWithRotation(1));
document.NewPage();
page1 = writer.GetImportedPage(reader1, 1);
page2 = writer.GetImportedPage(reader2, 1);
cb.AddTemplate(page1, 0, 0);
//play around to find the exact location for the next pdf
cb.AddTemplate(page2, 0, 300);
}
catch (Exception e) { throw e; }
finally { document.Close(); }
}
是的......即使對於PDF專家來說,它也很難。 通過提問,你已經表明你不是一個......至少現在還沒有。 把它拉下來,你會很順利......但是:
沒有簡單的方法來確定圍繞給定頁面上所有內容的邊界框。 com.itextpdf.text.pdf.parser(或它的#等價物)有幾個類可以幫助你一路走下去,但最重要的是PDF不是像這樣可以解析的。
我強烈建議你嘗試其他方法。 任何涉及短語“然后我們從PDF中獲取信息”的內容都需要進行徹底檢查。 哦,它可能,但幾乎總是一個更好的方法來做到這一點。
我們使用了一種名為PDFMerger的產品。 然而它並不便宜。 我們並沒有真正找到任何其他可以輕易實現的目標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.