实现pdf合并就是使用Spire.Pdf.dll类库的方法,但是注意需要同时引用Spire.Pdf.dll和Spire.License.dll两个类库,且两个类库的版本要一致

 String[] files = new String[Dir.GetFiles().Length];
int i = ;
foreach (FileInfo f in Dir.GetFiles()) //循环文件
{
files[i] = f.FullName;
i = i + ;
}
string outputFile = newPathDir + "\\" + Dir.Name+".pdf";//输出的路径
PdfDocumentBase doc =Spire.Pdf.PdfDocument.MergeFiles(files);
doc.Save(outputFile, FileFormat.PDF);
//System.Diagnostics.Process.Start(outputFile);//打开文件

但是使用spire的话合并成的pdf第一页会出现文字水印,这并不是我们要的,所以需要去除这个文字水印,那么就要用到iTextSharp.text.pdf.dll类库

 PRStream stream;
String content;
PdfArray contentarray;
string watermarkText = "要去除的水印文字";
PdfReader reader2 = new PdfReader(path);
reader2.RemoveUnusedObjects();
PdfDictionary page = reader2.GetPageN();//获取第一页
contentarray = page.GetAsArray(PdfName.CONTENTS);
if (contentarray != null)
{
//Loop through content
for (int j = ; j < contentarray.Size; j++)
{
//Get the raw byte stream
stream = (PRStream)contentarray.GetAsStream(j);
//Convert to a string. NOTE, you might need a different encoding here
content = System.Text.Encoding.ASCII.GetString(PdfReader.GetStreamBytes(stream));//获取pdf页内的文字内容
//Look for the OCG token in the stream as well as our watermarked text
if (content.IndexOf("/OC") >= || content.IndexOf(watermarkText) >= )//如果pdf内容包含水印文字
{
//Remove it by giving it zero length and zero data
content= content.Replace(watermarkText, "");//替换水印文字为空
byte[] byteArray = System.Text.Encoding.Default.GetBytes(content);//转换为byte[]
stream.Put(PdfName.LENGTH, new PdfNumber(byteArray.Length));//重新指定大小 stream.SetData(byteArray);//重新赋值
}
}
}
FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None);
PdfStamper stamper = new PdfStamper(reader2, fs);
//stamper.SetFullCompression();
if (stamper != null) {
stamper.Close();
} if (null != fs)
{
fs.Close();
} if (null != reader2)
{
reader2.Close();
}

对,就是这么简单

C#实现多个PDF合并及去除文字水印功能的更多相关文章

  1. java实现图片和pdf添加铺满文字水印

    依赖jar包 <!-- pdf start --> <dependency> <groupId>com.itextpdf</groupId> <a ...

  2. 「Python实用秘技04」为pdf文件批量添加文字水印

    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第4期 ...

  3. PDF合并

    要求:将多个table导出到一个PDF里,然后打印. 问题分析:要求将四个table放一个PDF打印,四个table的列各不相同,第一个是表头,其他三个是列表,列比表头多很多,如果直接生成一个exce ...

  4. PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题

    ---恢复内容开始--- require_once('../fpdf/fpdf.php');require_once('../fpdi/fpdi.php'); 使用此插件 pdf 合并 并添加水印 期 ...

  5. pdftk - handy tool for manipulating PDF 免费的pdf合并工具

    Linux pdf合并的工具 安装工具 $ sudo apt-get install pdftk 使用 $ pdftk *.pdf cat output all-in-one.pdf &&am ...

  6. 极客工具,PDF合并工具

    前言 这两天一番花两天的时间,重新用python和python图形化开发工具tkinter,完善了下PDF合并小工具,终于可以发布了. 工具目前基本功能已经完善,后期如果有反馈可以修复部分bug或完善 ...

  7. php如何清除html格式并去除文字中的空格然后截取文字

    PHP如何清除html格式并去除文字中的空格然后截取文字,详细分享一下处理方法(顺便对PHP清除HTML字符串的函数做了一个小结): htmlspecialchars 将特殊字元转成 HTML格式语法 ...

  8. 在线提取PDF中图片和文字

    无需下载软件,你就可以在线提取PDF中图片和文字,http://www.extractpdf.com/不仅可以获取本地PDF文档的图片和文字,还能获取远程PDF文档的图片和文字.如下图所示:结果本人测 ...

  9. 2014年去哪儿网笔试题--有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来。

    有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来. 这两个文件内容如下: context.txt “并不是每个人都需要$(qunar)自己的粮食,$(f ...

随机推荐

  1. H5中input[type="date"]默认样式修改 伪类

  2. 二、2.1 Java的下载和安装

    1.下载Java 下载地址: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...

  3. Python学习笔记,day5

    Python学习笔记,day5 一.time & datetime模块 import本质为将要导入的模块,先解释一遍 #_*_coding:utf-8_*_ __author__ = 'Ale ...

  4. hdu 2824 欧拉函数 O(nlogn) 和O(n)

    裸题 O(nlogn): #include <cstdio> #include <iostream> #include <algorithm> using name ...

  5. mininet的学习之三----------mininet中流表应用实战

    我们看的同一个视频,工整的整理见以下网址 https://blog.csdn.net/weixin_36372879/article/details/86371583#commentBox

  6. 7 种 join

    DROP TABLE IF EXISTS `test_student`; CREATE TABLE `test_student` ( `id` ) NOT NULL COMMENT '学号', `se ...

  7. Maven安装问题

    这是网上常见的解决方法,由于网络原因maven中央仓库的jar包经常加载不下来,很头疼的一个问题,如果第一个方法解决不了,那就用第二种方法 一.常见解决方法 二.第二种解决方法,使用阿里云的仓库 &l ...

  8. sum(case when ct.tradeTotal >= 0 then 1 else 0 end)的意思

    String hql = "select new com.ks.admin.report.dto.ReportMonthWithDrawalDto(" + "count( ...

  9. oracle over 函数几个例子

    测试使用的数据为scott/tiger模式下的emp表: 我们使用JOB和SAL这两个列测试: 上面语句指按照职业JOB分组(partition by job)然后在每个分组内,按照薪水(sal)进行 ...

  10. windows 8.1 启用hyper-v导致vmware 无法使用的问题解决方案(兼顾WP8.1模拟器和vmware)

    最近搭建了windows phone 8.1开发环境,为了开机就可以进行WP8.1开发,就使用了 bcdedit /set {BCD ID} hypervisorlaunchtype auto 命令将 ...