SoFunction
Updated on 2025-04-06

C# code implements PDF document operation class

This article is purely informative, and the PDF document operation C# code is posted.You need to add a reference to compile normally.

I won’t say much nonsense, I will just post code to you.

The code is as follows:

using ;
using ;
using ;
namespace 
{
 /// <summary>
 /// PDF document operation class /// </summary>
 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //PDFOperation pdf = new PDFOperation();
 //(new FileStream(path, ));
 //(@"C:\Windows\Fonts\");
 //("Test Document (Generation Time: " + + ")", 15, 1, 20, 0, 0); //();
 //-------------------------------------------------------------------------------------
 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 (such as "A4")</param>  public PDFOperation(string type)
  {
   SetPageSize(type);
   document = new Document(rect);
  }
  /// &lt;summary&gt;
  /// Constructor  /// &lt;/summary&gt;
  /// <param name="type">Page size (such as "A4")</param>  /// <param name="marginLeft">Distance from the left border</param>  /// <param name="marginRight">Distance from the right border</param>  /// <param name="marginTop">Distance from the upper border</param>  /// <param name="marginBottom">Distance from the lower 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;//Font  #endregion
  #region Set fonts  /// &lt;summary&gt;
  /// Set font  /// &lt;/summary&gt;
  public void SetBaseFont(string path)
  {
   basefont = (path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  }
  /// &lt;summary&gt;
  /// Set font  /// &lt;/summary&gt;
  /// <param name="size">Font size</param>  public void SetFont(float size)
  {
   font = new Font(basefont, size);
  }
  #endregion
  #region Set page size  /// &lt;summary&gt;
  /// Set page size  /// &lt;/summary&gt;
  /// <param name="type">Page size (such as "A4")</param>  public void SetPageSize(string type)
  {
   switch (())
   {
    case "A4":
     rect = PageSize.A4;
     break;
    case "A8":
     rect = PageSize.A8;
     break;
   }
  }
  #endregion
  #region Instantiated Document  /// &lt;summary&gt;
  /// Instantiate the document  /// &lt;/summary&gt;
  /// <param name="os">Document-related information (such as path, opening method, etc.)</param>  public void GetInstance(Stream os)
  {
   (document, os);
  }
  #endregion
  #region Open the document object  /// &lt;summary&gt;
  /// Open the document object  /// &lt;/summary&gt;
  /// <param name="os">Document-related information (such as path, opening method, etc.)</param>  public void Open(Stream os)
  {
   GetInstance(os);
   ();
  }
  #endregion
  #region Close open document  /// &lt;summary&gt;
  /// Close the open document  /// &lt;/summary&gt;
  public void Close()
  {
   ();
  }
  #endregion
  #region Add paragraph  /// &lt;summary&gt;
  /// Add paragraph  /// &lt;/summary&gt;
  /// <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);
   (pra);
  }
  /// &lt;summary&gt;
  /// Add paragraph  /// &lt;/summary&gt;
  /// <param name="content">Content</param>  /// <param name="fontsize">Font size</param>  /// <param name="Alignment">Alignment (1 is centered, 0 is left, and 2 is right)</param>  /// <param name="SpacingAfter">Number of empty lines after segment (0 is the default value)</param>  /// <param name="SpacingBefore">Number of empty lines before segment (0 is the default value)</param>  /// <param name="MultipliedLeading">Line 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);
    = Alignment;
   if (SpacingAfter != 0)
   {
     = SpacingAfter;
   }
   if (SpacingBefore != 0)
   {
     = SpacingBefore;
   }
   if (MultipliedLeading != 0)
   {
     = MultipliedLeading;
   }
   (pra);
  }
  #endregion
  #region Add image  /// &lt;summary&gt;
  /// Add picture  /// &lt;/summary&gt;
  /// <param name="path">Picture path</param>  /// <param name="Alignment">Alignment (1 is centered, 0 is left, and 2 is right)</param>  /// <param name="newWidth">Image width (0 is the default value, if the width is greater than the page width, it will be scaled by ratio)</param>  /// <param name="newHeight">High picture</param>  public void AddImage(string path, int Alignment, float newWidth, float newHeight)
  {
   Image img = (path);
    = Alignment;
   if (newWidth != 0)
   {
    (newWidth, newHeight);
   }
   else
   {
    if ( &gt; PageSize.)
    {
     (,  *  / );
    }
   }
   (img);
  }
  #endregion
  #region Add links, points  /// &lt;summary&gt;
  /// Add a link  /// &lt;/summary&gt;
  /// <param name="Content">Link text</param>  /// <param name="FontSize">FontSize</param>  /// <param name="Reference">Link address</param>  public void AddAnchorReference(string Content, float FontSize, string Reference)
  {
   SetFont(FontSize);
   Anchor auc = new Anchor(Content, font);
    = Reference;
   (auc);
  }
  /// &lt;summary&gt;
  /// Add link point  /// &lt;/summary&gt;
  /// <param name="Content">Link text</param>  /// <param name="FontSize">FontSize</param>  /// <param name="Name">Link name</param>  public void AddAnchorName(string Content, float FontSize, string Name)
  {
   SetFont(FontSize);
   Anchor auc = new Anchor(Content, font);
    = Name;
   (auc);
  }
  #endregion
 }
}

I would like to remind you that you need to add a reference to compile normally.