C code implements PDF document operation class

  • 2021-08-17 00:50:19
  • OfStack

This article is pure dry goods, paste PDF document operation class C # code, and need to add iTextSharp. dll reference before it can be compiled normally.

Don't talk too much nonsense, just post the code for everyone.

The code is as follows:


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 ( Such as "A4")</param>
  public PDFOperation(string type)
  {
   SetPageSize(type);
   document = new Document(rect);
  }
  /// <summary>
  ///  Constructor 
  /// </summary>
  /// <param name="type"> Page size ( Such as "A4")</param>
  /// <param name="marginLeft"> Distance between content and left border </param>
  /// <param name="marginRight"> Distance between content and 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;// Document object 
  private BaseFont basefont;// Font 
  #endregion
  #region  Set fonts 
  /// <summary>
  ///  Set fonts 
  /// </summary>
  public void SetBaseFont(string path)
  {
   basefont = BaseFont.CreateFont(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 the page size 
  /// <summary>
  ///  Set the 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  Instantiate a document 
  /// <summary>
  ///  Instantiate a 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 a document object 
  /// <summary>
  ///  Open a 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 an open document 
  /// <summary>
  ///  Close an 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"> 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"> Font size </param>
  /// <param name="Alignment"> Alignment ( 1 For the center, 0 To be on the left, 2 To the right) </param>
  /// <param name="SpacingAfter"> Number of empty lines after segment ( 0 Is the default) </param>
  /// <param name="SpacingBefore"> Number of blank lines before a segment ( 0 Is the default) </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);
   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 a picture 
  /// <summary>
  ///  Add a picture 
  /// </summary>
  /// <param name="path"> Picture path </param>
  /// <param name="Alignment"> Alignment ( 1 For the center, 0 To be on the left, 2 To the right) </param>
  /// <param name="newWidth"> Picture width ( 0 Is the default value, if the width is larger 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 a link, click 
  /// <summary>
  ///  Add a link 
  /// </summary>
  /// <param name="Content"> Linked 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 a link point 
  /// </summary>
  /// <param name="Content"> Linked text </param>
  /// <param name="FontSize"> Font size </param>
  /// <param name="Name"> Link roll call </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
 }
}

This site friendly reminder needs to pay attention to points: need to add iTextSharp. dll reference can be normal through compilation.


Related articles: