使用ItextSharop合并pdf文件,体积变大的解决
通用的合并方式导致输出的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文件,体积变大的解决的更多相关文章
- ImageMagick convert多张照片JPG转成pdf格式,pdfunite合并PDF文件
在认识ImageMagick之前,我***的图像浏览软件是KuickShow,截图软件是KSnapShot,这两款软件都是KDE附带的软件,用起来也是蛮方便的.在一次偶然的机会中,我遇到了Imag ...
- webpack打包经验——处理打包文件体积过大的问题
前言 最近对一个比较老的公司项目做了一次优化,处理的主要是webpack打包文件体积过大的问题. 这里就写一下对于webpack打包优化的一些经验. 主要分为以下几个方面: 去掉开发环境下的配置 Ex ...
- 使用Python批量合并PDF文件(带书签功能)
网上找了几个合并pdf的软件,发现不是很好用,一般都没有添加书签的功能. 又去找了下python合并pdf的脚本,发现也没有添加书签的功能的. 于是自己动手编写了一个小工具,使用了PyPDF2. 下面 ...
- Aspose.Pdf合并PDF文件
使用Aspose.Pdf类库,有很多种方法可以合并PDF文件,这里简单介绍小生见到的几种: Doucment.Pages.Add PdfFileEditor.Append PdfFileEditor. ...
- Java 合并PDF文件
处理PDF文档时,我们可以通过合并的方式,来任意合并几个不同的PDF文件,使我们方便的存储和管理文档.例如,在做毕业设计的时候,封面和论文正文往往是两个PDF文档,但是,上交电子档的时候,需要合二为一 ...
- 使用PyPdf2合并PDF文件(没有空白、报错)
使用PyPdf2合并PDF文件(没有空白.报错) 对于合并之后pdf空白,或者出现 'latin-1' codec can't encode characters in position 8-11: ...
- vs2010/2013项目的C++所在文件夹越来越大如何解决?
vs2010/2013项目所在文件夹越来越大如何解决? Tools->Options->Text Editor->C/C++->Advanced,在 Fallback Loca ...
- Response.Write()方法响应导致页面字体变大的解决办法
关于ASP.NET中用Response.Write()方法响应导致页面字体变大的解决办法 最近研究了ASP.NET,发现一个问题,比方说在页面里面有个Button,要点击以后要打开新窗口,而且 ...
- html标签被div嵌套页面字体变大的解决办法
html标签被div嵌套页面字体变大的解决办法 <div> <html> <head> <title></title> </head& ...
随机推荐
- select下拉框小DemoA
<html> <head> <meta charset="utf-8"> <script src="jquery-1.9.1.m ...
- BootstrapValidator 表单验证超详细教程
一. 引入js 和css文件 在有jquery和bootstrap的页面里引入 bootstrapValidator.js bootstrapValidator.css 链接: https://pan ...
- K8S 部署 ingress-nginx 配置 https
生成证书 mkdir cert && cd cert # 生成私钥 tls.key, 密钥位数是 2048 openssl genrsa -out tls.key 2048 # 使用 ...
- JSON的Go解析
JSON(Javascript Object Notation)是一种轻量级的数据交换语言,以文字为基础,具有自我描述性且易于让人阅读.尽管JSON是Javascript的一个子集,但JSON是独立于 ...
- Python入门篇-函数、参数及参数解构
Python入门篇-函数.参数及参数解构 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.函数概述 1>.函数的作用即分类 函数 数学定义:y=f(x) ,y是x的函数,x ...
- AXURE RP EXTENSION For Chrome----解决办法
出现这个问题是因为chrome://extensions/中没有安装扩展程序 解决办法: 步骤一:情景再现,打开某个html会出现如下页面,不停地提示你安装插件 步骤二:如果点击安装扩展程序,能够成功 ...
- 洛谷 P2725 邮票题解
题目背景 给一组 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K —— 表示信封上能够贴 K 张邮票.计算从 1 到 M 的最大连续可贴出的邮资. 题目描述 例如,假设有 1 分和 3 ...
- TCN时间卷积网络——解决LSTM的并发问题
TCN是指时间卷积网络,一种新型的可以用来解决时间序列预测的算法.在这一两年中已有多篇论文提出,但是普遍认为下篇论文是TCN的开端. 论文名称: An Empirical Evaluation of ...
- django migrate报错:1005 - Can't create table xxx (errno: 150 "Foreign key constraint is incorrectly formed")
自从mysql升级,以及使用mariaDB以来,很多不曾更新django中model的外键, 今天,按以前的思路写完外键之后, migrate命令报错: 1005 - Can't create tab ...
- Android init介绍(上)
1. 介绍 init进程是Linux系统第一个用户进程,是Android系统应用程序的根进程,即1号进程(PID为1):Android中的init文件位于/init,代码位于system/core/i ...