• C#生成PDF文档,读取TXT文件内容


    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:windowsfontsSIMKAI.TTF", BaseFont.IDENTITY_H, false); //调用的字体
                    Font font = new Font(bfSun, 15);

                    String text = "                            图片授权协议 ";
                    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 = "http://images.com/" + strCatalogId.ToLower() + "/thu/" + _pic_id.ToLower() + ".jpg";// "http://images.com/west004/thu/babf00254.jpg";//图片所在路径
                    iTextSharp.text.Image jpeg = iTextSharp.text.Image.getInstance(new Uri(FileNameUrl));
                    document.Add(new Paragraph(" 授权图片:", font));
                    document.Add(jpeg);

                    //插入图片编号
                    document.Add(new Paragraph(" 图片编号:"+ _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(" 授权码:"+ 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

  • 相关阅读:
    高亮表格行或列
    点击表格获取表格行或列索引
    文本框仅可接收decimal
    实现表格自动计算
    实现一个超简单的开关
    Nginx反向代理中proxy_set_header参数说明
    两个目录中,删除其中一个目录中同名文件的做法
    监控某个目录是否被更改
    centos下升级git版本的操作记录
    linux下core file size设置笔记
  • 原文地址:https://www.cnblogs.com/weihengblogs/p/3875311.html
Copyright © 2020-2023  润新知