[reprint] C ා tool class: use iTextSharp to operate PDF document

Keywords: C# Windows

iTextSharp is a component DLL program used to operate PDF files. In C ා program, iTextSharp component can be referenced to develop reports related to PDF files and other functions. Using the method interface provided by iTextSharp component, we can realize many operations related to PDF documents, such as opening PDF document objects, adding paragraphs to PDF documents, adding picture links, etc, It's very powerful. This part simply encapsulates the iTextSharp class and provides some common PDF operation methods.

iTextSharp official website: http://www.itextpdf.com/ (for good English suggestions, please check the original documents directly.).

In the process of developing a project in Visual Studio, you can manually import two DLL files of iTextSharp into your project, which can be used in the project after successful introduction. If your Visual Studio has NuGet tool installed, you can install it automatically through NuGet tool, as shown in the following figure:

The PDF operation help class encapsulated by iTextSharp component is as follows, which only contains some simple operations. If the reader has more complex requirements, please refer to the official documents and write it after getting familiar with it

  /// <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 documents (generated on:" + 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(as"A4")</param>
        public PDFOperation(string type)
        {
            SetPageSize(type);
            document = new Document(rect);
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="type">Page size(as"A4")</param>
        /// <param name="marginLeft">Distance from content to left border</param>
        /// <param name="marginRight">Distance from content to right border</param>
        /// <param name="marginTop">Content distance from top border</param>
        /// <param name="marginBottom">Distance from content to bottom border</param>
        public PDFOperation(string type, float marginLeft, float marginRight, float marginTop, float marginBottom)
        {
            SetPageSize(type);
            document = new Document(rect, marginLeft, marginRight, marginTop, marginBottom);
        }
        #endregion
        #region Private field
        private Font font;
        private Rectangle rect;   //Document size
        private Document document;//Document object
        private BaseFont basefont;//Typeface
        #endregion
        #region Set font
        /// <summary>
        /// Set font
        /// </summary>
        public void SetBaseFont(string path)
        {
            basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        }
        /// <summary>
        /// Set font
        /// </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(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 Instantiate document
        /// <summary>
        /// Instantiate document
        /// </summary>
        /// <param name="os">Document related information (such as path, opening method, etc.)</param>
        public void GetInstance(Stream os)
        {
            PdfWriter.GetInstance(document, os);
        }
        #endregion
        #region Open document object
        /// <summary>
        /// Open document object
        /// </summary>
        /// <param name="os">Document related information (such as path, opening method, etc.)</param>
        public void Open(Stream os)
        {
            GetInstance(os);
            document.Open();
        }
        #endregion
        #region Close open documents
        /// <summary>
        /// Close open documents
        /// </summary>
        public void Close()
        {
            document.Close();
        }
        #endregion
        #region Add paragraphs
        /// <summary>
        /// Add paragraphs
        /// </summary>
        /// <param name="content">content</param>
        /// <param name="fontsize">font size</param>
        public void AddParagraph(string content, float fontsize)
        {
            SetFont(fontsize);
            Paragraph pra = new Paragraph(content, font);
            document.Add(pra);
        }
        /// <summary>
        /// Add paragraphs
        /// </summary>
        /// <param name="content">content</param>
        /// <param name="fontsize">font size</param>
        /// <param name="Alignment">Alignment (1 center, 0 left, 2 right)</param>
        /// <param name="SpacingAfter">Number of blank lines after segment (0 is the default)</param>
        /// <param name="SpacingBefore">Number of blank lines before segment (0 is the default)</param>
        /// <param name="MultipliedLeading">Row spacing (0 is the default)</param>
        public void AddParagraph(string content, 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 center, 0 left, 2 right)</param>
        /// <param name="newWidth">Picture width (0 is the default, if the width is greater than the page width, it will be scaled by ratio)</param>
        /// <param name="newHeight">Picture height</param>
        public void AddImage(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="Content">Link text</param>
        /// <param name="FontSize">font size</param>
        /// <param name="Reference">Link address</param>
        public void AddAnchorReference(string Content, float FontSize, string Reference)
        {
            SetFont(FontSize);
            Anchor auc = new Anchor(Content, font);
            auc.Reference = Reference;
            document.Add(auc);
        }
        /// <summary>
        /// Add link point
        /// </summary>
        /// <param name="Content">Link text</param>
        /// <param name="FontSize">font size</param>
        /// <param name="Name">Link naming</param>
        public void AddAnchorName(string Content, float FontSize, string Name)
        {
            SetFont(FontSize);
            Anchor auc = new Anchor(Content, font);
            auc.Name = Name;
            document.Add(auc);
        }
        #endregion
    }

 

Extended reading: C ා tool class: use SharpZipLib to compress and decompress files.

Note: This article is reproduced from the blogger's personal technology site: IT technology fun house . Original link: View the original text.

Posted by xenoalien on Mon, 02 Dec 2019 03:24:27 -0800