Search code examples
c#pdfmergeitext

Combining different PDF files onto a single PDF page, creating a sample booklet


using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace CutAndPaste
{
    class Program
    {
        public static readonly string SRC = Environment.CurrentDirectory + "\\pdfs\\2.pdf";
        public static readonly string SRC2 = Environment.CurrentDirectory + "\\pdfs\\3.pdf";
        public static readonly string DEST = Environment.CurrentDirectory + "\\output.pdf";

        static void Main(string[] args)
        {
            MergePDFs(DEST, SRC, SRC2);
            Console.WriteLine("PDF'ler başarıyla birleştirildi.");
        }

        static void MergePDFs(string destPath, params string[] sourcePaths)
        {
            using (FileStream fs = new FileStream(destPath, FileMode.Create, FileAccess.Write, FileShare.None))
            using (Document doc = new Document())
            using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
            {
                doc.Open();
                PdfContentByte cb = writer.DirectContent;

                foreach (string sourcePath in sourcePaths)
                {
                    using (PdfReader reader = new PdfReader(sourcePath))
                    {
                        for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
                        {
                            doc.NewPage();
                            PdfImportedPage page = writer.GetImportedPage(reader, pageNumber);
                            cb.AddTemplate(page, 0, 0);
                        }
                    }
                }
            }
        }
    }
}

I'm getting this error with this code: 'System.ObjectDisposedException: Cannot access a closed file.' Do you think this approach is correct? I would appreciate your assistance on this matter


Solution

  • Hi Kindly try below code

    internal string PDFMerger(Dictionary<string, string> dic)
        {
            var outuptpath = Server.MapPath("~/" + DateTime.Now.ToString("yyyyMMdd") + "/_Final.pdf");
             
            Document doc = new Document();
            PdfCopy writer = new PdfCopy(doc, new FileStream(outuptpath, FileMode.Create));
            if (writer == null)
            {
                return "";
            }
            doc.Open();
            foreach (var filename in dict)
            {
                var fname = filename.Value;
                PdfReader reader = new PdfReader(fname);
                reader.ConsolidateNamedDestinations();
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    PdfImportedPage page = writer.GetImportedPage(reader, i);
                    writer.AddPage(page);
                }
                reader.Close();
            }
            writer.Close();
            doc.Close();
            return outuptpath;
        }
    

    Here I'm send the filename and path in the Dictionary.

    kindly check so the same.