Operations on PDF-iTextSharp-list in Asp. Net

Source: Internet
Author: User

In the previous article, we have learned how to use iTextSharp to create a PDF document and set the font style and style. this article describes the ordered list and unnecessary list in iTextSharp. if you have not read the previous article, the address is:

Create PDF-iTextSharp in ASP. NET

Operate PDF-iTextSharp-Use font in Asp. Net

Operate PDF-iTextSharp-add text in Asp. Net using blocks, phrases, and paragraphs

 

The List creation in iTextSharp is implemented through the iTextSharp. text. List object. The list is actually iTextSharp. text. set of ListItem. that is, an array composed of ListItem. the ListItem inherits the Paragraph object (while the Paragraph object inherits from Phrase and Phrase inherits from Arraylist), so every List generated will automatically wrap. just as the List is divided into <ul> and <ol> in HTML, the List in iTextSharp is also divided into ordered List and unordered List. the following describes how to generate the list Code:

 

 

String path = Server. MapPath ("PDFs ");

 

It. Document doc = new it. Document ();

 

Try

 

{

 

Using writer. GetInstance (doc, new FileStream (path + "/Lists.pdf", FileMode. Create ));

 

Doc. Open ();

 

It. List list = new it. List (it. List. UNORDERED );

 

List. Add (new it. ListItem ("One "));

 

List. Add ("Two ");

 

List. Add ("Three ");

 

List. Add ("Four ");

 

List. Add ("Five ");

 

It. Paragraph paragraph = new it. Paragraph ();

 

String text = "Lists ";

 

Paragraph. Add (text );

 

Doc. Add (paragraph );

 

Doc. Add (list );

 

}

 

Catch (it. Fetch entexception dex)

 

{

 

Response. Write (dex. Message );

 

}

 

Catch (IOException ioex)

 

{

 

Response. Write (ioex. Message );

 

}

 

Finally

 

{

 

Doc. Close ();

 

}

 

 

If you do not understand the meaning of the above Code. why do I need to explain why I want to use "it" to reference the List. as shown in the code, it is used as a reference to some classes, because if you directly go to ASP. net code-behind mode, you will find that visual studio references the ListItem of iTextSharp and the System that also contains ListItem. web. UI. a namespace conflict occurs during WebControls. this means that the following code is only used:

 

 

 

ListItem li = new ListItem ();

The system reports a warning that is not explicitly referenced. The solution is to use full reference:

 

ITextSharp. text. ListItem li = new iTextSharp. text. ListItem ();

 

 

However, full reference is smelly and long, so simple reference is used here:

 

Using it = iTextSharp. text;

 

Now you can use an alias.

Return to the role of the actual code. The first thing is to create a List object and input a Boolean parameter to tell the List to generate an ordered or unordered List. the default value is False (that is, unordered List), and five items are added to the List. The first item is to pass in the String parameter type through the anonymous function to create and pass in the ListItem. From the second item, it is to directly pass in the String type parameter. the last step is to create a Paragraph object and list object to pass the document together.

As you can see, each list item occupies one row as Paragraph does. The list is unordered. A horizontal bar is used before each list item, and the list is not indented. However, iTextSharp provides multiple methods to allow you to set the list to make it more elegant:

 

 

It. List list = new it. List (it. List. UNORDERED, 10f );

 

List. SetListSymbol ("\ u2022 ");

 

List. IndentationLeft = 30f;

 

The second parameter above (float type) is passed into the List constructor to set the indent of each List item to 10 (that is, the distance between the List symbol and the first character of the List item .). Then, I changed the list symbol to a more traditional one by using the SetListSymbol method, and finally indented the entire list to the right by 30. Now the list looks much better:

 

 

 

 

If you use an ordered list and use a roman number as an identifier, you can use the RomanList class:

 

 

RomanList romanlist = new RomanList (true, 20 );

 

Romanlist. IndentationLeft = 30f;

 

Romanlist. Add ("One ");

 

Romanlist. Add ("Two ");

 

Romanlist. Add ("Three ");

 

Romanlist. Add ("Four ");

 

Romanlist. Add ("Five ");

 

Doc. Add (romanlist );

 

 

For some strange reason, the second parameter passed into the RomanList constructor is an Int type value. The first parameter indicates whether the RomanList uses uppercase or lowercase letters as the row Project identifier:

 

 

 

 

There is also a GreekList class that supports the identification of list items using Greek characters, and two other classes, ZapfDingbatsList and ZapfDingbatsNumberList, because they use the ZapfDingBats font, therefore, these two classes provide more options for the list bullet. When the Greek and Roman characters are used as the line project identifiers, there cannot be more than 24 and 26 line projects, respectively, zapfDingBatsNumberList can only process up to 10 characters. When the character range is exceeded, the list starts from 0.

 

ZapfDingbatsList zlist = new it. ZapfDingbatsList (49, 15 );

 

Zlist. Add ("One ");

 

Zlist. Add ("Two ");

 

Zlist. Add ("Three ");

 

Zlist. Add ("Four ");

 

Zlist. Add ("Five ");

 

Doc. Add (zlist );

 

Lists can also be nested with each other. Because the List. Add () method accepts an Object-type parameter, you only need to input a valid List Object. The following code first creates a RomanList object, and then creates an ordered list. When we add the RomanList object to the ordered list, the RomanList is automatically indented to the parent ordered list:

 

 

RomanList romanlist = new RomanList (true, 20 );

 

Romanlist. IndentationLeft = 10f;

 

Romanlist. Add ("One ");

 

Romanlist. Add ("Two ");

 

Romanlist. Add ("Three ");

 

Romanlist. Add ("Four ");

 

Romanlist. Add ("Five ");

 

 

 

List list = new List (List. ORDERED, 20f );

 

List. SetListSymbol ("\ u2022 ");

 

List. IndentationLeft = 20f;

 

List. Add ("One ");

 

List. Add ("Two ");

 

List. Add ("Three ");

 

List. Add ("Roman List ");

 

List. Add (romanlist );

 

List. Add ("Four ");

 

List. Add ("Five ");

 

 

 

Doc. Add (paragraph );

 

Doc. Add (list );

 

 

 


-----------------
Original article: Lists with iTextSharp
Translated by CareySon

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.