惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
V2EX - 技术
V2EX - 技术
Webroot Blog
Webroot Blog
Google Online Security Blog
Google Online Security Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
AI
AI
N
News and Events Feed by Topic
S
Secure Thoughts
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Cloudflare Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tailwind CSS Blog
Vercel News
Vercel News
V
Vulnerabilities – Threatpost
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
I
Intezer
Schneier on Security
Schneier on Security
Martin Fowler
Martin Fowler
J
Java Code Geeks
K
Kaspersky official blog
H
Heimdal Security Blog
O
OpenAI News
I
InfoQ
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
云风的 BLOG
云风的 BLOG
Hacker News - Newest:
Hacker News - Newest: "LLM"
Blog — PlanetScale
Blog — PlanetScale
S
Schneier on Security
S
Security @ Cisco Blogs
Security Latest
Security Latest
PCI Perspectives
PCI Perspectives
H
Hacker News: Front Page
C
CERT Recently Published Vulnerability Notes
博客园_首页
The Last Watchdog
The Last Watchdog
罗磊的独立博客
L
LINUX DO - 热门话题
U
Unit 42
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
Scott Helme
Scott Helme

博客园 - 小肠与小豆子

博客园MetaWeblog使用帮助 [ASP.NET 1.1]Global.asax.cs中的方法的含义 Microsoft .NET Pet Shop 4 架构与技术分析 转载:键盘模拟鼠标(实现用键盘操作鼠标光标)(示例代码下载) 转载:Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载) 转载:Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载) 转载:ASP.NET 2.0 HttpHandler实现对某种文件类型权限保护 转载:asp.net千奇百怪的日历 转载:ASP.net 2.0资料吐血收藏 解决:此页正在访问其它域的数据…… sql server系统表详细说明 数据库设计方法、规范与技巧 (转自Blogcn的我的窝) 14个数据库的设计技巧 (来自Blogcn中我的窝) SQL 数据库中所有的用户表,一个表的中的所有列信息 的SQL 转载:汉字转拼音_gb2312 C# C#中的一些常用属性 IIS状态一览表 IIS简介 IIS中的 MIME类型
转载:Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
小肠与小豆子 · 2007-05-24 · via 博客园 - 小肠与小豆子

原文:http://blog.csdn.net/ChengKing/archive/2006/12/21/1452134.aspx

(一). 功能

   创建PDF文件

(二). 代码

  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10using System.IO;
 11using System.Text;
 12using System.Collections;
 13
 14public partial class _Default : System.Web.UI.Page 
 15{
 16    static float pageWidth = 594.0f;
 17    static float pageDepth = 828.0f;
 18    static float pageMargin = 30.0f;
 19    static float fontSize = 10.0f;
 20    static float leadSize = 10.0f;
 21
 22    //指定要生成PDF文件的路径    
 23    static StreamWriter pPDF = new StreamWriter("E:\\net\\WebSites\\Create_PDF_File\\test.pdf");//指定生成的路径
 24    
 25    static MemoryStream mPDF = new MemoryStream();
 26
 27    //Convert the Text Data to PDF format and write back to
 28    //Memory Stream
 29    static void ConvertToByteAndAddtoStream(string strMsg)
 30    {
 31        Byte[] buffer = null;
 32        buffer = ASCIIEncoding.ASCII.GetBytes(strMsg);
 33        mPDF.Write(buffer, 0, buffer.Length);
 34        buffer = null;
 35    }

 36
 37    //Format the data length in xRef Format
 38    static string xRefFormatting(long xValue)
 39    {
 40        string strMsg = xValue.ToString();
 41        int iLen = strMsg.Length;
 42        if (iLen < 10)
 43        {
 44            StringBuilder s = new StringBuilder();            
 45            int i = 10 - iLen;
 46            s.Append('0', i);
 47            strMsg = s.ToString() + strMsg;
 48        }

 49        return strMsg;
 50    }

 51    protected void Page_Load(object sender, EventArgs e)
 52    {
 53
 54    }

 55    protected void Button1_Click(object sender, EventArgs e)
 56    {
 57        //Create a ArrayList for xRefs of PDF Document
 58        ArrayList xRefs = new ArrayList();        
 59        float yPos = 0f;
 60        long streamStart = 0;
 61        long streamEnd = 0;
 62        long streamLen = 0;
 63        string strPDFMessage = null;
 64        //PDF Header Message
 65        strPDFMessage = "%PDF-1.1\n";
 66        ConvertToByteAndAddtoStream(strPDFMessage);
 67
 68        //ID 1 For Containt
 69        //ID 2 For Length of the Stream
 70        //write the Text
 71
 72        //1> Start a new Page
 73        xRefs.Add(mPDF.Length);
 74        strPDFMessage = "1 0 obj\n";
 75        ConvertToByteAndAddtoStream(strPDFMessage);
 76        strPDFMessage = "<< /Length 2 0 R >>\n";
 77        ConvertToByteAndAddtoStream(strPDFMessage);
 78        strPDFMessage = "stream\n";
 79        ConvertToByteAndAddtoStream(strPDFMessage);
 80
 81        //Get the start of the stream
 82        streamStart = mPDF.Length;
 83        strPDFMessage = "BT\n/F0 " + fontSize + " Tf\n";
 84        ConvertToByteAndAddtoStream(strPDFMessage);
 85        yPos = pageDepth - pageMargin;
 86        strPDFMessage = pageMargin + " " + yPos + " Td\n";
 87        ConvertToByteAndAddtoStream(strPDFMessage);
 88        strPDFMessage = leadSize + " TL\n";
 89        ConvertToByteAndAddtoStream(strPDFMessage);
 90
 91        //Add the text data to the PDF memory stream
 92        strPDFMessage = "(Do a test)Tj\n";
 93        ConvertToByteAndAddtoStream(strPDFMessage);
 94        strPDFMessage = "ET\n";
 95        ConvertToByteAndAddtoStream(strPDFMessage);
 96        //Get the End of the stream
 97        streamEnd = mPDF.Length;
 98        //Get the Length of the stream
 99        streamLen = streamEnd - streamStart;
100        strPDFMessage = "endstream\nendobj\n";
101        ConvertToByteAndAddtoStream(strPDFMessage);
102
103        //Add 2 object to xRef
104        xRefs.Add(mPDF.Length);
105        strPDFMessage = "2 0 obj\n" + streamLen + "\nendobj\n";
106        ConvertToByteAndAddtoStream(strPDFMessage);
107
108        //Add Page to xRefs
109        xRefs.Add(mPDF.Length);
110        strPDFMessage = "3 0 obj\n<</Type/Page/Parent 4 0 R/Contents 1 0 R>>\nendobj\n";
111        ConvertToByteAndAddtoStream(strPDFMessage);
112
113        //Build the Pages
114        xRefs.Add(mPDF.Length);
115        strPDFMessage = "4 0 obj\n<</Type /Pages /Count 1\n";
116        ConvertToByteAndAddtoStream(strPDFMessage);
117        strPDFMessage = "/Kids[\n3 0 R\n]\n";
118        ConvertToByteAndAddtoStream(strPDFMessage);
119        strPDFMessage = "/Resources<</ProcSet[/PDF/Text]/Font<</F0 5 0 R>> >>\n";
120        ConvertToByteAndAddtoStream(strPDFMessage);
121        strPDFMessage = "/MediaBox [ 0 0 " + pageWidth + " " + pageDepth + " ]\n>>\nendobj\n";
122        ConvertToByteAndAddtoStream(strPDFMessage);
123
124        //Add font to xRefs
125        xRefs.Add(mPDF.Length);
126        strPDFMessage = "5 0 obj\n<</Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding>>\nendobj\n";
127        ConvertToByteAndAddtoStream(strPDFMessage);
128
129        //Add the catalog to xRefs
130        xRefs.Add(mPDF.Length);
131        strPDFMessage = "6 0 obj\n<</Type/Catalog/Pages 4 0 R>>\nendobj\n";
132        ConvertToByteAndAddtoStream(strPDFMessage);
133
134        //xRefs Entry 
135        streamStart = mPDF.Length;
136        strPDFMessage = "xref\n0 7\n0000000000 65535 f \n";
137        for (int i = 0; i < xRefs.Count; i++)
138        {
139            strPDFMessage += xRefFormatting((long)xRefs[i]) + " 00000 n \n";
140        }

141        ConvertToByteAndAddtoStream(strPDFMessage);
142        //Trailer for the PDF
143        strPDFMessage = "trailer\n<<\n/Size " + (xRefs.Count + 1+ "\n/Root 6 0 R\n>>\n";
144        ConvertToByteAndAddtoStream(strPDFMessage);
145        //xRef location entry
146        strPDFMessage = "startxref\n" + streamStart + "\n%%EOF\n";
147        ConvertToByteAndAddtoStream(strPDFMessage);
148        //Write the PDF from Memory Stream to File Stream
149        mPDF.WriteTo(pPDF.BaseStream);
150        //Close the Stream
151        mPDF.Close();
152        pPDF.Close();
153    }

154}

155

(三). 示例代码下载

        Create_PDF_File.rar 

                    [本文核心代码为引用,方便查看用]

posted on 2007-05-24 14:51  小肠与小豆子  阅读(541)  评论(0)    收藏  举报

刷新页面返回顶部