winform 按顺序连续打印多个PDF文件

原文:winform 按顺序连续打印多个PDF文件

关于PDF打印的问题,前面有篇文章(点这里查看)也叙述过,今天来谈谈另外一种方法

其实方法很简单,因为需要把多个PDF文档按顺序连续打印,为此我们为什么不把要打印的pdf文档按顺序合并成一个PDF打印呢?如此就简单多了哦。

这里文章写出来并不是为了炫耀什么,只是觉得发现些好东西就分享出来而已,同时也做个记录,方便以后查找

开始正文

1、为了方便,打印方法就不另寻他路了,和前面一致,具体如下:

Process proc = new Process();
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = itemPath;//打印文件路径(本地完整路径包括文件名和后缀名)
proc.StartInfo.Verb = "print";
proc.Start();
proc.Close();

View Code

2、就是重点了,合并PDF文档,方法很简单,网上一搜一大把,因为我的需求需要把jpg图片和pdf一起打印,因此合并方法中包含图片

使用此方法需要第三方控件iTextSharp.dll(点击这里下载)

        /// <summary>
        /// 把多个PDF文件和JPG/PNG图合并成一个PDF文档
        /// </summary>
        /// <param name="fileList">需要合并文件的完整路径列表</param>
        /// <param name="outMergeFile">输出文件完整路径</param>
        public static void MergePDFFile(List<string> fileList, string outMergeFile)
        {
            PdfReader reader;
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage newPage;
            foreach (var itemFile in fileList)
            {
                if (!File.Exists(itemFile))
                {
                    string fileName = Path.GetFileName(itemFile);
                    LogMessageWrite.WriteMessage(string.Format("文件打印合并__{0} 文件不存在", fileName));
                    continue;
                }
                FileInfo fInfo = new FileInfo(itemFile);
                if (fInfo.Length < 1)
                {
                    string fileName = Path.GetFileName(itemFile);
                    LogMessageWrite.WriteMessage(string.Format("文件打印合并__文件内容为空,无法打印,{0}", fileName));
                    return;
                }

                var ext = Path.GetExtension(itemFile).ToLower();
                if (".pdf".Equals(ext))
                {
                    reader = new PdfReader(itemFile);
                    int iPageNum = reader.NumberOfPages;
                    for (int j = 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 = new FileStream(itemFile, FileMode.Open, FileAccess.Read);
                    int size = (int)rf.Length;
                    byte[] imext = new byte[size];
                    rf.Read(imext, 0, size);
                    rf.Close();

                    Image img = Image.GetInstance(imext);

                    //调整图片大小,使之适合A4
                    var imgHeight = img.Height;
                    var imgWidth = 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);

                    //调整图片位置,使之居中
                    img.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;

                    document.NewPage();
                    document.Add(img);
                }
            }
            document.Close();
        }

View Code

3、打印合并后的文件

            try
            {
    var mergeFilePath = string.Format("{0}mergepdf.pdf", tempDownDir);

                PDFPrintHelper.MergePDFFile(pdfList, mergeFilePath);

                Process proc = new Process();
                proc.StartInfo.CreateNoWindow = false;
                proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                proc.StartInfo.UseShellExecute = true;
                proc.StartInfo.FileName = mergeFilePath;//打印文件路径(本地完整路径包括文件名和后缀名)
                proc.StartInfo.Verb = "print";
                proc.Start();
                proc.Close();
            }
            catch (Exception ex)
            {
                LogMessageWrite.WriteMessage(ex.Message);
            }

View Code

至此 大功告成

 

时间: 2024-09-08 22:51:24

winform 按顺序连续打印多个PDF文件的相关文章

c# winform pdf 打印-求助:C# 怎么将一个winform画面生成一个pdf文件?

问题描述 求助:C# 怎么将一个winform画面生成一个pdf文件? 大家好,本人现在在做一个项目,需要把一个winform画面生成pdf文件,可是在网上查了一些资料,都说的很不全面,请哪位大神看到后帮忙指导一下,最好给出详细步骤,比如要安装什么辅助软件之类的,非常感谢了

winform c# pdf-C#Winform怎么把窗口上的信息生成PDF文件???

问题描述 C#Winform怎么把窗口上的信息生成PDF文件??? 窗口上的信息很多,估计要分好几页保存,在谷歌搜了说可以用pdfsharp,但是不会用 还有没有其他的方法,有具体的例子也好,谢谢大家..

C# WinForm打开PDF文件

c# winform打开pdf文件 其实winform打开pdf文件 有很多种方法 主要分为2个大的方法 一:使用adobe reader提供的com组件 1.添加引用 工具箱---右键---选择项--com组件--adobe pdf reader 2.使用方法  openfiledialog openfile=new openfiledialog();  open..filter = "pdf文件|*.pdf";  openfile.showdialog();  axacropdf1

C#WinForm打开office文件,pdf文件和视频文件

打开office文件 1 需要从网站下载dsoframer.ocx文件 2 把dsoframer.ocx文件复制到c:\windows\system32目录下          开始->运行->regsvr32 dsoframer.ocx , 系统会提示DSOFramer.ocx中的DllRegisterServer成功 3 在VS中新建项目,添加引用 4 将该控件加载到工具箱(添加新选项) 5 将控件拖到窗体中    打开文件:this.axFramerControl1.Open(filep

C#winform中如何用控件webBrowser打开pdf文件?

问题描述 我的编码是usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace_11{publicpartialclassForm1:Form{publicForm1(){InitializeCompon

C# WinForm打开PDF文件并在窗体中显示_C#教程

1.添加引用 工具箱---右键---选择项--COM组件--Adobe PDF Reader 2.使用方法 复制代码 代码如下: OpenFileDialog openFile=new OpenFileDialog(); open..Filter = "PDF文件|*.pdf"; openFile.ShowDialog(); axAcroPDF1.src = openFile.FileName; //axAcroPDF1.LoadFile(of.FileName);   //使用方法二

用C#制作PDF文件全攻略

攻略 前 言丽水市汽车运输集团有限公司信息中心 苟安廷PDF文件是目前比较流行的电子文档格式,在办公自动化(OA)等软件的开发中,经常要用到该格式,但介绍如何制作PDF格式文件的资料非常少,在网上搜来搜去,都转贴的是同一段"暴力"破解的方法,代码片断如下: StreamWriter pPDF=new StreamWriter(filePath); ArrayList xRefs=new ArrayList(); float yPos =0f; long streamStart=0; l

Photoshop如何将多张图片转换为PDF文件

  1.打开Photoshop,点击文件菜单,点击"自动"选项,选择"PDF批处理". 2. 点击"浏览"按钮选择要创建的图片文件. 3.选择图片并排序,你可以在这里拖动图片名称来进行排序,这将会影响到PDF输出后的页面顺序 4.你可以根据需要来改变PDF的具体参数,然后点击"存储PDF",Photoshop便会自动进行处理,稍等片刻就会看到输出的PDF文件了. 分类: PS入门教程

利用Photoshop将多张图片转换为PDF文件

  1. 支持版本 首先你电脑上要安装有Photoshop CS3及以上版本,版本过于陈旧的同学请更新你的Photoshop 2. PDF演示文稿 打开Photoshop,点击文件菜单>自动>PDF批处理. 3. 点击"浏览"按钮选择要创建的图片文件 4. 选择图片并排序 你可以在这里拖动图片名称来进行排序,这将会影响到PDF输出后的页面顺序. 5. 存储并设置参数 你可以根据需要来改变PDF的具体参数,包括是否嵌入页面缩览图.压缩属性等等.你可以根据自己的需要来进行设置.