Original: WinForm print multiple PDF files sequentially in sequential order
The question of PDF printing, a previous article (click here to see) also described, today to talk about another method
In fact, the method is very simple, because we need to print multiple PDF documents sequentially, why don't we combine the PDF documents we want to print into a single PDF print in order? It's so much easier.
Here the article is not written to show off what, just think found some good things to share it, but also make a record, convenient to find later
Start text
1, in order to facilitate, printing method will not find his way, and the previous consistent, specific as follows:
Newfalse=true= Itempath; // Print file path (local full path includes file name and suffix name) " Print " ;p Roc. Start ();p Roc. Close ();
View Code
2, is the focus, the combination of PDF documents, the method is very simple, a lot of online search, because I need to print JPG images and PDFs, so the merge method contains pictures
Using this method requires a third-party control ITextSharp.dll (click here to download)
/// <summary> ///combine multiple PDF files and jpg/png diagrams into one PDF document/// </summary> /// <param name= "FileList" >full path list of files that need to be merged</param> /// <param name= "Outmergefile" >output file full path</param> Public Static voidMergepdffile (list<string> FileList,stringoutmergefile) {Pdfreader reader; Document Document=NewDocument (); PDFWriter writer= Pdfwriter.getinstance (document,NewFileStream (Outmergefile, FileMode.Create)); Document. Open (); Pdfcontentbyte CB=writer. Directcontent; Pdfimportedpage NewPage; foreach(varItemfileinchfileList) { if(!file.exists (Itemfile)) { stringFileName =Path.getfilename (Itemfile); Logmessagewrite.writemessage (string. Format ("file Print Merge __{0} file does not exist", FileName)); Continue; } FileInfo Finfo=NewFileInfo (Itemfile); if(Finfo.length <1) { stringFileName =Path.getfilename (Itemfile); Logmessagewrite.writemessage (string. Format ("File Print Merge __ file content is empty, cannot print, {0}", FileName)); return; } varext =path.getextension (itemfile). ToLower (); if(". pdf". Equals (EXT)) {Reader=NewPdfreader (Itemfile); intIpagenum =Reader. Numberofpages; for(intj =1; J <= Ipagenum; J + +) {document. NewPage (); NewPage=writer. Getimportedpage (reader, J); Cb. Addtemplate (NewPage,0,0); } } Else if(". jpg". Equals (EXT) | |". Jpge". Equals (EXT) | |". PNG". Equals (EXT)) {FileStream RF=NewFileStream (Itemfile, FileMode.Open, FileAccess.Read); intSize = (int) RF. Length; byte[] Imext =New byte[size]; Rf. Read (Imext,0, size); Rf. Close (); Image img=image.getinstance (Imext); //Resize the picture so that it fits A4 varImgHeight =img. Height; varImgWidth =img. Width; if(IMG. Height >iTextSharp.text.PageSize.A4.Height) {imgheight=ITextSharp.text.PageSize.A4.Height; } if(IMG. Width >iTextSharp.text.PageSize.A4.Width) {imgwidth=ITextSharp.text.PageSize.A4.Width; } img. Scaletofit (ImgWidth, imgheight); //adjust the position of the picture so that it is centeredImg. Alignment =ITextSharp.text.Image.ALIGN_MIDDLE; Document. NewPage (); Document. Add (IMG); }} document. Close (); }
View Code
3. Print the merged file
Try { varMergefilepath =string. Format ("{0}mergepdf.pdf", Tempdowndir); Pdfprinthelper.mergepdffile (Pdflist, Mergefilepath); Process proc=NewProcess (); Proc. Startinfo.createnowindow=false; Proc. Startinfo.windowstyle=System.Diagnostics.ProcessWindowStyle.Hidden; Proc. Startinfo.useshellexecute=true; Proc. Startinfo.filename= Mergefilepath;//Print file path (local full path includes file name and suffix name)Proc. Startinfo.verb ="Print"; Proc. Start (); Proc. Close (); } Catch(Exception ex) {Logmessagewrite.writemessage (ex). Message); }
View Code
It's done.