This article mainly for you to introduce the C # generated PDF file stream of relevant information, with a certain reference value, interested in small partners can refer to
The example of this article for everyone to share the C # generated PDF file stream specific code for your reference, the specific content as follows
1. Set the font
static Basefont fontbase = Basefont.createfont ("C:\\windows\\fonts\\stsong. TTF ", Basefont.identity_h, basefont.embedded); static ITextSharp.text.Font Bodyfont = new ITextSharp.text.Font (fontbase, n); static ITextSharp.text.Font Titlefont = new ITextSharp.text.Font (fontbase); static ITextSharp.text.Font Paragraphfont = new ITextSharp.text.Font (fontbase, n); static ITextSharp.text.Font Linkfont = new ITextSharp.text.Font (fontbase, Font.underline, Basecolor.blue);
2. Generate a PDF file stream to return a byte array
Public byte[] Doccreate (System.Drawing.Image Image, list<treenodes> List) {MemoryStream file = new Memoryst Ream (); String fileName = String. Empty; Rectangle page = pagesize.a4; Float y = page. Height; Document document = new Document (page, 15, 15, 30, 30); float docwidth = page. WIDTH-15 * 2; float docheight = page. Height-document. Bottommargin-document. TopMargin; PDFWriter writer = pdfwriter.getinstance (document, file); Writer. Closestream = false; Writer. Open (); Pdfcontentbyte cb = writer. Directcontent; Document. Open (); Title Paragraph title = new Paragraph (New Chunk ("title", Titlefont)); Title. Alignment = Element.align_center; Document. ADD (title); Picture iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance (image, Imageformat.png); float Widthszie = (page. WIDTH-30)/img. Width; if (Widthszie < 1) {img. Scalepercent (Widthszie * 100); } document.Add (IMG); Document provenance Paragraph P2 = new Paragraph (New Chunk ("provenance", Paragraphfont)); P2. Indentationleft = Indentationleft; Document. ADD (p2); InitData (list);//Initialize the business data createsteps (list, document, list. FirstOrDefault (IT and it). PID = = 0);//Add Business Data////add stamp//itextsharp.text.image whyz = iTextSharp.text.Image.GetInstance (Whyzpath); Whyz. Scalepercent (50); Whyz. paddingtop = 100; Whyz. Alignment = Element.align_right; Document. ADD (Whyz); Add date Paragraph createtime = new Paragraph (New Chunk (DateTime.Now.ToLongDateString (). ToString (), Bodyfont)); Createtime. Alignment = Element.align_right; Createtime. Spacingbefore =-80; Createtime. Paddingtop = 200; Document. ADD (Createtime); Document. Close (); File. Position = 0; MemoryStream newfile = Setwatermark (file, "watermark content", docwidth, docheight);//Add watermark, see another blog newfile. Position = 0;//Reset stream pointer position byte[] bytes = new Byte[newfile. Length]; NewfiLe. Read (bytes, 0, bytes. Length); return bytes; }