using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Collections;
static void Main(string[] args)
{
String[] files = @"file1.pdf,file2.pdf".Split(',');
MergeFiles(@"file1.pdf",
files);
}
public static void MergeFiles(string destinationFile, string[] sourceFiles)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
PdfCopyFields copy = new PdfCopyFields(ms);
copy.Writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
ArrayList outlines = new ArrayList();
int pageOffset = 0;
int f = 0;
while (f < sourceFiles.Length)
{
string file =
sourceFiles[f];
PdfReader reader = new PdfReader(file);
copy.AddDocument(reader);
pageOffset +=
reader.NumberOfPages;
f++;
}
copy.Close();
MemoryStreamToFile(ms,
destinationFile);
}
}
catch (System.Exception e)
{
System.Console.Error.WriteLine(e.Message);
System.Console.Error.WriteLine(e.StackTrace);
System.Console.ReadLine();
}
}
public static void MemoryStreamToFile(MemoryStream MS, string FileName)
{
using (FileStream fs = new FileStream(@FileName, FileMode.Create))
{
byte[] data = MS.ToArray();
fs.Write(data, 0, data.Length);
fs.Close();
}
}
No comments:
Post a Comment