2012-04-17 50 views
5

我試圖合併很多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

回答

13

使用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

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

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和using語句優化馬里蘭州Kamruzzaman Sarker的答案。像這樣,它看起來更清潔,但所有的積分都歸他所有。