abcPdf 用法

Code
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using WebSupergoo.ABCpdf5;
  5
  6namespace *****.model
  7{
  8    public class PdfHelper : IDisposable
  9    {
 10        /**//// <summary>
 11        /// save pdf file as a local file
 12        /// </summary>
 13        /// <param name="htmlContext"></param>
 14        /// <param name="pdfSavePath"></param>

 15        public static void SaveAsFile(string htmlContext, string pdfSavePath)
 16        {
 17            using (Doc pdfDoc = new Doc())
 18            {
 19                string tempFile = System.Web.HttpContext.Current.Server.MapPath("~/Calculator/temp/"+ "temp." + DateTime.Now.Millisecond.ToString() + "." + DateTime.Now.ToString("MM-dd-yyyy-hh"+ ".html";
 20                System.IO.StreamWriter strWriter = new System.IO.StreamWriter(tempFile);
 21                strWriter.Write(htmlContext);
 22                strWriter.Close();
 23                strWriter = null;
 24                int pdfId = 0;
 25                pdfDoc.HPos = .3;
 26                double scale = .8;
 27                int x = 30;
 28                int y = 50;
 29                pdfDoc.Rect.Magnify(1 / scale, 1 / scale);
 30                pdfDoc.Transform.Magnify(scale, scale, 00);
 31                pdfDoc.Rect.Inset(x, y);
 32                try
 33                {
 34                    pdfId = pdfDoc.AddImageUrl("file:///" + tempFile);
 35                    while (pdfDoc.Chainable(pdfId))
 36                    {
 37                        pdfDoc.Page = pdfDoc.AddPage();
 38                        pdfId = pdfDoc.AddImageToChain(pdfId);
 39                    }

 40                    for (int i = 0; i < pdfDoc.PageCount - 1; i++)
 41                    {
 42                        pdfDoc.PageNumber = i;
 43                        pdfDoc.Flatten();
 44                    }

 45                    pdfDoc.Save(pdfSavePath);
 46                }

 47                catch (Exception ex)
 48                {
 49
 50                    throw ex;
 51                }

 52                finally
 53                {
 54                    System.IO.File.Delete(tempFile);
 55                    pdfDoc.Delete(pdfId);
 56                    pdfDoc.Clear();
 57                }

 58
 59            }

 60        }

 61
 62        /**//// <summary>
 63        /// get the pdf byte[]
 64        /// </summary>
 65        /// <param name="htmlContext"></param>
 66        /// <returns></returns>

 67        public static byte[] GetBytes(string htmlContext)
 68        {
 69            byte[] context = null;
 70            Doc pdfDoc = new Doc();
 71            string tempFile = System.Web.HttpContext.Current.Server.MapPath("~/Calculator/temp/"+ "temp." + DateTime.Now.Millisecond.ToString() + "." + DateTime.Now.ToString("MM-dd-yyyy-hh"+ ".html";
 72            System.IO.StreamWriter strWriter = new System.IO.StreamWriter(tempFile);
 73            strWriter.Write(htmlContext);
 74            strWriter.Close();
 75            strWriter = null;
 76            int pdfId = 0;
 77            pdfDoc.HPos = .3;
 78            double scale = .8;
 79            int x = 30;
 80            int y = 50;
 81            pdfDoc.Rect.Magnify(1 / scale, 1 / scale);
 82            pdfDoc.Transform.Magnify(scale, scale, 00);
 83            pdfDoc.Rect.Inset(x, y);
 84            try
 85            {
 86                pdfId = pdfDoc.AddImageUrl("file:///" + tempFile);
 87                while (pdfDoc.Chainable(pdfId))
 88                {
 89                    pdfDoc.Page = pdfDoc.AddPage();
 90                    pdfId = pdfDoc.AddImageToChain(pdfId);
 91                }

 92                for (int i = 0; i < pdfDoc.PageCount - 1; i++)
 93                {
 94                    pdfDoc.PageNumber = i;
 95                    pdfDoc.Flatten();
 96                }

 97                context = pdfDoc.GetData();
 98            }

 99            catch (Exception ex)
100            {
101
102                throw ex;
103            }

104            finally
105            {
106                System.IO.File.Delete(tempFile);
107                pdfDoc.Delete(pdfId);
108                pdfDoc.Clear();
109            }

110
111            return context;
112        }

113
114        public static byte[] PdfBytes(StringBuilder htmlcontext)
115        {
116            byte[] temp = null;
117            using (Doc pdf = new Doc())
118            {
119                int pdfId = 0;
120                pdf.HPos = .3;
121                double scale = .8;
122                int x = 30;
123                int y = 50;
124                pdf.Rect.Magnify(1 / scale, 1 / scale);
125                pdf.Transform.Magnify(scale, scale, 00);
126                pdf.Rect.Inset(x, y);
127                try
128                {
129                    //pdfId = pdfDoc.AddImageUrl("file:///" + tempFile);
130                    pdfId = pdf.AddImageHtml(htmlcontext.ToString());
131                    while (pdf.Chainable(pdfId))
132                    {
133                        pdf.Page = pdf.AddPage();
134                        pdfId = pdf.AddImageToChain(pdfId);
135                    }

136                    for (int i = 0; i < pdf.PageCount - 1; i++)
137                    {
138                        pdf.PageNumber = i;
139                        pdf.Flatten();
140                    }

141                    temp = pdf.GetData();
142                }

143                catch (Exception ex)
144                {
145
146                    throw ex;
147                }

148                finally
149                {
150                    pdf.Delete(pdfId);
151                    pdf.Clear();
152
153                }
 return temp;
154            }

155        }

156        IDisposable Members#region IDisposable Members
157
158        public void Dispose()
159        {
160        }

161
162        #endregion

163    }

164}

165

转载于:https://www.cnblogs.com/cnherman/articles/1187977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值