[英]Merging pdf files with bookmarks
我正在嘗試合並很多 pdf,對於每個 pdf 我想添加一個書簽(pdf 的名稱),我發現了合並 pdf 的不同技術,但沒有一個可以只添加書簽,例如。 itextsharp 添加一章,然后是該章的書簽,我不想更改 pdf。
使用 itextsharp 你可以做到。 我通過以下方法來做到這一點:
MergePdfFiles(string outputPdf, string[] sourcePdfs) {
PdfReader reader = null;
Document document = new Document();
PdfImportedPage page = null;
PdfCopy pdfCpy = null;
int n = 0;
int totalPages = 0;
int page_offset = 0;
List < Dictionary < string, object >> bookmarks = new List < Dictionary < string, object >> ();
IList < Dictionary < string, object >> tempBookmarks;
for (int i = 0; i <= sourcePdfs.GetUpperBound(0); i++) {
reader = new PdfReader(sourcePdfs[i]);
reader.ConsolidateNamedDestinations();
n = reader.NumberOfPages;
tempBookmarks = SimpleBookmark.GetBookmark(reader);
if (i == 0) {
document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
pdfCpy = new PdfCopy(document, new FileStream(outputPdf, FileMode.Create));
document.Open();
SimpleBookmark.ShiftPageNumbers(tempBookmarks, page_offset, null);
page_offset += n;
if (tempBookmarks != null)
bookmarks.AddRange(tempBookmarks);
// MessageBox.Show(n.ToString());
totalPages = n;
} else {
SimpleBookmark.ShiftPageNumbers(tempBookmarks, page_offset, null);
if (tempBookmarks != null)
bookmarks.AddRange(tempBookmarks);
page_offset += n;
totalPages += n;
}
for (int j = 1; j <= n; j++) {
page = pdfCpy.GetImportedPage(reader, j);
pdfCpy.AddPage(page);
}
reader.Close();
}
pdfCpy.Outlines = bookmarks;
document.Close();
}
嘗試使用Docotic.Pdf 庫來完成任務。
這是執行您所描述的操作的示例代碼:
public static void combineDocumentsWithBookmarks()
{
string[] names = new string[] { "first.pdf", "second.pdf", "third.pdf" };
using (PdfDocument pdf = new PdfDocument())
{
int targetPageIndex = 0;
for (int i = 0; i < names.Length; i++)
{
string currentName = names[i];
if (i == 0)
pdf.Open(currentName);
else
pdf.Append(currentName);
pdf.OutlineRoot.AddChild(currentName, targetPageIndex);
targetPageIndex = pdf.PageCount;
}
// setting PageMode will cause PDF viewer to display
// bookmarks pane when document is open
pdf.PageMode = PdfPageMode.UseOutlines;
pdf.Save("output.pdf");
}
}
該示例將不同的文檔合並為一個 PDF 並創建書簽。 每個書簽指向原始文檔的第一頁。
免責聲明:我為開發 Docotic.Pdf 庫的公司工作。
public string MergeFiles(string outputPath)
{
if (string.IsNullOrEmpty(outputPath))
throw new NullReferenceException("Path for output document is null or empty.");
using (Document outputDocument = new Document())
{
using (PdfCopy pdf = new PdfCopy(outputDocument, new FileStream(outputPath, FileMode.Create)))
{
outputDocument.Open();
// All bookmarks for output document
List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();
// Bookmarks of the current document
IList<Dictionary<string, object>> tempBookmarks;
int pageOffset = 0;
// Merge documents and add bookmarks
foreach (string file in Files)
{
using (PdfReader reader = new PdfReader(file))
{
reader.ConsolidateNamedDestinations();
// Get bookmarks of current document
tempBookmarks = SimpleBookmark.GetBookmark(reader);
SimpleBookmark.ShiftPageNumbers(tempBookmarks, pageOffset, null);
pageOffset += reader.NumberOfPages;
if(tempBookmarks != null)
// Add bookmarks of current document to all bookmarks
bookmarks.AddRange(tempBookmarks);
// Add every page of document to output document
for (int i = 1; i <= reader.NumberOfPages; i++)
pdf.AddPage(pdf.GetImportedPage(reader, i));
}
}
// Add all bookmarks to output document
pdf.Outlines = bookmarks;
}
}
return outputPath;
}
我通過使用 foreach 循環遍歷 pdf 並使用語句來優化Md Kamruzzaman Sarker 的答案。 像這樣對我來說看起來更干凈,但所有的功勞都歸功於他。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.