关于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();

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 < )
{
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 = ; j <= iPageNum; j++)
{
document.NewPage();
newPage = writer.GetImportedPage(reader, j);
cb.AddTemplate(newPage, , );
}
}
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, , 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();
}

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);
}

至此 大功告成

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

  1. 浅谈linux系统中pdf文件的默认打开方式

    atril.gimp和evince,三者均可以打开application/pdf格式文件.gimp为一款图像处理软件:atril为mate环境下常用的文档查看器:evince为gnome环境下常用的文 ...

  2. SNF开发平台WinForm之十三-单独从服务器上获取PDF文件进行显示-SNF快速开发平台3.3-Spring.Net.Framework

    1运行效果: 2开发实现: 如果需要单独显示PDF文件时用下面代码去实现,指定url地址. 地址: . 获取附件管理的实体对象: List<KeyValuePair<string, obj ...

  3. winform 替换word文档中的字段(包含图片添加),生成导出PDF文件(也可是word文件)

    1.先打开你需要替换的word文档,在想要后续更换字段值的地方添加“书签”. 2.将模板文档存放在 程序的Debug文件下. 3.生成文件的按钮点击事件 代码: string templatePath ...

  4. 如何给现有的PDF文件添加页码

    如何给现有的PDF文件添加页码 之前我写了如何打印PDF文件,有人qq问我怎样在打印时给PDF文件添加页码,的确,给PDF文件添加页码,可以帮助我们区分纸质档的PDF文件页面的先后顺序,方便我们对它的 ...

  5. C#的Process类调用第三方插件实现PDF文件转SWF文件

    在项目开发过程中,有时会需要用到调用第三方程序实现本系统的某一些功能,例如本文中需要使用到的swftools插件,那么如何在程序中使用这个插件,并且该插件是如何将PDF文件转化为SWF文件的呢?接下来 ...

  6. C# 将多个office文件转换及合并为一个PDF文件

    PDF文件介绍 PDF(Portable Document Format )文件源于20世纪90年代初期,如今早已成为了一种最流行的的文件格式之一.因为PDF文件有很多优点: 支持跨平台和跨设备共享 ...

  7. 如何通过WPS 2013 API 将Office(Word、Excel和PPT)文件转PDF文件

    1. 描述 PDF 文件是一种便携文件格式,是由Adobe公司所开发的独特的跨平台文件格式.PDF文件以PostScript语言图象模型为基础,无论在哪种打印机上都可保证精确的颜色和准确的打印效果,即 ...

  8. nodejs将PDF文件转换成txt文本,并利用python处理转换后的文本文件

    目前公司Web服务端的开发是用Nodejs,所以开发功能的话首先使用Nodejs,这也是为什么不直接用python转换的原因. 由于node对文本的处理(提取所需信息)的能力不强,类似于npm上的包: ...

  9. 用C#制作PDF文件全攻略

    用C#制作PDF文件全攻略 目  录 前    言... 3 第一部分 iText的简单应用... 4 第一章 创建一个Document 4 第一步 创建一个Document实例:... 5 第二步 ...

随机推荐

  1. 一个特别不错的jQuery快捷键插件:js-hotkeys

    这其实不是什么新技术,这个插件在很早前就已经发布了,之前有项目用到,所以分享出来添加方式的例子 jQuery.hotkeys.add('esc',function (){ //执行函数 }); jQu ...

  2. php安装redis扩展连接redis服务器

    扩展包的下载地址:https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz tar xf phpredis-2.2.4.tar.gz cd ...

  3. Ajax异步刷新局部页面的小李子

    看到下面那幅图没有,我们要的是当点击确定以后,根据条形码搜索出商品信息,并且异步刷新右边页面:

  4. 关于PYTHON_EGG_CACHE无权限的问题

    Perhaps your account does not have write access to this directory? You can change the cache director ...

  5. Adapter的getView方法详解

    来自:http://blog.csdn.net/yelbosh/article/details/7831812 BaseAdapter就Android应用程序中经常用到的基础数据适配器,它的主要用途是 ...

  6. 添加常驻Notification

    private static final int NOTIFICATION_ID=250; //用来标示notification,通过notificatinomanager来发布同样标示的notifi ...

  7. 生产WCF客户端类文件的命令格式

    生产WCF客户端类文件的命令格式: svcutil.exe net.tcp://127.0.0.1:8732/ChromaMI.Remote.ConfigService/RemoteConfigSer ...

  8. MVC的Filters(拦截过滤)的Error页面,支持Ajax报错

    报错拦截过滤到error页面 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, A ...

  9. 在CentOS6.5下安装Memcached

    CentOS 6.5 安装软件非常方便, yum install memcached

  10. programming ruby

    ri #rdoc reader attr_reader attr_writer @@xx 类变量都是私有的 def 类名.xx end 类方法 [1,3,5,7].inject(0){|sum,e| ...