通用的合并方式导致输出的pdf 文件中嵌入了大量的重复字体。导致文件体积膨胀。

使用基于内存流的方式,读取文件字节,可以解决重复字体的嵌入问题;

    public static string MergeFiles(string targetPdfFilesDir)
{
string outPath = string.Empty;
//验证文件是否存在
if (!Directory.Exists(targetPdfFilesDir))
{
throw new FileNotFoundException("指定的目录不存在:" + targetPdfFilesDir);
} var filePathList = Directory.EnumerateFiles(targetPdfFilesDir, "*.pdf");
if (filePathList.IsEmpty())
{
return outPath;
} //合并pdf文件 string runningDir = AppDomainTypeFinder.Instance.GetBinDirectory(); outPath = Path.Combine(runningDir, "temp", Guid.NewGuid().ToString() + ".pdf"); MergeFiles(outPath, filePathList.ToArray()); return outPath;
} public static void MergeFiles(string destinationFile, string[] sourceFiles)
{ try
{ byte[] bs = MergePDFs(sourceFiles);
using (var fsm = new FileStream(destinationFile, FileMode.Create))
{
fsm.Write(bs, , bs.Length);
fsm.Flush();
} }
catch (Exception e)
{
string strOb = e.Message;
}
}
/// <summary>
/// 合并多个pdf文件,并返回合并后的文件字节
/// </summary>
/// <param name="pdfFiles"></param>
/// <returns></returns>
private static byte[] MergePDFs(string[] pdfFiles)
{
if (pdfFiles == null || pdfFiles.Length <= )
{
return null;
}
if (pdfFiles.Length == )
{
return File.ReadAllBytes(pdfFiles[]);
} PdfReader reader;
Document document;
PdfWriter writer;
MemoryStream msFinalPdf;
using (msFinalPdf = new MemoryStream())
{ reader = new PdfReader(pdfFiles[]);
using (document = new Document())
{
//一个PdfSmartCopy基类
writer = new PdfSmartCopy(document, msFinalPdf);
document.Open(); for (int k = ; k < pdfFiles.Length; k++)
{
reader = new PdfReader(pdfFiles[k]);
//将子文件中的页都追加到尾部
for (int i = ; i < reader.NumberOfPages + ; i++)
{
((PdfSmartCopy)writer).AddPage(writer.GetImportedPage(reader, i));
}
writer.FreeReader(reader); }
reader.Close();
writer.Close();
document.Close();
}
} return msFinalPdf.ToArray();
} 下面的代码合并pdf 就会产生体积增加。字体重复被嵌入的问题
public static void MergeFiles(string destinationFile, string[] sourceFiles)
{ try
{
int f = 0;
// we create a reader for a certain document
PdfReader reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
//Console.WriteLine("There are " + n + " pages in the original file.");
// step 1: creation of a document-object
Document document = new Document(reader.GetPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
// step 3: we open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
// step 4: we add content
while (f < sourceFiles.Length)
{
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
//Console.WriteLine("Processed page " + i);
}
f++;
if (f < sourceFiles.Length)
{
reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
n = reader.NumberOfPages;
//Console.WriteLine("There are " + n + " pages in the original file.");
}
}
// step 5: we close the document
document.Close();
}
catch (Exception e)
{
string strOb = e.Message;
}
}
----下面也会产生重复字体嵌入的----
 public static void Merge(List<String> InFiles, String OutFile)
{ using (FileStream stream = new FileStream(OutFile, FileMode.Create))
using (Document doc = new Document())
using (PdfCopy pdf = new PdfCopy(doc, stream))
{
doc.Open(); PdfReader reader = null;
PdfImportedPage page = null; //fixed typo
InFiles.ForEach(file =>
{
reader = new PdfReader(file); for (int i = 0; i < reader.NumberOfPages; i++)
{
page = pdf.GetImportedPage(reader, i + 1);
pdf.AddPage(page);
} pdf.FreeReader(reader);
reader.Close();
File.Delete(file);
});
}

使用ItextSharop合并pdf文件,体积变大的解决的更多相关文章

  1. ImageMagick convert多张照片JPG转成pdf格式,pdfunite合并PDF文件

      在认识ImageMagick之前,我***的图像浏览软件是KuickShow,截图软件是KSnapShot,这两款软件都是KDE附带的软件,用起来也是蛮方便的.在一次偶然的机会中,我遇到了Imag ...

  2. webpack打包经验——处理打包文件体积过大的问题

    前言 最近对一个比较老的公司项目做了一次优化,处理的主要是webpack打包文件体积过大的问题. 这里就写一下对于webpack打包优化的一些经验. 主要分为以下几个方面: 去掉开发环境下的配置 Ex ...

  3. 使用Python批量合并PDF文件(带书签功能)

    网上找了几个合并pdf的软件,发现不是很好用,一般都没有添加书签的功能. 又去找了下python合并pdf的脚本,发现也没有添加书签的功能的. 于是自己动手编写了一个小工具,使用了PyPDF2. 下面 ...

  4. Aspose.Pdf合并PDF文件

    使用Aspose.Pdf类库,有很多种方法可以合并PDF文件,这里简单介绍小生见到的几种: Doucment.Pages.Add PdfFileEditor.Append PdfFileEditor. ...

  5. Java 合并PDF文件

    处理PDF文档时,我们可以通过合并的方式,来任意合并几个不同的PDF文件,使我们方便的存储和管理文档.例如,在做毕业设计的时候,封面和论文正文往往是两个PDF文档,但是,上交电子档的时候,需要合二为一 ...

  6. 使用PyPdf2合并PDF文件(没有空白、报错)

    使用PyPdf2合并PDF文件(没有空白.报错) 对于合并之后pdf空白,或者出现 'latin-1' codec can't encode characters in position 8-11: ...

  7. vs2010/2013项目的C++所在文件夹越来越大如何解决?

    vs2010/2013项目所在文件夹越来越大如何解决? Tools->Options->Text Editor->C/C++->Advanced,在 Fallback Loca ...

  8. Response.Write()方法响应导致页面字体变大的解决办法

    关于ASP.NET中用Response.Write()方法响应导致页面字体变大的解决办法     最近研究了ASP.NET,发现一个问题,比方说在页面里面有个Button,要点击以后要打开新窗口,而且 ...

  9. html标签被div嵌套页面字体变大的解决办法

    html标签被div嵌套页面字体变大的解决办法 <div> <html> <head> <title></title> </head& ...

随机推荐

  1. Centos7搭建DockerRegistry

    1. 说明 以下使用系统centos7,64位,镜像为CentOS-7-x86_64-Minimal-1804,均已root用户进行操作 2. 安装Registry Docker Registry 是 ...

  2. httpd的压力测试工具-ab工具使用案例

    httpd的压力测试工具-ab工具使用案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.httpd自带的工具程序 事实上,在我们安装完Apache之后,它默认就会给我们安 ...

  3. svn 没有killall命令的解决方法 -bash: killall: command not found

    debian.ubuntu系统下:   apt-get install psmisc centos 下:   yum install psmisc

  4. HBase应用

    太多column family的影响 每个 MemoryStore分配到的内存较少,进而导致过多的合并,影响性能   几个column family比较合适呢 推荐是:1-3个 划分column fa ...

  5. ccf算法模板

    bellman ford 算法求最短路径 #include <iostream> using namespace std; ; ; // 边, typedef struct Edge{ i ...

  6. ajax 样式

    Ajax 由 HTML.JavaScript™ 技术.DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序.它是一种构建网站的强大方法. 使用aja ...

  7. WinDbg常用命令系列---?*

    ? (Command Help) 问号(?)字符显示所有命令和运算符的列表.问号本身显示命令帮助. 环境 模式 用户模式下,内核模式 目标 实时. 崩溃转储 平台 全部 0:000> ? Ope ...

  8. pgloader 学习(二)特性矩阵&&命令行

    pgloader 对于各种数据库支持的还是很完整的,同时有一套自己的dsl 特性矩阵 操作命令 命令格式 pgloader [<options>] [<command-file> ...

  9. cube.js 学习(十一)cube + gitbase 分析git 代码

    这个是一个简单的demo,使用gitbase+cube 分析git 仓库代码 需求 我们平时使用的gitlab,或者gogs 等git 仓库管理工具,有自己的管理强项,但是对于分析上可能就不是那么强大 ...

  10. Html结构标准

    < ! doctype html> 声明文档类型 <html> 根标签 <head> 头标签 <title></title> 标题标签 &l ...