2012-04-17 14 views
5

मैं बहुत सारे पीडीएफ को मर्ज करने की कोशिश कर रहा हूं और प्रत्येक पीडीएफ के लिए मैं एक बुकमार्क (पीडीएफ का नाम) जोड़ना चाहता हूं, मुझे पीडीएफ के विलय की अलग तकनीकें मिलीं लेकिन उनमें से कोई भी केवल जोड़ नहीं सकता बुकमार्क सामने उदाहरण के लिए। itextsharp जोड़ एक अध्याय है, फिर अध्याय के लिए बुकमार्क, मैं पीडीएफ को बदलना नहीं चाहता।बुकमार्क के साथ पीडीएफ फाइलों को विलय करना

+1

शायद आपको अलग-अलग पृष्ठों को निकालने और उन्हें एक फ़ाइल में फिर से इकट्ठा करने की आवश्यकता है। इस तरह आप बुकमार्क – gyurisc

+0

के साथ प्रत्येक पीडीएफ के पहले पृष्ठ को चिह्नित कर सकते हैं मुझे नहीं पता कि एक सरल बुकमार्क कैसे जोड़ें – 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

इस कोड को चलाएं और पीडीएफ विलय करें लेकिन इसमें बुकमार्क नहीं जोड़े गए हैं। आरंभिक पीडीएफ फ़ाइलों को अंतिम पीडीएफ में दिखाने के लिए बुकमार्क होना चाहिए। – Moji

+0

बस इस कोड के लिए धन्यवाद कहना है! मैं पिछले कुछ दिनों से वेब सर्फ कर रहा हूं जब तक कि मैं अभी इस पर ठोकर नहीं लगाता। पुन: धन्यवाद! – calcazar

+0

ठीक है, खोज के कुछ समय बाद मैं कोड के इस टुकड़े पर ठोकर खाई, पूरी तरह से काम करता है! – 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"); 
    } 
} 

नमूना एक पीडीएफ में विभिन्न दस्तावेजों को जोड़ती है और बुकमार्क पैदा करता है। प्रत्येक बुकमार्क मूल दस्तावेज़ के पहले पृष्ठ पर इंगित करता है।

अस्वीकरण: मैं उस कंपनी के लिए काम करता हूं जो डॉकॉटिक.पीडीएफ लाइब्रेरी विकसित करता है।

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 पाश का उपयोग कर pdfs पर जाने के लिए और बयानों का उपयोग करके अनुकूलित मोहम्मद Kamruzzaman Sarker के जवाब। इस तरह यह मेरे लिए क्लीनर दिखता है लेकिन सभी क्रेडिट उसके पास जाते हैं।

संबंधित मुद्दे