将pdf文件与书签合并

时间:2012-04-17 08:10:26

标签: c# pdf merge itext pdf-generation

我正在尝试合并很多pdf和每个pdf我想要添加一个书签(pdf的名称),我找到了合并pdf的不同技术,但是没有一个只能添加书签,例如。 itextsharp添加章节,然后是章节的书签,我不想改变pdf的。

3 个答案:

答案 0 :(得分: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();
     }

答案 1 :(得分:0)

尝试Docotic.Pdf library完成任务。

以下示例代码执行您所描述的内容:

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

答案 2 :(得分: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循环来优化 Md Kamruzzaman Sarker的答案,以查看pdf和使用语句。就像这样,它对我来说看起来更干净,但所有的归功于他。