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. python 列表和字符串

    python 列表中保留所有字符串前三项,并保存到一个新的列表l = [s[:3] for s in data] python 在列表中查找包含所以某个字符串的项,并保存到一个新的列表l = [s f ...

  2. 洛谷 P1504 积木城堡

    题目传送门 解题思路: 01背包. AC代码: #include<iostream> #include<cstdio> #include<vector> using ...

  3. 17 —— 服务端渲染 —— art-template

    一,前端渲染数据 的弊端 仿 apache 服务器与客户端的几次交互: 1,加载静态页面 2,加载静态资源 3,发送 ajax 请求 ,接收请求并处理返回 . 4,前端浏览器接收数据循环遍历. 存在的 ...

  4. cf 261B.Maxim and Restaurant

    什么什么期望的,不会! (题解http://blog.sina.com.cn/s/blog_140e100580102wj4e.html(看不懂)) #include<bits/stdc++.h ...

  5. Canvas绘制水波进度加载

    效果: 用到图片下载: 自定义View: package com.czm.mysinkingview; import android.content.Context; import android.g ...

  6. Django xadmin图片上传与缩略图处理

    基本摘要 用python django开发时,个人选中Xadmin后台管理系统框架,因为它*内置功能丰富, 不仅提供了基本的CRUD功能,还内置了丰富的插件功能.包括数据导出.书签.图表.数据添加向导 ...

  7. 生成私钥、公钥,配置到Git上

    ssh-keygen -t rsa -C "1032671220@qq.com" 输入完毕指令,输入密码.然后会在 /z/.ssh/文件夹下生成一个私钥rsa_id.公钥rsa_p ...

  8. PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]

    题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...

  9. 第四章:Schema与数据类型优化

    1. 选择优化的数据类型 选择数据类型的原则 更小的通常更好:选择可以正确存储数据的最小数据类型 小的数据类型消耗更少的内存.CPU;占用更少的磁盘 选用简单的数据类型:简单的数据类型通常需要更少的C ...

  10. linux 安装禅道 和 CentOS 7 开放防火墙端口 命令

    linux 安装禅道链接:  https://www.cnblogs.com/maohuidong/p/9750202.html CentOS 7 开放防火墙端口 命令 链接:https://www. ...