5

我正在尝试合并很多pdf,并且对于每个pdf我想添加一个书签(pdf的名称),我发现了合并pdf的不同技术,但它们都不能只添加书签,例如。itextsharp 添加一章,然后是该章的书签,我不想更改 pdf 的。

4

3 回答 3

14

使用 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();
}
于 2012-06-15T04:59:49.137 回答
0

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

于 2012-04-25T17:18:24.243 回答
0
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 的答案。像这样对我来说看起来更干净,但所有功劳都归他所有。

于 2017-05-24T08:07:39.933 回答