C operates on class instances using PDF files encapsulated by iTextSharp

  • 2021-01-22 05:19:54
  • OfStack

This article illustrates C# using the PDF file operation classes encapsulated by iTextSharp. Share with you for your reference. The specific analysis is as follows:

This C# code mainly encapsulates the methods used in iTextSharp to operate PDF files. It is more convenient to access PDF documents. It can dynamically generate PDF files, add content, set paragraphs, set fonts, etc.


using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace DotNet.Utilities
{
  /// <summary>
  /// PDF Document manipulation class 
  /// </summary>
  //------------------ call --------------------------
  //PDFOperation pdf = new PDFOperation();
  //pdf.Open(new FileStream(path, FileMode.Create));
  //pdf.SetBaseFont(@"C:\Windows\Fonts\SIMHEI.TTF");
  //pdf.AddParagraph(" Test documentation (Generation time: " + DateTime.Now + " ) ", 15, 1, 20, 0, 0);
  //pdf.Close();
  //-------------------------------
  public class PDFOperation
  {
    #region  The constructor 
    /// <summary>
    ///  The constructor 
    /// </summary>
    public PDFOperation()
    {
      rect = PageSize.A4;
      document = new Document(rect);
    }
    /// <summary>
    ///  The constructor 
    /// </summary>
    /// <param name="type"> The page size ( Such as "A4")</param>
    public PDFOperation(string type)
    {
      SetPageSize(type);
      document = new Document(rect);
    }
    /// <summary>
    ///  The constructor 
    /// </summary>
    /// <param name="type"> The page size ( Such as "A4")</param>
    /// <param name="marginLeft"> Distance from content to left border </param>
    /// <param name="marginRight"> Distance from the content to the right border </param>
    /// <param name="marginTop"> Distance between content and top border </param>
    /// <param name="marginBottom"> Distance between content and 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;// The document object 
    private BaseFont basefont;// The font 
    #endregion
    #region  Set the font 
    /// <summary>
    ///  Set the font 
    /// </summary>
    public void SetBaseFont(string path)
    {
      basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    }
    /// <summary>
    ///  Set the font 
    /// </summary>
    /// <param name="size"> The 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"> The 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  Instantiation document 
    /// <summary>
    ///  Instantiation document 
    /// </summary>
    /// <param name="os"> Document related information (such as path, open method, etc.) </param>
    public void GetInstance(Stream os)
    {
      PdfWriter.GetInstance(document, os);
    }
    #endregion
    #region  Open the document object 
    /// <summary>
    ///  Open the document object 
    /// </summary>
    /// <param name="os"> Document related information (such as path, open method, etc.) </param>
    public void Open(Stream os)
    {
      GetInstance(os);
      document.Open();
    }
    #endregion
    #region  Close the open document 
    /// <summary>
    ///  Close the open document 
    /// </summary>
    public void Close()
    {
      document.Close();
    }
    #endregion
    #region  Add a paragraph 
    /// <summary>
    ///  Add a paragraph 
    /// </summary>
    /// <param name="content"> content </param>
    /// <param name="fontsize"> The font size </param>
    public void AddParagraph(string content, float fontsize)
    {
      SetFont(fontsize);
      Paragraph pra = new Paragraph(content, font);
      document.Add(pra);
    }
    /// <summary>
    ///  Add a paragraph 
    /// </summary>
    /// <param name="content"> content </param>
    /// <param name="fontsize"> The font size </param>
    /// <param name="Alignment"> Alignment ( 1 As the center, 0 To the left, 2 To the right) </param>
    /// <param name="SpacingAfter"> Number of blank lines after paragraph ( 0 Is the default value) </param>
    /// <param name="SpacingBefore"> Number of blank lines before segment ( 0 Is the default value) </param>
    /// <param name="MultipliedLeading"> Line spacing ( 0 Is the default value) </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 images 
    /// <summary>
    ///  Add images 
    /// </summary>
    /// <param name="path"> Image path </param>
    /// <param name="Alignment"> Alignment ( 1 As the center, 0 To the left, 2 To the right) </param>
    /// <param name="newWidth"> Image width ( 0 Is the default value, if the width is larger than the page width will scale by ratio) </param>
    /// <param name="newHeight"> Pictures of high </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, dots 
    /// <summary>
    ///  Add links 
    /// </summary>
    /// <param name="Content"> Link to the text </param>
    /// <param name="FontSize"> The font size </param>
    /// <param name="Reference"> The 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 a link point 
    /// </summary>
    /// <param name="Content"> Link to the text </param>
    /// <param name="FontSize"> The font size </param>
    /// <param name="Name"> Link name </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
  }
}

I hope this article will be helpful to your C# programming.


Related articles: