First I loop thru a directory to merge all the pdf files:
string strFileName;
ceTe.DynamicPDF.Page page = new ceTe.DynamicPDF.Page(PageSize.Letter);
int PageCnt = 0;
int FileCnt = 0;
string PDFDir = MapPath("~/PDFs/");
MergeDocument document = new MergeDocument();
string[] files = System.IO.Directory.GetFiles(PDFDir);
//loop thru directory and merge all PDF documents
foreach (string file in files)
{
if (System.IO.Path.GetExtension(file) == ".pdf")
{
if (FileCnt == 0)
{
document = new MergeDocument(file);
// Add Bookmark
strFileName = System.IO.Path.GetFileNameWithoutExtension(file);
document.Pages[0].Elements.Add(new ceTe.DynamicPDF.PageElements.Bookmark(strFileName, 0, 0));
PageCnt = document.Pages.Count;
}
else
{
// Append additional PDF
document.Append(file);
// Add Bookmark
strFileName = System.IO.Path.GetFileNameWithoutExtension(file);
document.Pages[PageCnt].Elements.Add(new ceTe.DynamicPDF.PageElements.Bookmark(strFileName, 0, 0));
PageCnt = document.Pages.Count;
}
FileCnt += 1;
}
}
Then I merge them.
try
{
//Save document to specific location
document.Draw(MapPath("~/PDFs/MergePDFs.pdf"));
}
catch (Exception ex)
{
string msg = "<script>alert('" +
"Process error, Contact support or try again. " +
ex.Message.Replace("\r\n", "").Replace("'", "") + "');</script>";
ScriptManager.RegisterClientScriptBlock(this, typeof(System.Web.UI.Page), "alert", msg, false);
return;
}
My code crashes on the document.Draw method, it only happens when I run the code twice, always works the first time around