using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.ComponentModel;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing.Printing;
using System.Drawing; namespace bcsPDFMerge
{
class iTextSharp
{
/// <summary> 合併PDF檔(集合) </summary>
/// <param name="fileList">欲合併PDF檔之集合(一筆以上)</param>
/// <param name="outMergeFile">合併後的檔名</param>
public void mergePDFFiles(List<string> fileList, string outMergeFile, bool print)
{
PdfReader reader;
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage newPage;
for (int i = 0; i < fileList.Count; i++)
{
reader = new PdfReader(fileList[i]);
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
document.NewPage();
newPage = writer.GetImportedPage(reader, j);
cb.AddTemplate(newPage, 0, 0);
}
} if (print)
{
StringBuilder script = new StringBuilder();
script.Append("this.print({bUI: true,bSilent: true,bShrinkToFit: false});"); writer.ViewerPreferences = PdfWriter.HideMenubar | PdfWriter.HideToolbar | PdfWriter.HideWindowUI;
writer.AddJavaScript(script.ToString()); //PdfAction.JavaScript("myOnMessage();", writer);
//writer.AddJavaScript("this.print(true);function myOnMessage(aMessage) {app.alert('Test',2);} var msgHandlerObject = new Object();doc.onWillPrint = myOnMessage;this.hostContainer.messageHandler = msgHandlerObject;");
} document.Close();
} }
}

调用函数:

 private const short PDSAVEFULL = 1;

 private void btnMerage_Click(object sender, EventArgs e)
{
string path = "C:\\Users\\Administrator\\Desktop\\PDFTest\\newpdf2.pdf";
iTextSharp test = new iTextSharp();
test.mergePDFFiles(FileList,path,true);
System.Diagnostics.Process.Start(path);
} List<string> FileList = new List<string>();
private void btnSelectFolder_Click(object sender, EventArgs e)
{
folderBrowserDialog.ShowDialog();
string floderpath = folderBrowserDialog.SelectedPath;
if (string.IsNullOrEmpty(floderpath)) { MessageBox.Show("path error"); return; }
DirectoryInfo theFolder = new DirectoryInfo(floderpath);
FileInfo[] fileInfo = theFolder.GetFiles();
foreach (FileInfo NextFile in fileInfo) //遍历文件
{
FileList.Add(NextFile.FullName);
}
}






使用iTextSharp來合併PDF檔的更多相关文章

  1. Linux 下編輯 PDF 檔的工具(PDF editor under Linux)(转载)

    转自:http://www.gtwang.org/2011/05/linux-pdf.html PDF 檔雖然是一個跨平台的檔案格式,但 Adobe 只有提供免費的 Adobe Reader,要看 P ...

  2. [ASP.NET] 如何利用Javascript分割檔案上傳至後端合併

    最近研究了一下如何利用javascript進行檔案分割上傳並且透過後端.特地記錄一下相關的用法 先寫限制跟本篇的一些陷阱 1.就是瀏覽器的支援了 因為本篇有用到blob跟webworker 在ie中需 ...

  3. ORACLE列值合併

    合併列值最通用的方法就是寫一個自定義函數去實現,這裏介紹的是其它方法. 在SQL Server中合併列值能够使用For Xml Path,在Oracle中則能够使用wm_concat 或 ListAg ...

  4. [ASP.NET]利用itextsharp将GridView汇出PDF档

    原文 [ASP.NET]利用itextsharp将GridView汇出PDF档 最近在讨论区看到有人说itextsharp可以把网页变成PDF 小弟就去抓一下itextsharp来玩玩,先教大家最实用 ...

  5. C#使用iTextSharp+ZXing.Net+FreeSpire.PDF生成和打印pdf文档

    项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.Net+FreeSpire.PDF三个类库实现了生成pdf.生成条形码和打印pdf功能. 首先在项 ...

  6. concat函數 函數concat 可以用來合拼兩個或以上的字串。

    12. “Mexico 墨西哥”的首都是”Mexico City”. 顯示所有國家名字,其首都是國家名字加上”City”. concat函數 函數concat 可以用來合拼兩個或以上的字串. : SE ...

  7. ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下

    ADO.NET   一.ADO.NET概要 ADO.NET是.NET框架中的重要组件,主要用于完成C#应用程序访问数据库 二.ADO.NET的组成 ①System.Data  → DataTable, ...

  8. C#&.Net干货分享- iTextSharp导出数据源到PDF

    namespace Frame.ITextSharp{    /// <summary>    /// iTextSharp导出数据源到PDF    /// </summary> ...

  9. ITextSharp用来生成 PDF 的一个组件

    iTextSharp 是用来生成  PDF 的一个组件,在 1998 年夏天的时候,Bruno Lowagie ,iText 的创作者,参与了学校的一个项目,当时使用 HTML 来生成报告,但是,使用 ...

随机推荐

  1. JSP标签 <fmt:formatDate>格式化日期

    <fmt:formatDate>标签用于使用不同的方式格式化日期. <%@ page language="java" contentType="text ...

  2. 51nod 1105:第K大的数

    1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...

  3. Linux系统中的变量PATH

    PATH 作用 在Linux安装一些可执行文件通常要添加路径环境变量PATH,PATH环境变量通俗的讲就是把程序的路径"备案"到系统中,这样执行这些程序时就不需要输入完整路径,直接 ...

  4. 四个因素决定Essay写作段落长度

    段落是一篇Essay的基石,写好Essay应从写好段落开始.那么Essay写作中一个段落多长为好?英语和修辞学教授理查德·诺德奎斯特著文介绍了一些专家的观点.从以下的译文可以看到,段落长度虽然没有固定 ...

  5. c#textBox控件限制只允许输入数字及小数点,是否为空

    c#textBox控件限制只允许输入数字及小数点 转载 //判断按键是不是要输入的类型. if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) ...

  6. Cannot access android.support.v4.app.*

    解决办法: 添加到受影响的 module build.gradle 中(比如app的gradle文件根代码下) configurations.all { resolutionStrategy.each ...

  7. Mybatis实现增删改查(二)

    1. 准备 请先完成Mybatis基本配置(一)的基本内容 2. 查询多条商品信息 1)在com.mybatis.dao.PartDao中增加接口函数 public List<PartInfo& ...

  8. jvm调优原则

    合理规划jvm性能调优 JVM性能调优涉及到方方面面的取舍,往往是牵一发而动全身,需要全盘考虑各方面的影响.但也有一些基础的理论和原则,理解这些理论并遵循这些原则会让你的性能调优任务将会更加轻松.为了 ...

  9. JAVA初学者——Hello,World!

    大家好,我是浩宇大熊猫 我本科专业学的是GIS(Geographical Information System),大学期间也学习了很多的编程语言,有C/C++/JAVA等 之前给我们授课的是韩冰老师, ...

  10. shell字符串大小写转换

    1.typeset  有两个选项 -l 代表小写 -u 代表大写. 用法: typeset -u name name='asdasdas' echo $name   typeset -l ame am ...