/// <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. ThinkPHP框架 表单传值自动验证!!

    如果有多个字段都包含错误,默认只显示一个错误.如果想显示全部错误,可以设置属 性: //批量验证 d protected $patchValidate = true; 如果是直接 POST 过来的,直 ...

  2. df

    hdu 1052 Tian Ji -- The Horse Racing (2011-08-26 08:32:51) 转载▼ 标签: 杂谈 分类: acm杂谈 Tian Ji -- The Horse ...

  3. 洛谷P1074 靶形数独【dfs】【剪枝】

    题目:https://www.luogu.org/problemnew/show/P1074 题意: 数独的分数如下.一个数独的总分数就是权值乘所填数字之和. 现在给一个未完成的数独,问分数最高的数独 ...

  4. python字符串魔法

    test = 'alEx' print(test.capitalize()) # 首字母大写 其他都变小写 print(test.casefold()) # 变小写 更牛逼 print(test.lo ...

  5. Spring 嵌套方法AOP不生效问题

    问题描述, 如下Abc定义为一个Bean, b()方法添加@TargetDatasource,定义切面DynamicDataSourceAspect,期望:调用a()方法,b()方法上的AOP拦截能生 ...

  6. 【视频】dx dy的意思 微分的定义 导数符号的意思

    视频解说 http://www.bilibili.com/video/av8565224/

  7. 闭区间套定理(Nested intervals theorem)讲解1

    ① ②这里用到了极限与不等关系 ③如果a≠b,那么便不会有$\lim _{n\rightarrow \infty }\left| I_n \right| =0$ ④如果还存在一点c在 内,那么同样也不 ...

  8. CSS:盒模型和position定位

    盒模型 页面上显示的每个元素(包括内联元素)都可以看作一个盒子,即盒模型( box model ).请看Chrome DevTools 里的截图: 可以显而易见的看出盒模型由 4 部分组成.从内到外分 ...

  9. CSS盒子模型(简要了解)

    CSS中, Box Model叫盒子模型(或框模型),Box Model规定了元素框处理元素内容(element content).内边距(padding).边框(border) 和 外边距(marg ...

  10. [elastic search][redis] 初试 ElasticSearch / redis

    现有项目组,工作需要. http://www.cnblogs.com/xing901022/p/4704319.html Elastic Search权威指南(中文版) https://es.xiao ...