使用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& ...
随机推荐
- 【python】udp 数据的发送和接收
import socket def send_message(): # 创建一个udp套接字 udp_socker = socket.socket(socket.AF_INET,socket.SOCK ...
- PB连接数据库
SQLCA.DBMS = "ODBC" SQLCA..AutoCommit = False SQLCA.DBParm = "ConnectString='DSN=fire ...
- 注入 Istio sidecar
注入 Istio sidecar 网格中的每个 Pod 都必须伴随一个 Istio 兼容的 Sidecar 一同运行. 下文中将会介绍两种把 Sidecar 注入到 Pod 中的方法:使用 istio ...
- 【监控】jvisualvm之jmx远程连接 tomcat war启动应用
一.tomcat相应jmx配置 1.在tomcat bin目录下的catalina.sh文件中添加如下配置: CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun. ...
- Linux 运维入门到跑路书单推荐
一.基础入门 <鸟哥的Linux私房菜基础学习篇>:最具知名度的Linux入门书<鸟哥的Linux私房菜基础学习篇>,全面而详细地介绍了Linux操作系统. https://b ...
- 将mysql从MyISAM更改为INNODB
今天更新django中的表字段,由于mysql从5.1升级到5.7.以前的外键关联必须从MYISAM改新为INNODB才可以继续. 过程有点刺激,但还好,只要想清楚了过程,提前作好备份,就没啥大问题. ...
- oracle 将与本端(name)联系的人取出
本人与其他所有人认识的SQL: 首先新建测试表 create table DIM_IA_TEST6 ( NAME ), OTHERNAME ) ) 插入数据 --如果没有重复的记录,则不用去重使用un ...
- NPM——npm|cnpm如何升级
前言 手动更新了node.js版本后,想要升级下npm的版本 步骤 其实无论npm还是cnpm升级的命令都是一样的,除了需要指定包名. 升级npm $ npm install -g npm 查看npm ...
- 小米BL不解锁刷机
关于小米NOTE顶配近期解锁的问题中发现还有很多人不会用9008模式刷机,现出个简单教程方便米粉们救砖.硬件:小米NOTE顶配手机 win10系统的电脑 手机与电脑相连的数据线软件:老版本的mifla ...
- 写一段程序,删除字符串a中包含的字符串b,举例 输入a = "asdw",b = "sd" 返回 字符串 “aw”;一个容易被忽略的bug
代码如下: public class test{ public static void main(String args[]){ String test=test("sahsjkshabsh ...