Using System. IO;
Using iTextSharp. text;
Using iTextSharp.text.pdf;
/// <Summary>
/// PDF document operations
/// </Summary>
// -------------------------------------- Call --------------------------------------------
// Audio operation pdf = new audio operation ();
// Pdf. Open (new FileStream (path, FileMode. Create ));
// Pdf. SetBaseFont (@ "C: \ Windows \ Fonts \ SIMHEI. TTF ");
// Pdf. AddParagraph ("test document (generated at:" + DateTime. Now + ")", 15, 1, 20, 0, 0 );
// Pdf. Close ();
//--------------------------------------------------------------------------------
Public class operation
{
# Region Constructor
/// <Summary>
/// Constructor
/// </Summary>
Public writable operation ()
{
Rect = PageSize. A4;
Document = new Document (rect );
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "type"> page size (for example, "A4") </param>
Public parameter operation (string type)
{
SetPageSize (type );
Document = new Document (rect );
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "type"> page size (for example, "A4") </param>
/// <Param name = "marginLeft"> content distance from the Left Border </param>
/// <Param name = "marginRight"> content distance from the Right Border </param>
/// <Param name = "marginTop"> the content distance from the upper border </param>
/// <Param name = "marginBottom"> the content distance from the bottom border </param>
Public writable operation (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 font setting
/// <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"> font size </param>
Public void SetFont (float size)
{
Font = new Font (basefont, size );
}
# Endregion
# Region set page size
/// <Summary>
/// Set the page size
/// </Summary>
/// <Param name = "type"> page size (for example, "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>
/// Instantiate the document
/// </Summary>
/// <Param name = "OS"> document information (such as path and open mode) </param>
Public void GetInstance (Stream OS)
{
Using writer. GetInstance (document, OS );
}
# Endregion
# Region Open Document Object
/// <Summary>
/// Open the Document Object
/// </Summary>
/// <Param name = "OS"> document information (such as path and open mode) </param>
Public void Open (Stream OS)
{
GetInstance (OS );
Document. Open ();
}
# Endregion
# Region close opened documents
/// <Summary>
/// Close the opened document
/// </Summary>
Public void Close ()
{
Document. Close ();
}
# Endregion
# Add a section in region
/// <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 PHA = new Paragraph (content, font );
Document. Add (HPA );
}
/// <Summary>
/// Add a paragraph
/// </Summary>
/// <Param name = "content"> content </param>
/// <Param name = "fontsize"> font size </param>
/// <Param name = "Alignment"> Alignment </param>
/// <Param name = "SpacingAfter"> Number of empty lines after a segment (0 is the default value) </param>
/// <Param name = "SpacingBefore"> Number of empty lines before a 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 PHA = new Paragraph (content, font );
Panel. Alignment = Alignment;
If (SpacingAfter! = 0)
{
Panel. SpacingAfter = SpacingAfter;
}
If (SpacingBefore! = 0)
{
Panel. SpacingBefore = SpacingBefore;
}
If (MultipliedLeading! = 0)
{
UPA. MultipliedLeading = MultipliedLeading;
}
Document. Add (HPA );
}
# Endregion
# Add an image to region
/// <Summary>
/// Add an image
/// </Summary>
/// <Param name = "path"> image path </param>
/// <Param name = "Alignment"> Alignment </param>
/// <Param name = "newWidth"> image width (0 is the default value. If the width is greater than the page width, the image width is scaled proportionally) </param>
/// <Param name = "newHeight"> image 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
# Add links and points to region
/// <Summary>
/// Add Link
/// </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 a link
/// </Summary>
/// <Param name = "Content"> link text </param>
/// <Param name = "FontSize"> 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