C # implement PDF copying and automatic printing through itextsharp

Source: Internet
Author: User
Code

///   <Summary>
/// PDF copying
///   </Summary>
///   <Param name = "filepath"> Source PDF File </Param>
///   <Param name = "topath"> Target PDF File </Param>
///   <Param name = "print"> Whether to enable automatic printing </Param>

Private   Void Convertpdftopdf ( String Filepath, String Topath, Bool Print)
{
Pdfreader Reader =   New Pdfreader (filepath );
Document document =   New Document (reader. getpagesizewithrotation ( 1 ));
Int N = Reader. numberofpages;
Filestream baos =   New Filestream (topath, filemode. Create, fileaccess. Write );
Pdfcopy =   New Upload copy (document, baos );
Copy. viewerpreferences = Pdfwriter. hidetoolbar | Using writer. hidemenubar;
// Write content to PDF
Document. open ();
For ( Int I =   1 ; I <= N; I ++ )
{
Pdfimportedpage = Copy. getimportedpage (reader, I );
Copy. addpage (PAGE );
}
If (Print)
{
Pdfaction. JavaScript ( " Myonmessage (); " , Copy );
Copy. addjavascript ( " This. print (true); function myonmessage (amessage) {app. alert ('test', 2);} var msghandlerobject = new object (); Doc. onwillprint = myonmessage; this. hostcontainer. messagehandler = msghandlerobject; " );
}
Document. Close ();
Reader. Close ();
}

A better site for learning itextsharp:

Http://pdfhome.hope.com.cn/ArcSearch.aspx? Content = step-by-step learning itextsharp & page = 1

 

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.