2012-04-17 29 views
5

私は多くのpdfをマージしようとしていますが、ブックマーク(pdfの名前)を追加したいのですが、pdfをマージする方法は異なりますが、ブックマークの先頭など。 itextsharpは章を追加してから章のしおりを、私はpdfを変更したくありません。ブックマークでPDFファイルをマージする

+1

個々のページを抽出し、それらを1つのファイルに再構成する必要があるかもしれません。この方法で、各PDFの最初のページにブックマーク – gyurisc

+0

を付けることができます。simplebookmarkを追加する方法がわかりません – XandrUu

答えて

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

+0

このコードをありがとうございます!私はこれを直ちに見つけ出すまで、ここ数日間ウェブをサーフィンしてきました。ありがとう! – calcazar

+0

ok、検索の時間がたった後、私はこのコードを見つけたが、完璧に動作する! – Gelootn

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"); 
    } 
} 

サンプルは、1つの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; 
} 

私はpdfファイルの上に行くためにforeachループを使用してステートメントを使用してメリーKamruzzaman Sarkerの答えを最適化。このように私はより清潔に見えるが、すべてのクレジットは彼に行く。

関連する問題