通用的合并方式导致输出的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. php的多功能文件操作类

    本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录,列出目录里的文件等功能,路径后面别忘了加"/" 创建指定路径下的指定文件 * @param s ...

  2. C++类中创建线程

    ​ 经常会遇到需要在类中创建线程,可以使用静态成员函数,并且将类实例的指针传入线程函数的方式来实现. 实现代码代码如下: /* 类头文件 CTestThread.h */ #include<io ...

  3. OpenStack共享组件-Memcache缓存系统

    1. 缓存系统 1.1 静态web页面 1> 在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Requ ...

  4. gcc的__builtin_函数(注意前面是两个下划线)

    说明: GCC provides a large number of built-in functions other than the ones mentioned above. Some of t ...

  5. 项目Alpha冲刺(团队)-测试篇

    格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队)-代码规范.冲刺任务与计划 团队名称:为了交项目干杯 测试用例:测试用例文档.zip 作业目标:描述项目的测试 ...

  6. python 查询文件修改python lib 库文件

    运行code import os, time import sys import re def search(path, name): for root, dirs, files in os.walk ...

  7. 前端知识--控制input按钮的显示与隐藏

    if(fm.ReadFlag.value=="readonly"){ var arr = document.getElementsByTagName("input&quo ...

  8. mybatis的注意事项一

    在UserMapper.xml文件中写resultType="cn.smbms.dao.pojo.User"返回类型的全路径是不是很长,而且也比较不美观:不便于后期项目的维护. 解 ...

  9. Python微信操控(itchat)

    itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单. 开源地址 https://github.com/littlecodersh/ItChat 文档: https://itc ...

  10. [转载]XML非法字符的处理

    https://blog.csdn.net/qq_36330228/article/details/84779390 static void Main(string[] args) { string ...