Http://blog.csdn.net/yinshuhai/article/details/3042702
Using itextsharp. text;
Using itextsharp.text.pdf;
/// <Summary>
/// PDF file Merging
/// </Summary>
Class merge mergemanager
{
Private volume writer PW;
Private pdfreader reader;
Private document;
Private metadata contentbyte CB;
Private pdfimportedpage Newpage;
/// <Summary>
/// Build the merge management through the output file and merge it into the new file. After the merge is completed, call the finishedmerge method.
/// </Summary>
/// <Param name = "soutfiles"> </param>
Public synchronized mergemanager (string soutfiles)
{
Document = new document (pagesize. A3 );
If (file. exists (soutfiles )&&! Messagehelper. showquestion ("the corresponding file already exists. Is the file directory/R/N" + soutfiles + "/R/n overwriting the file? "))
{
Document = NULL;
Throw new ioexception ("User cancels operation ");
}
PW = writable writer. getinstance (document, new filestream (soutfiles, filemode. Create ));
Document. open ();
CB = pw. directcontent;
}
/// <Summary>
/// Merge files into the current writable stream through the file stream. After the merge is completed, the finishedmerge method is called.
/// </Summary>
/// <Param name = "SM"> </param>
Public synchronized mergemanager (Stream sm)
{
Document = new document ();
PW = writable writer. getinstance (document, Sm );
Document. open ();
CB = pw. directcontent;
}
/// <Summary>
/// Merge files
/// </Summary>
/// <Param name = "sfiles"> name of the file path to be merged </param>
/// <Returns> </returns>
Public bool mergefile (string sfiles)
{
Reader = new pdfreader (sfiles );
{
Int ipagenum = reader. numberofpages;
For (Int J = 1; j <= ipagenum; j ++)
{
Newpage = pw. getimportedpage (reader, J );
Rectangle r = reader. getpagesize (j );
Document. setpagesize (R );
Document. Newpage ();
CB. addtemplate (Newpage, 0, 0 );
}
}
Reader. Close ();
Return true;
}
/// <Summary>
/// Merge files with byte data
/// </Summary>
/// <Param name = "coming in"> pdf byte data </param>
/// <Returns> </returns>
Public bool mergefile (byte [] bytes in)
{
Reader = new pdfreader (updated in );
{
Int ipagenum = reader. numberofpages;
For (Int J = 1; j <= ipagenum; j ++)
{
Newpage = pw. getimportedpage (reader, J );
Rectangle r = reader. getpagesize (j );
Document. setpagesize (R );
Document. Newpage ();
CB. addtemplate (Newpage, 0, 0 );
}
}
Reader. Close ();
Return true;
}
/// <Summary>
/// Merge files through a PDF file stream
/// </Summary>
/// <Param name = "mongostream"> pdf file stream </param>
/// <Returns> </returns>
Public bool mergefile (stream processing Stream)
{
Reader = new pdfreader (mongostream );
{
Int ipagenum = reader. numberofpages;
For (Int J = 1; j <= ipagenum; j ++)
{
Newpage = pw. getimportedpage (reader, J );
Rectangle r = reader. getpagesize (j );
Document. setpagesize (R );
Document. Newpage ();
CB. addtemplate (Newpage, 0, 0 );
}
}
Reader. Close ();
Return true;
}
/// <Summary>
/// Merge files by network address
/// </Summary>
/// <Param name = "pdfurl"> network path of the PDF file to be merged </param>
/// <Returns> </returns>
Public bool mergefile (URI signed URL)
{
Reader = new pdfreader (volume URL );
{
Int ipagenum = reader. numberofpages;
For (Int J = 1; j <= ipagenum; j ++)
{
Newpage = pw. getimportedpage (reader, J );
Rectangle r = reader. getpagesize (j );
Document. setpagesize (R );
Document. Newpage ();
CB. addtemplate (Newpage, 0, 0 );
}
}
Reader. Close ();
Return true;
}
/// <Summary>
/// Merge
/// </Summary>
Public void finishedmerge ()
{
Try
{
If (reader! = NULL)
{
Reader. Close ();
}
If (PW! = NULL)
{
PW. Flush ();
PW. Close ();
}
If (document. isopen ())
{
Document. Close ();
}
}
Catch
{
}
}
}