A C # operation class code through the Itextsharp encapsulated PDF file

Source: Internet
Author: User
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{///// PDF document Operation class ///------------------------------------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////// constructor ///Public pdfoperation () {rect = pagesize.a4;        Document = new document (rect); }          ////// constructor //////Page size (e.g. "A4")Public pdfoperation (String type) {setpagesize (type);        Document = new document (rect); }          ////// constructor //////Page size (e.g. "A4")///Content distance from left border///Content distance from right border///Content distance from top border///Content distance from bottom borderPublic 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 Settings Font////// Set Font ///public void Setbasefont (string path) {Basefont = Basefont.createfont (path, Basefont.identity_h, Basefon        t.not_embedded); }          ////// Set Font //////Font sizepublic void SetFont (float size) {font = new font (basefont, size); #endregion #region Set the page size///// Set Page size /////Page size (e.g. "A4")public void SetPageSize (string type) {switch (type).                    Trim ()) {case "A4": rect = pagesize.a4;                Break                    Case "A8": rect = pagesize.a8;            Break }} #endregion #region instantiate the document////// Instantiation of documents //////Document related information (such as path, open mode, etc.)public void getinstance (Stream os) {pdfwriter.getinstance (document, OS); #endregion Open the Document object #region///// Open Document Object //////Document related information (such as path, open mode, etc.)public void Open (Stream os) {getinstance (OS); Document.        Open (); #endregion #region Close the open Document///// Close the Open document ///public void Close () {document.        Close (); } #endregion #region Add a paragraph///// Add a paragraph /////Content///Font sizepublic void Addparagraph (string content, float fontsize) {SetFont (fontsize);            Paragraph pra = new Paragraph (content, font); Document.        ADD (PRA); }          ////// Add a paragraph /////Content///Font size///Alignment (1 is centered, 0 is left and 2 is right)///Number of empty rows after segment (0 is the default value)///Number of empty rows before segment (0 is the default value)///Line spacing (0 is the default value)public void Addparagraph (string content, float fontsize, int Alignment, float spacingafter, float spacingbefore, float Mul            tipliedleading) {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////// Add picture //////Picture path///Alignment (1 is centered, 0 is left and 2 is right)///Picture width (0 is the default value, if the width is greater than the page width will be scaled by ratio)///Picture Highpublic void AddImage (string path, int Alignment, float newwidth, float newheight) {Image img = image.ge            Tinstance (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, dots////// Add link /////Link text///Font size///Link Addresspublic void Addanchorreference (string Content, float FontSize, string Reference) {SetFont (FontSize);            Anchor AUC = new Anchor (Content, font); Auc.            Reference = Reference; Document.        ADD (AUC); }          ////// Add Link Contact //////Link text///Font size///Link namepublic void Addanchorname (string Content, float FontSize, string Name) {SetFont (FontSize);            Anchor AUC = new Anchor (Content, font); Auc.            name = name; Document.        ADD (AUC); } #endregion}}
  • 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.