2012-04-17 35 views
5

Tôi đang cố gắng hợp nhất nhiều tệp pdf và cho mỗi pdf Tôi muốn thêm dấu trang (tên của pdf), tôi đã tìm thấy các kỹ thuật khác nhau của việc hợp nhất pdf nhưng không ai trong số chúng chỉ có thể thêm bookmark đánh dấu ví dụ. itextsharp thêm một chương, sau đó đánh dấu cho chương, tôi không muốn thay đổi của pdf.Kết hợp các tệp pdf với các dấu trang

+1

Có thể bạn cần trích xuất các trang riêng lẻ và ghép lại chúng thành một tệp duy nhất. Bằng cách này, bạn có thể đánh dấu trang đầu tiên của mỗi pdf bằng dấu trang – gyurisc

+0

Tôi không biết cách thêm một mã đơn giản – XandrUu

Trả lời

13

Sử dụng itextsharp bạn có thể thực hiện. tôi làm điều đó bằng phương pháp sau đây ...

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

Chạy mã này và hợp nhất pdf nhưng không thêm dấu trang. Các tệp pdf ban đầu cần có dấu trang để hiển thị trong pdf cuối cùng. – Moji

+0

Chỉ cần nói lời cảm ơn cho mã này! Tôi đã lướt web trong vài ngày qua cho đến khi tôi tình cờ gặp điều này ngay bây giờ. Cảm ơn một lần nữa! – calcazar

+0

ok, sau một thời gian tìm kiếm tôi tình cờ gặp đoạn mã này, hoạt động hoàn hảo! – Gelootn

0

Hãy thử Docotic.Pdf library cho tác vụ.

Dưới đây là một số mẫu mã mà những gì bạn mô tả:

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

Các mẫu kết hợp các tài liệu khác nhau vào một PDF và tạo bookmark. Mỗi dấu trang trỏ đến trang đầu tiên của tài liệu gốc.

Tuyên bố từ chối trách nhiệm: Tôi làm việc cho công ty phát triển thư viện 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; 
} 

tôi tối ưu hóa Md Kamruzzaman Sarker của câu trả lời bằng cách sử dụng một vòng lặp foreach để đi qua các file PDF và sử dụng báo cáo. Như thế này nó trông sạch hơn với tôi nhưng tất cả các khoản tín dụng đi với anh ta.

Các vấn đề liên quan