将 pdf 文件与书签合并

本文关键字:书签 合并 文件 pdf | 更新日期: 2023-09-27 18:31:17

我正在尝试合并很多pdf,对于每个pdf,我想添加一个书签(pdf的名称),我发现了合并pdf的不同技术,但没有一个可以只添加书签。 itextsharp 添加一个章节,然后是该章节的书签,我不想更改 pdf。

将 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();
}
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;
}

我优化了Md Kamruzzaman Sarker的答案,通过使用foreach循环来浏览pdf并使用语句。像这样对我来说看起来更干净,但所有的功劳都归于他。

尝试 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库的公司工作。