c#產生PDF檔案源碼

來源:互聯網
上載者:User
 
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<>/nendobj/n";      ConvertToByteAndAddtoStream(strPDFMessage);      xRefs.Add(mPDF.Length);      strPDFMessage="4 0 obj/n<>/n";      ConvertToByteAndAddtoStream(strPDFMessage);      strPDFMessage="startxref/n" + streamStart+"/n%%EOF/n";      ConvertToByteAndAddtoStream(strPDFMessage);      mPDF.WriteTo(pPDF.BaseStream);      mPDF.Close();      pPDF.Close(); } } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.