iText C# 合并PDF文件流,以及A5变A4时内容默认放在最底下的问题的解决方法;ASP.NET 实现Base64文件流下载PDF
/// <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的更多相关文章
- ASP.NET 实现Base64文件流下载PDF
因为业务需要调用接口获取的是 Base64文件流 需要提供给客户下载PDF文档 源码部分借鉴网上,具体地址忘记了. //Base64文件流 byte[] buffer = Convert.FromBa ...
- Myeclipse中文件已经上传到server文件夹下,文件也没有被占用,可是页面中无法读取和使用问题的解决方法
这个问题是因为Myeclipse中文件不同步引起的.在Myeclipse中,project文件是由Myeclipse自己主动扫描加入的,假设在外部改动了project文件夹中的文件但又关闭了自己主动刷 ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- Latex文件本机能正常编译,但在另一台电脑不能编译的解决方法
问题:同样的文件在台式机能编译出正常的PDF文件,但发现在另一个电脑上不能编译出PDF文件. \documentclass[preprint,10pt,5p,times,twocolumn]{elsa ...
- springboot框架里的pom.xml文件里的m不显示,只有标红和<>符号的解决方法
这是因为没有把pom.xml文件加入到maven工程中,所以需要如图所示 亲测有效,原文链接:https://blog.csdn.net/qq_41026946/article/details/107 ...
- 微信图片解决方法-windows版的dat文件
public string decodeImg(string filepath) { Dictionary<string, byte[]> headers = new Dictionary ...
- bin文件夹下的某个dll总是自动刷新为不同版本的dll的解决方法
如上图所示,一般这种问题都是dll版本和配置文件中的dll版本对应不上才引起的,可以通过替换对应版本的dll或者修改配置文件中的版本号即可. 然而我的情况是:修复后,还是不定时出现这样的问题,我以为是 ...
- 阿里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 ...
- pip install .whl文件时is not a supported wheel on this platform.解决方法
首先,在python中输入import pip和print(pip.pep425tags.get_supported()),从而获取pip支持的文件名和版本. somnus@somnus-HP-Pa ...
随机推荐
- sometimes we should use "disable fork" instead of "disable block_name"
A disable named block statement stops the execution of all blocks with that same name in all threads ...
- jenkins编辑报错Exception when publishing, exception message的解决办法
jenkins编辑报错Exception when publishing, exception message的解决办法 查看目标主机的磁盘空间是否占满,清理磁盘空间即可
- Maven知识点积累一
配置maven变量,变量名可以是:MAVEN_HOME 或 M2_HOME settings.xml配置本地仓库地址: <localRepository>G:/.m2/repository ...
- .NET Core开发日志——Linux版本的SQL Server
SQL Server 2017版本已经可以在Linux系统上安装,但我在尝试.NET Core跨平台开发的时候使用的是Mac系统,所以这里记录了在Mac上安装SQL Server的过程. 最新的SQL ...
- [No0000118]SQL Server附加数据库拒绝访问解决方法汇总
修改权限 打开要附加的数据库文件所在的文件夹,右键单击mdf文件,选择“属性”: 单击“安全”选项卡,给所有用户添加读写权限. 最后点击"确定"就可以了.修改权限完成后,你就可以成 ...
- Spring <context:annotation-config> 与<context-component-scan> 的作用
<context:annotation-config> 与<context-component-scan> 的作用 <context:annotation-config& ...
- jcgrid
1.jcgrid实现 <span class="JcGrid" id="MyGrid" listdata="Rds.List.UserList& ...
- 托布利兹变换 toeplitz 变换
托布利兹变换 toeplitz 变换 算术平均变换 '''An->C=>(A1+A2+A3+...+An)/n->C,K_ni=1/n=>+[AiK_ni->C (Yn- ...
- Cookie映射
Cookie映射 第 5 章 Cookie映射 http://amp.ad.sina.com.cn/sax/doc/zh-CN/xhtml/bk01pt02ch05.xhtml 第 5 章 Cooki ...
- c#构造函数对string类型赋初值
public class Stu { public Stu() { //当成员属性非常多难以一一赋值时,采用本方法.当然写代码逐一成员直接赋值效率更高. AssignEmptyStringMember ...