Code case sharing for generating PDF file streams using C #

Source: Internet
Author: User
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; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.