使用书签

问题描述:

我试图合并很多PDF文件,并为每个PDF我想添加一个书签(PDF的名称)合并的PDF文件,我发现合并的PDF文件的difrent技术,但没有人可以只添加例如前面的书签。 itextsharp添加了一章,然后是章节的书签,我不想改变pdf的。使用书签

+1

也许你需要提取的个人页面和他们重新组合成一个单一的文件。这样你可以用书签标记每个pdf的第一页 – gyurisc 2012-04-17 08:15:03

+0

我不知道如何添加一个简单的标记 – XandrUu 2012-04-17 08:16:35

使用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(); 
    } 
+0

冉这个代码和合并的PDF,但它没有添加书签。 最初的pdf文件需要有书签才能显示在最终的pdf中。 – Moji 2014-07-22 16:11:22

+0

只需要说声谢谢你的代码!最近几天我一直在网上冲浪,直到刚才我偶然发现了这一点。再次感谢! – calcazar 2014-11-26 12:37:23

+0

好吧,经过一段时间的搜索,我偶然发现了这段代码,完美地工作! – Gelootn 2015-05-28 12:33:05

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

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和using语句优化马里兰州Kamruzzaman Sarker的答案。像这样,它看起来更清洁,但所有的积分都归他所有。