/// <summary> 合併PDF檔(集合) </summary>
/// <param name="files">欲合併PDF檔之集合(一筆以上)</param>
/// <param name="pagesize">纸张大小</param>
public static MemoryStream mergePDFStream(MemoryStream[] files, Rectangle pagesize = null)
{
//outMergeFile = Server.MapPath(outMergeFile);
//if (outMergeFile == null)
//{
// outMergeFile = "d:\\" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + new Random().Next(1000) + ".pdf";
//}
PdfReader reader;
Document document;
if (pagesize == null)
{
pagesize = PageSize.A4;
} document = new Document(pagesize);
//var fs = new FileStream(outMergeFile, FileMode.Create);
MemoryStream fs = new MemoryStream();
//BufferedStream bfs = new BufferedStream(fs);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
writer.CloseStream = false; //不自动关闭内存流.
document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage newPage; for (int i = ; i < files.Length; i++)
{
//这里的是,读取字节流
reader = new PdfReader(files[i].ToArray());
int iPageNum = reader.NumberOfPages; for (int j = ; j <= iPageNum; j++)
{
document.NewPage();
newPage = writer.GetImportedPage(reader, j);
//cb.Add(newPage);
//cb.MoveTo(0, 0);
// PageSize.A4.Height - newPage.Height 解决itext 高度的问题. itext 的y 轴默认是从下面开始
cb.AddTemplate(newPage, , pagesize.Height - newPage.Height);
} } document.Close(); byte[] fileBuffer = fs.GetBuffer(); return fs;
}
//Base64文件流
byte[] buffer = Convert.FromBase64String(decodedString);
System.IO.Stream iStream = new System.IO.MemoryStream(buffer);
try
{
int length;
long dataToRead;
string filename = sn + ".pdf";//保存的文件名称
dataToRead = iStream.Length;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/pdf"; //文件类型
Response.AddHeader("Content-Length", dataToRead.ToString());//添加文件长度,进而显示进度
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
while (dataToRead > )
{
if (Response.IsClientConnected)
{
length = iStream.Read(buffer, , );
Response.OutputStream.Write(buffer, , length);
Response.Flush();
buffer = new Byte[];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -;
}
}
}
catch (Exception ex)
{
Response.Write("文件下载时出现错误!");
}
finally
{
if (iStream != null)
{
iStream.Close();
}
//结束响应,否则将导致网页内容被输出到文件,进而文件无法打开
Response.Flush();
Response.End(); }

iText C# 合并PDF文件流,以及A5变A4时内容默认放在最底下的问题的解决方法;ASP.NET 实现Base64文件流下载PDF的更多相关文章

  1. ASP.NET 实现Base64文件流下载PDF

    因为业务需要调用接口获取的是 Base64文件流 需要提供给客户下载PDF文档 源码部分借鉴网上,具体地址忘记了. //Base64文件流 byte[] buffer = Convert.FromBa ...

  2. Myeclipse中文件已经上传到server文件夹下,文件也没有被占用,可是页面中无法读取和使用问题的解决方法

    这个问题是因为Myeclipse中文件不同步引起的.在Myeclipse中,project文件是由Myeclipse自己主动扫描加入的,假设在外部改动了project文件夹中的文件但又关闭了自己主动刷 ...

  3. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

  4. Latex文件本机能正常编译,但在另一台电脑不能编译的解决方法

    问题:同样的文件在台式机能编译出正常的PDF文件,但发现在另一个电脑上不能编译出PDF文件. \documentclass[preprint,10pt,5p,times,twocolumn]{elsa ...

  5. springboot框架里的pom.xml文件里的m不显示,只有标红和<>符号的解决方法

    这是因为没有把pom.xml文件加入到maven工程中,所以需要如图所示 亲测有效,原文链接:https://blog.csdn.net/qq_41026946/article/details/107 ...

  6. 微信图片解决方法-windows版的dat文件

    public string decodeImg(string filepath) { Dictionary<string, byte[]> headers = new Dictionary ...

  7. bin文件夹下的某个dll总是自动刷新为不同版本的dll的解决方法

    如上图所示,一般这种问题都是dll版本和配置文件中的dll版本对应不上才引起的,可以通过替换对应版本的dll或者修改配置文件中的版本号即可. 然而我的情况是:修复后,还是不定时出现这样的问题,我以为是 ...

  8. 阿里OSS Vue上传文件提示The OSS Access Key Id you provided does not exist in our records.解决方法

    vue项目 1.安装OSS的Node SDK npm install ali-oss --save 2.参考官方提示https://help.aliyun.com/document_detail/11 ...

  9. pip install .whl文件时is not a supported wheel on this platform.解决方法

     首先,在python中输入import pip和print(pip.pep425tags.get_supported()),从而获取pip支持的文件名和版本. somnus@somnus-HP-Pa ...

随机推荐

  1. 写写我的硕士三年【zz】

    昨天我们组的10bit-40M ADC测出来了,自己终于能松口气,可以无牵无挂的毕业了.晚上老板bg全组毕业生,喝了很多,我对老板说:"这3年在组里,我是把它当作事业来做的!"是的 ...

  2. 初窥scrapy爬虫

    2017-10-30  21:49:55 前言: 初步使用scrapy爬虫框架,爬取各个网站信息 系统环境: 64位win10系统,装有64位python3.6,IDE为pycharm,使用cmd命令 ...

  3. 新复制行绑定JQuery.autocomplete事件

    在工作中经常需要生成动态增加行的表格,此次需要对表格中的文本框绑定autocomplete功能 操作流程: 表格初始时只有一行,当页面加载时执行先执行一次增加行功能,将行增加到10行 在$(funct ...

  4. javascript与java正则表达式写法的区别

    Js验证写法:(转义符\) var str = "待验证文本"; var regular = new RegExp(/这里是正则表达式/); if (regular.test(st ...

  5. nunit2.5.7 单元测试时提示:“当前不会命中断点 还没有为该文档加载任何符号”

    解决方案: 因为项目的“目标框架”是.net4.5 所以要将对应的 nunit.exe.config 或 nunit-x86.exe.config 文件中加上: <startup> < ...

  6. 洛谷P1433 吃奶酪【dfs】【剪枝】

    题目:https://www.luogu.org/problemnew/show/P1433 题意: 给定n个坐标,要求从(0,0)开始走遍所有点,最少经过的路程. 思路: 刚开始想像数字三角形一样适 ...

  7. .NET Core下的Socket示例.

    About.schtml中的代码 @{ ViewData["Title"] = "About"; } <h2>@ViewData["Tit ...

  8. ARCSDE直连Oracle时出现错误Failed to connect to the specified server. Underlying DBMS error[ORA-12154: TNS:could not resolve the connect identifier specified. No extended error]

    买了新笔记本,装软件. 在ARCSDE直连Oracle时遇到问题. esri官网给的解释是因为安装arcgis时安装目录里有特殊字符(详见:https://support.esri.com/en/te ...

  9. [knowledge][ETA] Encrypted Traffic Analytics

    思科ETA主页 https://www.cisco.com/c/en/us/solutions/enterprise-networks/enterprise-network-security/eta. ...

  10. [security] security engine things

    1. luarock luarock 之于 lua,就好比 pip 之于 python https://luarocks.org/ 2.  lua的库 [root@base package]# ls ...