欢迎光临
我们一直在努力

建站超值云服务器,限时71元/月

摘自《程序员大本营2001.net版》

using system;
using system.io;
using system.text;
using system.collections;

namespace pdfgenerator
{
    /// <summary>
    /// application : generation of pdf file from text
    /// author        : pramod kumar singh
    /// date        : 25th july 2001
    ///</summary>
        
    public class pdfgenerator
    {
        static float pagewidth = 594.0f;
        static float pagedepth = 828.0f;
        static float pagemargin = 30.0f;
        static float fontsize = 10.0f;
        static float leadsize = 10.0f;
        
        //create a pdf file.
        //pdf on disk
        static streamwriter ppdf=new streamwriter("f:\\temp\\mypdf.pdf");
        //pdf in memory
        static memorystream mpdf= new memorystream();
        
        //convert the text data to pdf format and write back to
        //memory stream
        static void converttobyteandaddtostream(string strmsg)
        {
            byte[] buffer=null;
            buffer=asciiencoding.ascii.getbytes(strmsg);
            mpdf.write(buffer,0,buffer.length);  
            buffer=null;
        }
        
        //format the data length in xref format
        static string xrefformatting(long xvalue)
        {
            string strmsg =xvalue.tostring();
            int ilen=strmsg.length;
            if (ilen<10)
            {
                stringbuilder s=new stringbuilder();
                //string s=null;
                int i=10-ilen;
                s.append(0,i);
                strmsg=s.tostring() + strmsg;
            }
            return strmsg;
        }

        //entry point
        static void main(string[] args)
        {
            //create a arraylist for xrefs of pdf document
            arraylist xrefs=new arraylist();
            byte[] buffer=null;
            float ypos =0f;
            long streamstart=0;
            long streamend=0;
            long streamlen =0;
            string strpdfmessage=null;
            //pdf header message
            strpdfmessage="%pdf-1.1\n";
            converttobyteandaddtostream(strpdfmessage);
            
            //id 1 for containt
            //id 2 for length of the stream
            //write the text
            
            //1> start a new page
            xrefs.add(mpdf.length);
            strpdfmessage="1 0 obj\n";
            converttobyteandaddtostream(strpdfmessage);
            strpdfmessage="<< /length 2 0 r >>\n";
            converttobyteandaddtostream(strpdfmessage);
            strpdfmessage="stream\n";
            converttobyteandaddtostream(strpdfmessage);
            
            //get the start of the stream
            streamstart=mpdf.length;
            strpdfmessage="bt\n/f0 " + fontsize +" tf\n";
            converttobyteandaddtostream(strpdfmessage);
            ypos = pagedepth – pagemargin;
            strpdfmessage=pagemargin + " " + ypos +" td\n" ;
            converttobyteandaddtostream(strpdfmessage);
            strpdfmessage= leadsize+" tl\n" ;
            converttobyteandaddtostream(strpdfmessage);
            
            //add the text data to the pdf memory stream
            strpdfmessage= "(pramod kumar singh)tj\n" ;
            converttobyteandaddtostream(strpdfmessage);
            strpdfmessage= "et\n";
            converttobyteandaddtostream(strpdfmessage);
            //get the end of the stream
            streamend=mpdf.length;
            //get the length of the stream
            streamlen=streamend-streamstart;
            strpdfmessage= "endstream\nendobj\n";
            converttobyteandaddtostream(strpdfmessage);
                    
            //add 2 object to xref
            xrefs.add(mpdf.length);
            strpdfmessage="2 0 obj\n"+ streamlen + "\nendobj\n";
            converttobyteandaddtostream(strpdfmessage);
                        
            //add page to xrefs
            xrefs.add(mpdf.length);
            strpdfmessage="3 0 obj\n<</type/page/parent 4 0 r/contents 1 0 r>>\nendobj\n";
            converttobyteandaddtostream(strpdfmessage);
            
            //build the pages
            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);
            
            //add font to xrefs
            xrefs.add(mpdf.length);
            strpdfmessage="5 0 obj\n<</type/font/subtype/type1/basefont/courier/encoding/winansiencoding>>\nendobj\n";
            converttobyteandaddtostream(strpdfmessage);
                    
            //add the catalog to xrefs
            xrefs.add(mpdf.length);
            strpdfmessage="6 0 obj\n<</type/catalog/pages 4 0 r>>\nendobj\n";
            converttobyteandaddtostream(strpdfmessage);
                        
            //xrefs entry
            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);
            //trailer for the pdf
            strpdfmessage="trailer\n<<\n/size "+ (xrefs.count+1)+"\n/root 6 0 r\n>>\n";
            converttobyteandaddtostream(strpdfmessage);
            //xref location entry
            strpdfmessage="startxref\n" + streamstart+"\n%%eof\n";
            converttobyteandaddtostream(strpdfmessage);
            //write the pdf from memory stream to file stream
            mpdf.writeto(ppdf.basestream);
            //close the stream
            mpdf.close();
            ppdf.close();
        }
    }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 »
分享到: 更多 (0)

相关推荐

  • 暂无文章