C # print PDF files

Source: Internet
Author: User

After several days of searching and testing, we found that the following three methods can be used to print PDF files directly with C #, but none of them are what I want, I want to print a PDF file directly by clicking the print button in C # without jumping out of any window. I am still searching. If a friend knows how to implement it, please leave a message, however, the following three methods are provided for your reference.

Method 1: Call the command line:

Using system. Drawing. printing;
Using system. diagnostics;
Using system. Collections. Specialized;

// Print method

Private void upload print (string filepath)
{
Printdocument Pd = new printdocument ();
PROCESS p = new process ();
Processstartinfo startinfo = new processstartinfo ();
Startinfo. createnowindow = true;
Startinfo. windowstyle = processwindowstyle. hidden;
Startinfo. useshellexecute = true;
Startinfo. filename = filepath;
Startinfo. verb = "print ";
Startinfo. Arguments = @ "/P/h \" + filepath + "\" \ "" + PD. printersettings. printername + "\"";
P. startinfo = startinfo;
P. Start ();
P. waitforexit ();


}
Protected void btn_print_click (Object sender, eventargs E)
{

String filepath = "C: \ Documents ents and Settings \ auyeungck \ My Documents ents \ myfile \ 1.20 ";
Export print (filepath );
}

Conclusion: After clicking print in the preceding method, an Adobe window will pop out, but no content will be displayed. The printer will print it automatically. The problem has been tested, what I printed when I printed my company announcement was abnormal.

Method 2: Call other class libraries.

// The code after the six DLL files are found and referenced in the project

// O2s. components. javasview4net. dll
// O2s. components. pdfrender4net. dll
// FontBox-0.1.0-dev.dll
// Ikvm. GNU. classpath. dll
// Ikvm. runtime. dll
// PDFBox-0.7.3.dll

// This is the referenced three namespaces
Using o2s. components. pdfrender4net;
Using system. Drawing. printing;
Using o2s. components. pdfrender4net. printing;

/// <Summary>
/// Printed code
/// </Summary>
/// <Param name = "url"> pdf path to be printed </param>
Private int printshow (string URL)
{
Int isok = 0;
Pdffile file = pdffile. Open (URL );
Printersettings settings = new printersettings ();
System. Drawing. Printing. printdocument Pd = new system. Drawing. Printing. printdocument ();
Settings. printername = "HP LaserJet 1160 PCL 5E ";
Settings. printtofile = false;

// Set the paper size (you can choose not to set the size, take the default setting) 3.90 In, 8.65 in
Papersize PS = new papersize ("test", 4,9 );
PS. rawkind = 9; // if it is custom paper, It will be greater than 118, (A4 value is 9, detailed paper type and value of the contrast please see http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind (V = vs.85 ). aspx)

O2s. components. pdfrender4net. Printing. Program printsettings kernel printsettings = new o2s. components. pdfrender4net. Printing. Program printsettings (settings );
Required printsettings. papersize = Ps;
Required printsettings. pagescaling = o2s. components. pdfrender4net. Printing. pagescaling. fittoprintermarginsproportional;
Required printsettings. printersettings. Copies = 1;

Try
{
File. Print (embedded printsettings );
Isok = 1;
}
Catch (exception)
{
Isok =-1;
Throw;
}
Finally
{

File. Dispose ();
}
Return isok;
}

// Click a print button to print
Protected void btnprint_click (Object sender, eventargs E)
{
String url = "here is the path of the PDF file, for example, C: \ Documents and Settings \ auyeungck \ My Documents \ myfile \ aa1.pdf ";
Int isok =-1;
Isok = printshow (URL );
If (isok> 0)
{
Response. Write ("printed successfully! ");
}
Else {
Response. Write ("Printing failed! ");
}
}

Conclusion: After clicking print, the above method is printed directly. However, the printed paper displays the "Export view4.0.2.0 eveluation version" and displays the text and image content, print only the image part.

 

Method 3: Load Adobe COM components

1. Open the winform interface, right-click on the toolbar on the left, and click choose items. Click COM components to check Adobe PDF reader.

2. Drag the newly loaded Adobe PDF Reader control to the winform interface.

3. Enter the following code on the loading interface:

String filename = "C :\\ documents ents and settings \\ auyeungck \ My Documents ents \ myfile \ aa1.pdf ";
This. axacroapps1.loadfile (filename );

Axacro).1setshowtoolbar (false );

Axacroupload 1.loadfile (filename );
Ax1_1_1.printall ();

 

In addition, you can also create the following code by yourself:

Axacropdflib. axacropdf 1 = new axacropdflib. axacropdf ();
Axacroroute 1.location = new system. Drawing. Point (0, 24 );
Axacro1_1.size = new system. Drawing. Size (292,242 );
Axacropattern 1.dock = dockstyle. Fill;
Controls. Add (ax1_pdf1 );

 

Summary: The above method has completed the display of PDF files and printing functions, but after this operation, a prompt window will pop out to show whether to print. Whether you print or not, an Adobe window with no content will be displayed, then, the PDF file is displayed in winform.

Method 4: Use a third-party control itextsharp to copy a PDF file

/// <Summary>

/// Implement PDF copying

/// </Summary>

/// <Param name = "filepath"> source PDF file </param>

/// <Param name = "topath"> Target c1pdf 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 );

Required copy Copy = new temporary copy (document, baos );

Copy. viewerpreferences = extends writer. hidetoolbar | extends writer. hidemenubar;

// Write 20837 e in J to PDF

Document. open ();

For (INT I = 1; I <= N; I ++)

{

Pdfimportedpage page = 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 ();

}

Protected void into print_click (Object sender, eventargs E)

{

String filepath = @ "D: \ itextsharppdf \ gold medal service printer sequence number ";

String topath = @ "D: \ itextsharppdf \ Copy \ gold medal service printer sequence number ";

Convertpdftopdf (filepath, topath, true );

Response. Write ("Copied successfully ");

}

Summary: This method copies the PDF file to another place. After you open the copied PDF file, the file will be printed directly. This file also jumps out of the Adobe interface, however, you can only implement the printing function and cannot save it separately.

 

Related Article

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.