我們有ASP.NET應用程序,其中,我試圖合併兩個PDF文件,並且我們正在使用BCL easyPDF7庫。我想在特定位置或頁面合併新文件(例如,在主文檔中的第3頁之後)。但是我發現在該庫中的合併只是將文件附加到最後。在特定頁面或位置合併PDF文件ASP.NET
我們決定使用新的工具PDF4NET,並且我從PDF4NET的示例代碼中觀察到他們還提供了合併功能,最終附加了文檔。
有什麼辦法可以做到這一點? (通過PDF4NET或BCL easyPDF7)請分享您的觀點。
我們有ASP.NET應用程序,其中,我試圖合併兩個PDF文件,並且我們正在使用BCL easyPDF7庫。我想在特定位置或頁面合併新文件(例如,在主文檔中的第3頁之後)。但是我發現在該庫中的合併只是將文件附加到最後。在特定頁面或位置合併PDF文件ASP.NET
我們決定使用新的工具PDF4NET,並且我從PDF4NET的示例代碼中觀察到他們還提供了合併功能,最終附加了文檔。
有什麼辦法可以做到這一點? (通過PDF4NET或BCL easyPDF7)請分享您的觀點。
我通過提取頁面和創建一個新文件來實現它。我從主文件提取頁面索引,這是我想插入我的第二個文件的頁碼。
希望它可以幫助誰在處理PDF4NET並希望合併文件在特定的頁碼。
private string MergeFiles(string mainfile, string attachment, string path, int index)
{
var newFile = @"C:\Test\PDF\NewInsertedAt2.pdf";
int mainFilePages, attachFilePages, i, j, k;
PDFFile mainFile = PDFFile.FromFile(mainfile);
PDFFile attachFile = PDFFile.FromFile(attachment);
PDFImportedPage ip = null;
PDFDocument doc = new PDFDocument();
mainFilePages = mainFile.PagesCount;
attachFilePages = attachFile.PagesCount;
for (i = 0; i < index; i++)
{
ip = mainFile.ExtractPage(i);
doc.Pages.Add(ip);
}
for (j = 0; j < attachFilePages; j++)
{
ip = attachFile.ExtractPage(j);
doc.Pages.Add(ip);
}
for (k = i; k < mainFilePages; k++)
{
ip = mainFile.ExtractPage(k);
doc.Pages.Add(ip);
}
doc.Save(newFile);
mainFile.Close();
attachFile.Close();
return newFile;
}
我已經使用iTextSharp之前做到這一點,基本上創建一個新的輸出pdf,然後閱讀新的文檔,並通過頁面循環添加頁面到新的輸出文檔。這甚至會保持每個頁面的頁面大小和方向信息。
下面是增加的PDF到輸出一個的代碼:
int pc = pdfReader.NumberOfPages;
int p, rotation;
Rectangle box;
PdfImportedPage page;
for (p = 0; p < pc; p++) {
pageNo++;
page = pdfWriter.GetImportedPage(pdfReader, p + 1);
rotation = pdfReader.GetPageRotation(p + 1);
box = pdfReader.GetPageSizeWithRotation(p + 1);
outputDoc.SetPageSize(box);
outputDoc.NewPage();
if ((rotation == 90) || (rotation == 270)) {
pdfContentByte.AddTemplate(page, 0, -1.0f, 1.0f, 0, 0, box.Height);
} else {
pdfContentByte.AddTemplate(page, 1.0f, 0, 0, 1.0f, 0, 0);
}
}
pdfReader.Close();
在這個例子中是pdfReader類PdfReader引用源的PDF添加的一個實例。這可以通過文件,流或字節數組。 pdfWriter是輸出內容的PdfWriter類的新實例。
我知道它沒有使用PDF4NET或BCL easyPDF7,但希望它會有所幫助。
感謝分享這個,它肯定有幫助。但我們專注於使用PDF4NET或BCL easyPDF7。我確信iTextSharp的確讓這看起來很簡單。 –