Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use iTextSharp to manipulate PDF documents in C #

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use iTextSharp to operate PDF documents in C #, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

ITextSharp is a component DLL program for operating PDF documents, iTextSharp components can be referenced in the C # program for the development of reports related to PDF documents and other functions, using the method interface provided by iTextSharp components, we can achieve a lot of operations related to PDF documents, such as opening PDF document objects, adding paragraphs to PDF documents, adding links to pictures, etc., the function is very powerful. Here we simply encapsulate the iTextSharp class and provide some common PDF operation methods.

ITextSharp official website: http://www.itextpdf.com/ (good English suggestions directly view the original document).

In the course of a project developed by Visual Studio, you can manually import two DLL files of iTextSharp into your project, which can be used in the project after the introduction is successful. If you have the NuGet tool installed on your Visual Studio, you can install it automatically through the NuGet tool.

The PDF operation help classes encapsulated by iTextSharp components are as follows, including only some simple operations. If readers have more complex requirements, please refer to the official documents and be familiar with them and then package and write them yourself:

Public class PDFOperation

{

# region constructor

/ / /

/ / Constructor

/ / /

Public PDFOperation ()

{

Rect = PageSize.A4

Document = new Document (rect)

}

/ / /

/ / Constructor

/ / /

/ / Page size (such as "A4")

Public PDFOperation (string type)

{

SetPageSize (type)

Document = new Document (rect)

}

/ / /

/ / Constructor

/ / /

/ / Page size (such as "A4")

/ / the distance between the content and the left border

/ / the distance between the content and the right border

/ / the distance between the content and the top border

/ / the distance between the content and the bottom border

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 sets fonts

/ / /

/ set font

/ / /

Public void SetBaseFont (string path)

{

Basefont = BaseFont.CreateFont (path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)

}

/ / /

/ set font

/ / /

/ / Font size

Public void SetFont (float size)

{

Font = new Font (basefont, size)

}

# endregion

# region sets the page size

/ / /

/ / set the page size

/ / /

/ / Page size (such as "A4")

Public void SetPageSize (string type)

{

Switch (type.Trim ())

{

Case "A4":

Rect = PageSize.A4

Break

Case "A8":

Rect = PageSize.A8

Break

}

}

# endregion

# region instantiation document

/ / /

/ / instantiate the document

/ / /

/ / document-related information (such as path, opening method, etc.)

Public void GetInstance (Stream os)

{

PdfWriter.GetInstance (document, os)

}

# endregion

# region Open document object

/ / /

/ / Open the document object

/ / /

/ / document-related information (such as path, opening method, etc.)

Public void Open (Stream os)

{

GetInstance (os)

Document.Open ()

}

# endregion

# region closes open documents

/ / /

/ / close the open document

/ / /

Public void Close ()

{

Document.Close ()

}

# endregion

# region add paragraph

/ / /

/ / add a paragraph

/ / /

/ / content

/ / Font size

Public void AddParagraph (string content, float fontsize)

{

SetFont (fontsize)

Paragraph pra = new Paragraph (content, font)

Document.Add (pra)

}

/ / /

/ / add a paragraph

/ / /

/ / content

/ / Font size

/ / one alignment (1 for center, 0 for left, 2 for right)

/ / the number of blank lines after the paragraph (0 is the default)

/ / number of blank lines in front of the paragraph (0 is the default)

/ / Line spacing (0 is the default)

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 Picture

/ / /

/ / add a picture

/ / /

/ / Image path

/ / one alignment (1 for center, 0 for left, 2 for right)

/ / Picture width (0 is the default, and will be scaled proportionally if the width is greater than the page width)

/ / the picture is high

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 and points

/ / /

/ / add a link

/ / /

/ / Link text

/ / Font size

/ / Link address

Public void AddAnchorReference (string Content, float FontSize, string Reference)

{

SetFont (FontSize)

Anchor auc = new Anchor (Content, font)

Auc.Reference = Reference

Document.Add (auc)

}

/ / /

/ / add link points

/ / /

/ / Link text

/ / Font size

/ / Link Roll call

Public void AddAnchorName (string Content, float FontSize, string Name)

{

SetFont (FontSize)

Anchor auc = new Anchor (Content, font)

Auc.Name = Name

Document.Add (auc)

}

# endregion

}

Thank you for reading this article carefully. I hope the article "how to use iTextSharp to operate PDF documents in C#" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report