博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#生成PDF文档,读取TXT文件内容
阅读量:5062 次
发布时间:2019-06-12

本文共 4436 字,大约阅读时间需要 14 分钟。

using System.IO;

using iTextSharp.text;
using iTextSharp.text.pdf;
//需要在项目里引用ICSharpCode.SharpZipLib.dll和itextsharp.dll
public string TxtFilePath;
public string SavePdfPath;//保存PDF的路径
  #region 读取TXT内容
        private string ReadXieyi(string FilePath)
        {
            string xieyi = "";
            FileInfo fi = new FileInfo(FilePath);
            StreamReader sr = fi.OpenText();
            xieyi = sr.ReadToEnd();
            sr.Close();
            return xieyi;
        }
        #endregion
        #region 创建PDF文档方法
        private void zscsc()
        {
            Document document = new Document(PageSize.A4);
            try
            {
                //<appSettings>
                // <add key="TxtFilePath" value="config/pubcode/"/>
                //<!-- 在WEBCOFIG里面加上的节点( value值是WebUI下的config/pubcode/)包文件保存在指定的文件下 -->
                //<appSettings>
                string name = Online.WebUI.AccountController.LoginName + "_" + _pic_id + ".pdf";
                SavePdfPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationSettings.AppSettings["SavePdfPath"] + name;
               // object filename = "C://" + name; //filename,文件保存路径
           PdfWriter writer = PdfWriter.getInstance(document, new FileStream(SavePdfPath.ToString(), FileMode.Create));
                document.addTitle("图片授权协议");
                document.addAuthor("Quanjing.COM");
                document.addHeader("Expires", "0");
                document.Open();
                BaseFont bfSun = BaseFont.createFont(@"c:\windows\fonts\SIMKAI.TTF", BaseFont.IDENTITY_H, false); //调用的字体
                Font font = new Font(bfSun, 15);
                String text = "                            图片授权协议\n\n";
                document.Add(new Paragraph(text, font));
                //<appSettings>
                // <add key="TxtFilePath" value="config/pubcode/xieyi.txt"/>
                //<!-- 在WEBCOFIG里面加上的节点( value值是WebUI下的config/pubcode/xieyi.txt)这样的好处是:如果TXT的名称有改动,直接更改webconfig就行了 -->
                //<appSettings>
                TxtFilePath = AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationSettings.AppSettings["TxtFilePath"];
                font = new Font(bfSun, 10);
                text = ReadXieyi(TxtFilePath); //(@"C:\xieyi.txt");读取TXT文件的路径" 图片授权TXT文件协议内容";
                document.Add(new Paragraph(text, font));
                //插入图片
           string strCatalogId;
                DataTable dt = Online.Business.Production.bizDownPro.GetCatalogID(_pic_id);
                strCatalogId = dt.Rows[0][0].ToString();
                string FileNameUrl = "" + strCatalogId.ToLower() + "/thu/" + _pic_id.ToLower() + ".jpg";// "west004/thu/babf00254.jpg";//图片所在路径
                iTextSharp.text.Image jpeg = iTextSharp.text.Image.getInstance(new Uri(FileNameUrl));
                document.Add(new Paragraph("\n\n授权图片:", font));
                document.Add(jpeg);
                //插入图片编号
                document.Add(new Paragraph("\n图片编号:"+ _pic_id, font));
                string ActivateCode;
                ActivateCode = _pic_id + Online.WebUI.AccountController.LoginName; //用图片编号加上登陆名生成MD5编码,只取前25位
                string Md5Code;
                Md5Code = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(ActivateCode, "MD5").ToLower().Substring(0, 20); //加密MD5,只取前20位//"QJ-QQQQW-EEEER-TTTTY-UUUUN";
                string code;
                code = Md5Code.Substring(0, 5);//每5位之间用"-"分开
                code = code + "-" + Md5Code.Substring(5, 5);
                code = code + "-" + Md5Code.Substring(10, 5);
                code = code + "-" + Md5Code.Substring(15, 5);
                code = "QJ-" + code.ToString().ToUpper();
                document.Add(new Paragraph("\n授权码:"+ code, font));//code是授权码
            }
            catch (DocumentException de)
            {
                Response.Write(de.Message);
            }
            catch (IOException ioe)
            {
                Response.Write(ioe.Message);
            }
            document.Close();
        }
#endregion
//按钮事件
        protected void Button2_Click(object sender, EventArgs e)
        {
            string name = Online.WebUI.AccountController.LoginName + "_" + _pic_id + ".pdf";
            SavePdfPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationSettings.AppSettings["SavePdfPath"] + name;
            if (!File.Exists(SavePdfPath.ToString()))
            {
                zscsc();//创建PDF文档
                ReadDownData();//下载TXT文件
            }
            else
            {
                ReadDownData();//下载PDF文件
            }
        }
        //读取TXT文件并下载 PDF文件
        public void ReadDownData()
        {
            string name = Online.WebUI.AccountController.LoginName + "_" + _pic_id + ".pdf";
            SavePdfPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationSettings.AppSettings["SavePdfPath"] + name;
            //读取文件,并写入到客户端响应
            FileStream fs = new FileStream(SavePdfPath.ToString(), FileMode.Open, FileAccess.Read);
            byte[] b = new Byte[fs.Length];
            fs.Read(b, 0, b.Length);
            fs.Flush();
            fs.Close();
            //File.Delete(filename.ToString()); 下载之后不执行删除
        Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = false;
            Response.ContentType = "application/octet-stream";//ContentType;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
            Response.AppendHeader("Content-Length", b.Length.ToString());
            Response.OutputStream.Write(b, 0, b.Length);
            Response.Flush();
            Response.End();
        }

转载 http://www.idai.com.cn/blog/article.asp?id=489

转载于:https://www.cnblogs.com/weihengblogs/p/3875311.html

你可能感兴趣的文章
数据库Sharding的基本思想和切分策略
查看>>
javascript:event对象
查看>>
react-native 踩坑记
查看>>
VC为控件添加背景
查看>>
Wojilu学习笔记 (01)
查看>>
web.xml上下文初始化参数
查看>>
nginx,docker反向代理
查看>>
同事间一些搞笑的事情
查看>>
Python 基础篇(2)
查看>>
NOIP临考经验【转】
查看>>
ZOJ 3235 Prototype
查看>>
Best Practices in Asynchronous Programming
查看>>
数组的使用方法
查看>>
64位操作系统下IIS报“试图加载格式不正确的程序”错误
查看>>
存储过程里构建模糊查询,引号的处理
查看>>
黑莓游戏引擎
查看>>
Dijkstra算法和其邻接矩阵实现
查看>>
IE6 中 window.location.href 不能跳转
查看>>
v形 加强版
查看>>
EMVTag系列12《卡片内部风险管理数据》
查看>>