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
Hi Kindly try below code
kindly check so the same.