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. POJ 3253:Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33114   Accepted: 10693 De ...

  2. windows driver 分配内存

    UNICODE_STRING str = {0}; wchar_t strInfo[] = {L"马上就是光棍节了"}; str.Buffer = (PWCHAR)ExAlloca ...

  3. 创建Oracle序列sequence

    create sequence SEQ_ID minvalue 1 maxvalue 99999999 start with 1 increment by 1 nocache order; 建解发器代 ...

  4. Python属性和内建属性

    属性property 1. 私有属性添加getter和setter方法 class Money(object): def __init__(self): self.__money = 0 def ge ...

  5. pip2 install protobuf==2.6.1

    [libprotobuf FATAL google/protobuf/stubs/common.cc:61] This program requires version 3.5.0 of the Pr ...

  6. 关于Pytorch中autograd和backward的一些笔记

    参考自<Pytorch autograd,backward详解>: 1 Tensor Pytorch中所有的计算其实都可以回归到Tensor上,所以有必要重新认识一下Tensor. 如果我 ...

  7. MySQL数据类型使用总结,浮点使用注意事项

    1.对于精度要求较高的应用中,建议使用定点数来存储数值,以保证结果的准确性. 2.对于字符类型,要根据存储引擎进行相应的选择 3.对含有TEXT和BOLB字段的表,如果经常做删除和修改记录的操作要定时 ...

  8. 判断苹果和安卓端或者wp端

    window.onload = function() { var u = navigator.userAgent; if(u.indexOf('Android') > -1 || u.index ...

  9. Integer和int的区别

    1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象的引用,当new一个Integer ...

  10. Oracle 基础1

    oracle基础 表空间: Oracle数据库对数据的管理是基于表空间的概念来的, 各种数据的以及存储数据的优化, 实际上也是通过优化表空间来实现的 表空间分类: 永久表空间 用来存放表的数据, 视图 ...