欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#编程简单实现生成PDF文档的方法示例

程序员文章站 2024-02-09 21:39:52
本文实例讲述了c#编程简单实现生成pdf文档的方法。分享给大家供大家参考,具体如下: using system; using system.io; using...

本文实例讲述了c#编程简单实现生成pdf文档的方法。分享给大家供大家参考,具体如下:

using system;
using system.io;
using system.text;
using system.collections;
namespace pdfgenerator
{
  public class pdfgenerator
  {
    static float pagewidth = 594.0f;
    static float pagedepth = 828.0f;
    static float pagemargin = 30.0f;
    static float fontsize = 20.0f;
    static float leadsize = 10.0f;
    static streamwriter ppdf = new streamwriter("e:\\mypdf.pdf");
    static memorystream mpdf = new memorystream();
    static void converttobyteandaddtostream(string strmsg)
    {
      byte[] buffer = null;
      buffer = asciiencoding.ascii.getbytes(strmsg);
      mpdf.write(buffer, 0, buffer.length);
      buffer = null;
    }
    static string xrefformatting(long xvalue)
    {
      string strmsg = xvalue.tostring();
      int ilen = strmsg.length;
      if (ilen < 10)
      {
        stringbuilder s = new stringbuilder();
        int i = 10 - ilen;
        s.append('0', i);
        strmsg = s.tostring() + strmsg;
      }
      return strmsg;
    }
    static void main(string[] args)
    {
      arraylist xrefs = new arraylist();
      //byte[] buffer=null;
      float ypos = 0f;
      long streamstart = 0;
      long streamend = 0;
      long streamlen = 0;
      string strpdfmessage = null;
      //pdf文档头信息
      strpdfmessage = "%pdf-1.1\n";
      converttobyteandaddtostream(strpdfmessage);
      xrefs.add(mpdf.length);
      strpdfmessage = "1 0 obj\n";
      converttobyteandaddtostream(strpdfmessage);
      strpdfmessage = "<< /length 2 0 r >>\n";
      converttobyteandaddtostream(strpdfmessage);
      strpdfmessage = "stream\n";
      converttobyteandaddtostream(strpdfmessage);
      ////////pdf文档描述
      streamstart = mpdf.length;
      //字体
      strpdfmessage = "bt\n/f0 " + fontsize + " tf\n";
      converttobyteandaddtostream(strpdfmessage);
      //pdf文档实体高度
      ypos = pagedepth - pagemargin;
      strpdfmessage = pagemargin + " " + ypos + " td\n";
      converttobyteandaddtostream(strpdfmessage);
      strpdfmessage = leadsize + " tl\n";
      converttobyteandaddtostream(strpdfmessage);
      //实体内容
      strpdfmessage = "(http://www.wenhui.org)tj\n";
      converttobyteandaddtostream(strpdfmessage);
      strpdfmessage = "et\n";
      converttobyteandaddtostream(strpdfmessage);
      streamend = mpdf.length;
      streamlen = streamend - streamstart;
      strpdfmessage = "endstream\nendobj\n";
      converttobyteandaddtostream(strpdfmessage);
      //pdf文档的版本信息
      xrefs.add(mpdf.length);
      strpdfmessage = "2 0 obj\n" + streamlen + "\nendobj\n";
      converttobyteandaddtostream(strpdfmessage);
      xrefs.add(mpdf.length);
      strpdfmessage = "3 0 obj\n<</type/page/parent 4 0 r/contents 1 0 r>>\nendobj\n";
      converttobyteandaddtostream(strpdfmessage);
      xrefs.add(mpdf.length);
      strpdfmessage = "4 0 obj\n<</type /pages /count 1\n";
      converttobyteandaddtostream(strpdfmessage);
      strpdfmessage = "/kids[\n3 0 r\n]\n";
      converttobyteandaddtostream(strpdfmessage);
      strpdfmessage = "/resources<</procset[/pdf/text]/font<</f0 5 0 r>> >>\n";
      converttobyteandaddtostream(strpdfmessage);
      strpdfmessage = "/mediabox [ 0 0 " + pagewidth + " " + pagedepth + " ]\n>>\nendobj\n";
      converttobyteandaddtostream(strpdfmessage);
      xrefs.add(mpdf.length);
      strpdfmessage = "5 0 obj\n<</type/font/subtype/type1/basefont/courier/encoding/winansiencoding>>\nendobj\n";
      converttobyteandaddtostream(strpdfmessage);
      xrefs.add(mpdf.length);
      strpdfmessage = "6 0 obj\n<</type/catalog/pages 4 0 r>>\nendobj\n";
      converttobyteandaddtostream(strpdfmessage);
      streamstart = mpdf.length;
      strpdfmessage = "xref\n0 7\n0000000000 65535 f \n";
      for (int i = 0; i < xrefs.count; i++)
      {
        strpdfmessage += xrefformatting((long)xrefs[i]) + " 00000 n \n";
      }
      converttobyteandaddtostream(strpdfmessage);
      strpdfmessage = "trailer\n<<\n/size " + (xrefs.count + 1) + "\n/root 6 0 r\n>>\n";
      converttobyteandaddtostream(strpdfmessage);
      strpdfmessage = "startxref\n" + streamstart + "\n%%eof\n";
      converttobyteandaddtostream(strpdfmessage);
      mpdf.writeto(ppdf.basestream);
      mpdf.close();
      ppdf.close();
    }
  }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#文件操作常用技巧汇总》、《c#遍历算法与技巧总结》、《c#程序设计之线程使用技巧总结》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。