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 = 0; i < files.Length; i++)
            { 
            //这里的是,读取字节流
                reader = new PdfReader(files[i].ToArray());  
                int iPageNum = reader.NumberOfPages;

                for (int j = 1; 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, 0, 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 > 0)
               {
                   if (Response.IsClientConnected)
                   {
                       length = iStream.Read(buffer, 0, 10000);
                       Response.OutputStream.Write(buffer, 0, length);
                       Response.Flush();
                       buffer = new Byte[10000];
                       dataToRead = dataToRead - length;
                   }
                   else
                   {
                       dataToRead = -1;
                   }
               }
           }
           catch (Exception ex)
           {
               Response.Write("文件下载时出现错误!");
           }
           finally
           {
               if (iStream != null)
               {
                   iStream.Close();
               }
               //结束响应,否则将导致网页内容被输出到文件,进而文件无法打开  
               Response.Flush();
               Response.End();
 
           }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值