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 = "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("\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

C#生成PDF文档,读取TXT文件内容的更多相关文章

  1. 使用PHP生成PDF文档

    原文:使用PHP生成PDF文档 实际工作中,我们要使用PHP动态的创建PDF文档,目前有许多开源的PHP创建PDF的类库,今天我给大家来介绍一款优秀的PDF库,它就是TCPDF,TCPDF是一个用于快 ...

  2. Apache PDFbox开发指南之PDF文档读取

    转载请注明来源:http://blog.csdn.net/loongshawn/article/details/51542309 相关文章: <Apache PDFbox开发指南之PDF文本内容 ...

  3. 手把手教你使用 Java 在线生成 pdf 文档

    一.介绍 在实际的业务开发的时候,研发人员往往会碰到很多这样的一些场景,需要提供相关的电子凭证信息给用户,例如网银/支付宝/微信购物支付的电子发票.订单的库存打印单.各种电子签署合同等等,以方便用户查 ...

  4. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  5. Aspose.Words操作word生成PDF文档

    Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...

  6. 如何从Windows Phone 生成PDF文档

    我需要从我的Windows Phone应用程序生成PDF. 遗憾的是没有标准的免费的PDF生成库在Windows Phone上运行. 我不得不自己生成PDF,通过直接写入到文件格式. 这竟然是真的很容 ...

  7. DocFX生成PDF文档

    使用DocFX生成PDF文档,将在线文档转换为PDF离线文档. 关于DocFX的简单介绍使用DocFX生成文档 使用docfx 命令 1.下载 https://github.com/dotnet/do ...

  8. qt 利用 HTML 生成PDF文档,不能显示jpg图片

    利用 QPrinter 和html 生成 pdf文档 其中用html语句有显示图片的语句 但只能显示png格式的图片,不能显示jpg格式图片. 经过排查:语法,文件路径等都正确,最终在stack ov ...

  9. 自动把动态的jsp页面(或静态html)生成PDF文档,并且上传至服务器

    置顶2017年11月06日 14:41:04 阅读数:2311 这几天,任务中有一个难点是把一个打印页面自动给生成PDF文档,并且上传至服务器,然而公司框架只有手动上传文档,打印时可以保存为PDF在本 ...

  10. Spring Boot集成JasperReports生成PDF文档

    由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲染生成PDF文档.本人文采欠缺,写作能力 ...

随机推荐

  1. flume学习

    下载 自定义sink(mysql) 1.ide打开下载后的源码 2.代码如下: /** * Licensed to the Apache Software Foundation (ASF) under ...

  2. 【转】深入Windows内核——C++中的消息机制

    上节讲了消息的相关概念,本文将进一步聊聊C++中的消息机制. 从简单例子探析核心原理 在讲之前,我们先看一个简单例子:创建一个窗口和两个按钮,用来控制窗口的背景颜色.其效果 图1.效果图  Win32 ...

  3. Mac 制作 10.11.3 U盘安装盘

    U盘要且只分一个区  Mac OS 拓展(日志式)  GUID分区表: 将“安装 OS X El Capitan” 放到  应用程序文件夹下 命令: sudo /Applications/Instal ...

  4. NYOJ之素数求和问题

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsoAAAKCCAIAAABH/2gWAAAgAElEQVR4nO3dPVLjStsG4G8T5CyEdF

  5. js 闭包原理理解

    问题?什么是js(JavaScript)的闭包原理,有什么作用? 一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 很显然 ...

  6. Android Programming: Pushing the Limits -- Chapter 4: Android User Experience and Interface Design

    User Stories Android UI Design 附加资源 User Stories: @.通过写故事来设计应用. @.每个故事只关注一件事. @.不同的故事可能使用相同的组件,因此尽早地 ...

  7. MYSQL中'TYPE=MyISAM'错误的解决方案

    create 语句后面的TYPE=MyISAM TYPE=MyISAM 和 ENGINE=MyISAM 都是设置数据库存储引擎的语句 ,(老版本的MySQL使用TYPE而不是ENGINE(例如,TYP ...

  8. 【翻译十一】java-原子性操作

    Atomic Access In programming, an atomic action is one that effectively happens all at once. An atomi ...

  9. GENERATED_UCLASS_BODY 和 GENERATED_BODY 区别

    the GENERATED_BODY() macro allows the class to build without having a constructor defined. If you ne ...

  10. 使用PHPExcel导入Excel到MySql

    .连接数据库的connection.php文件 <?php //修改下面代码来联接数据库 // mysql_connect打开一个到 MySQL 服务器的连接,如果成功则返回一个 MySQL 连 ...