A pdf merge operation implementation class, implemented using the itextsharp Control

Source: Internet
Author: User

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

{

 

}

}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.