使用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& ...
随机推荐
- 【书评:Oracle查询优化改写】第四章
[书评:Oracle查询优化改写]第四章 BLOG文档结构图 一.1 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① check的 ...
- appium自动化webview时遇到的chromedriver问题
安卓app里面的网页,基本上都是使用手机系统上的webview 去显示的. 安卓 webview 可以看成是 手机上的 chrome 浏览器精简版. appium desktop 里面内置了 用于 w ...
- Yii2 路由美化
一.美化路由形式 如:localhost/index.php?r=site/index 这种路由形式对SEO不友好,那么是否可以对路由进行一下美化呢?在Yii2中我们可以将路由必成以下的形式: 如:l ...
- Linux kernel启动选项(参数)(转)
Linux kernel启动选项(参数) 转载链接https://www.cnblogs.com/linuxbo/p/4286227.html 在Linux中,给kernel传递参数以控制其行为总共 ...
- H3C 帧聚合
- Locust性能模块浅谈
今天接触到Locust性能模块,下面介绍一下安装与简单的应用 1.安装方式:pip install Locust Locust支持Python 2.7, 3.4, 3.5, and 3.6的版本,小编 ...
- java继承 、方法重写、重写toString方法
1.Java的继承,关键词Extends package cn.mwf.oo; public class TextExtends { public static void main(String[] ...
- linux中container_of
linux 驱动程序中 container_of宏解析 众所周知,linux内核的主要开发语言是C,但是现在内核的框架使用了非常多的面向对象的思想,这就面临了一个用C语言来实现面向对象编程的问题,今天 ...
- K8s基本概念入门
序言 没等到风来,绵绵小雨,所以写个随笔,聊聊k8s的基本概念. k8s是一个编排容器的工具,其实也是管理应用的全生命周期的一个工具,从创建应用,应用的部署,应用提供服务,扩容缩容应用,应用更新,都非 ...
- Xenia and Weights(Codeforces Round #197 (Div. 2)+DP)
题目链接 传送门 思路 \(dp[i][j][k]\)表示第\(i\)次操作放\(j\)后与另一堆的重量差为\(k\)是否存在. 代码实现如下 #include <set> #includ ...