C # using the Itextsharp encapsulated PDF file Operation class Instance

Source: Internet
Author: User

This example describes the PDF file operation class that C # uses in Itextsharp encapsulation. Share to everyone for your reference. The specific analysis is as follows:

This C # code mainly itextsharp in the operation of the PDF file method has been re-encapsulated, more convenient to access the PDF document, you can dynamically generate PDF files, add content, set paragraphs, set fonts and so on.

Using system.io;using itextsharp.text;using itextsharp.text.pdf;namespace dotnet.utilities{//<summary>// PDF document Operation class///</summary>//------------------call--------------------------//pdfoperation PDF = new Pdfoperation ()  ; Pdf.  Open (New FileStream (Path, filemode.create)); Pdf. Setbasefont (@ "C:\Windows\Fonts\SIMHEI.  TTF "); Pdf.  Addparagraph ("Test document (Build time:" + DateTime.Now + ")", 15, 1, 20, 0, 0); Pdf.  Close (); -------------------------------public class Pdfoperation {#region constructor///<summary>///constructor///      </summary> public pdfoperation () {rect = pagesize.a4;    Document = new document (rect);    }///<summary>///constructor///</summary>//<param name= "type" > page size (e.g. "A4") </param>      Public pdfoperation (String type) {setpagesize (type);    Document = new document (rect); }///<summary>///constructor///</summary>//<param name= "type" > page size (e.g. "A4") </param>//<param name= "MarginLeft" > content from the Left border </param>//<param name= "marginright" > Content distance from right border < /param>//<param name= "margintop" > Content from top Border </param>//<param name= "MarginBottom" > content from bottom border distance &lt    ;/param> public pdfoperation (string type, float marginleft, float marginright, float margintop, float marginbottom)      {setpagesize (type);    Document = new Document (Rect, MarginLeft, MarginRight, MarginTop, marginbottom);    The private font font is #endregion #region proprietary field;  Private Rectangle rect; Document Size Private Document document;//documents object private Basefont basefont;//font #endregion #region Set font///<summ Ary>///Set font///</summary> public void Setbasefont (string path) {Basefont = Basefont.createfo    NT (path, Basefont.identity_h, basefont.not_embedded); }///<summary>///Set fonts///</summary>/<param name= "size" > Font size </param> public void SetfonT (float size) {font = new font (basefont, size); #endregion #region Set Page size//<summary>//Set Page Size///</summary>//<param name= "type "> page size (such as" A4 ") </param> public void SetPageSize (string type) {switch (type).          Trim ()) {case "A4": rect = pagesize.a4;        Break          Case "A8": rect = pagesize.a8;      Break }} #endregion #region instantiated document///<summary>///Instantiate document///</summary>//<param name= "O     S "> Document related information (such as path, open mode, etc.) </param> public void getinstance (Stream os) {pdfwriter.getinstance (document, OS); #endregion #region Open Document Object////<summary>///Open Document Object///</summary>//<param name= "      Os > documentation related information (such as path, open mode, etc.) </param> public void Open (Stream os) {getinstance (OS); Document.    Open (); #endregion #region Close the Open Document///<summary>///Close open Document///</summary> public void Close () {document.    Close (); #endregion #region Add paragraph///<summary>///Add paragraph///</summary>//<param name= "content" > Content </param>//<param name= "fontsize" > Font size </param> public void addparagraph (string content, FL      Oat fontsize) {SetFont (fontsize);      Paragraph pra = new Paragraph (content, font); Document.    ADD (PRA); }///<summary>//Add paragraphs///</summary>//<param name= "Content" > Contents </param>///&L    T;param name= "fontsize" > Font size </param>//<param name= "Alignment" > Alignment (1 is centered, 0 is left and 2 is right) </param> <param name= "Spacingafter" > Segment number of empty lines (0 default) </param>//<param name= "Spacingbefore" > Segment number of empty rows (0 is the default value) </param>//<param name= "multipliedleading" > Line spacing (0 is the default) </param> public void Addparagraph (String co Ntent, float fontsize, int Alignment, float spacingafter, float spacingbefore, float MULTIPLiedleading) {SetFont (fontsize);      Paragraph pra = new Paragraph (content, font); Pra.      Alignment = Alignment; if (Spacingafter! = 0) {pra.      Spacingafter = Spacingafter; } if (Spacingbefore! = 0) {pra.      Spacingbefore = Spacingbefore; } if (multipliedleading! = 0) {pra.      multipliedleading = multipliedleading; } document.    ADD (PRA); #endregion #region Add Pictures///<summary>//Add Pictures///</summary>//<param name= "path" Picture path </param>///<param Name= "Alignment" > Alignment (1 is center, 0 is left, 2 is right) </param>//<param name= "Newwidt H "> Picture width (0 is default, if width is greater than page width will be scaled by ratio) </param>//<param name=" newheight "> Picture high </param> public void Addima      GE (string path, int Alignment, float newwidth, float newheight) {Image img = image.getinstance (path); Img.      Alignment = Alignment; if (newwidth! = 0) {img. Scaleabsolute (Newwidth, Newheight); } else {if (img. Width > PageSize.A4.Width) {img. Scaleabsolute (rect. Width, IMG. Width * img. Height/rect.        Height); }} document.    Add (IMG); #endregion #region Add links, points///<summary>///Add links//</summary>//<param name= "Conten T "> Link text </param>//<param name=" FontSize "> Font size </param>///<param Name=" Reference "> Link address & lt;/param> public void Addanchorreference (string Content, float FontSize, string Reference) {SetFont (Fontsi      Ze);      Anchor AUC = new Anchor (Content, font); Auc.      Reference = Reference; Document.    ADD (AUC);  }///<summary>//Add link Contact///</summary>//<param name= "Content" > Link text </param>// <param name= "FontSize" > Font size </param>//<param name= "name" > link name </param> public void Addanch ORName (string Content, float FontSize, string Name) {SetFont (FontSize);      Anchor AUC = new Anchor (Content, font); Auc.      name = name; Document.    ADD (AUC); } #endregion}}

I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # using the Itextsharp encapsulated PDF file Operation class Instance

This address: http://www.paobuke.com/develop/c-develop/pbk23059.html






Related Content C # Simple implementation of the method of displaying the Chinese format day of the week how to implement the stack by using the object Class C # Generate barcode Operation knowledge Summary attached source code Download C # Print class PrintDocument, PrintDialog, PrintPreviewDialog Use Example
WinForm Windows 2 ways to implement multi-display display C # nullable type usage Analysis hosting process (Vshost.exe) in C # IDE VS2005 Introduction to C # Methods for remote connection to an Oracle database

C # using the Itextsharp encapsulated PDF file Operation class Instance

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.