You must Sign In to post a response.
  • Category: Windows 10

    Please hely me to merge pdf files

    can i use this page to merge pdf files on my computer? http://www.rasteredge.com/how-to/csharp-imaging/pdf-merge/
    or is there any other high quality software to merge pdf files very fast?
  • #765433
    You can use it but its a third party dll and which is the license version, if you are going to buy its license then it is good to use it, other wise you need to search for any open source PDF library, to merge PDF.
    you can use PDF sharp library, PDFsharp is the Open Source .NET library that easily creates and processes PDF documents on the fly from any .NET language, which has good code of PDF merging, see below snippet
    download it from
    http://www.pdfsharp.net/?AspxAutoDetectCookieSupport=1

    using (PdfDocument one = PdfReader.Open("file1.pdf", PdfDocumentOpenMode.Import))
    using (PdfDocument two = PdfReader.Open("file2.pdf", PdfDocumentOpenMode.Import))
    using (PdfDocument outPdf = new PdfDocument())
    {
    CopyPages(one, outPdf);
    CopyPages(two, outPdf);

    outPdf.Save("file1and2.pdf");
    }

    void CopyPages(PdfDocument from, PdfDocument to)
    {
    for (int i = 0; i < from.PageCount; i++)
    {
    to.AddPage(from.Pages[i]);
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765435
    Hi,

    Use the below for merge multiple pdf files


    // Find all pdf documents in input folder.
    DirectoryInfo directoryInfo = new DirectoryInfo(@"..\..\../inputdocuments");
    FileInfo[] allPDFs = directoryInfo.GetFiles("*.pdf");

    // Create a document for the merged result.
    Document mergedDocument = new Document();

    // Keep a list of input streams to close when done.
    List<FileStream> streams = new List<FileStream>();
    foreach (FileInfo fileInfo in allPDFs)
    {
    // Open input stream and add to list of streams.
    FileStream stream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);
    streams.Add(stream);

    // Open input document.
    Document document = new Document(stream);

    // Append all pages to target document.
    // The target document holds references to data in the input stream.
    // For efficiency it does not copy this data so the input streams
    // should not be closed before the merged document is saved.
    mergedDocument.Pages.AddRange(document.Pages.CloneToArray());
    }

    // Save merged document.
    using (FileStream output = new FileStream(@"..\..\output.pdf", FileMode.Create, FileAccess.Write))
    {
    mergedDocument.Write(output);
    }

    // Close all input streams.
    streams.ForEach(stream => stream.Close());

  • #765439
    I recommend you take a look at Spire.PDF...
    Check following solutions it provided for merging PDFs:

    http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Merge-PDF-Files-with-New-Method-in-C.html

    http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Document-Operation/Merge-Selected-Pages-from-Multiple-PDF-Files-into-One-in-C-VB.NET.html

    http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/PDF-Merge-–-Merge-PDF-Files-and-Add-Page-Number-with-C-VB.NET.html

    Even other types of files:
    http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Document-Operation/Merge-Multiple-File-Types-into-One-PDF-in-C-VB.NET.html

  • #765494
    If you want to merge source documents using iText(Sharp), there are two basic situations:

    You really want to merge the documents, acquiring the pages in their original format, transfering as much of their content and their interactive annotations as possible. In this case you should use a solution based on a member of the Pdf*Copy* family of classes.

    You actually want to integrate pages from the source documents into a new document but want the new document to govern the general format and don't care for the interactive features (annotations...) in the original documents (or even want to get rid of them). In this case you should use a solution based on the PdfWriter class.

    You can find details in chapter 6 (especially section 6.4) of iText in Action — 2nd Edition. The Java sample code can be accessed here and the C#'ified versions here.

    A simple sample using PdfCopy is Concatenate.java / Concatenate.cs. The central piece of code is:

    byte[] mergedPdf = null;
    using (MemoryStream ms = new MemoryStream())
    {
    using (Document document = new Document())
    {
    using (PdfCopy copy = new PdfCopy(document, ms))
    {
    document.Open();

    for (int i = 0; i < pdf.Count; ++i)
    {
    PdfReader reader = new PdfReader(pdf[i]);
    // loop over the pages in that document
    int n = reader.NumberOfPages;
    for (int page = 0; page < n; )
    {
    copy.AddPage(copy.GetImportedPage(reader, ++page));
    }
    }
    }
    }
    mergedPdf = ms.ToArray();
    }
    Here pdf can either be defined as a List<byte[]> immediately containing the source documents (appropriate for your use case of merging intermediate in-memory documents) or as a List<String> containing the names of source document files (appropriate if you merge documents from disk).

    An overview at the end of the referenced chapter summarizes the usage of the classes mentioned:

    PdfCopy: Copies pages from one or more existing PDF documents. Major downsides: PdfCopy doesn't detect redundant content, and it fails when concatenating forms.

    PdfCopyFields: Puts the fields of the different forms into one form. Can be used to avoid the problems encountered with form fields when concatenating forms using PdfCopy. Memory use can be an issue.

    PdfSmartCopy: Copies pages from one or more existing PDF documents. PdfSmartCopy is able to detect redundant content, but it needs more memory and CPU than PdfCopy.

    PdfWriter: Generates PDF documents from scratch. Can import pages from other PDF documents. The major downside is that all interactive features of the imported page (annotations, bookmarks, fields, and so forth) are lost in the process.


Sign In to post your comments