[Itextsharp learning notes] use the basic construction block of itext
Source: Internet
Author: User
Chunk (Block) Chunk is the smallest object added to the document object to represent the text. The chunk object contains a stringbuffer used to store text. characters in the text are in the same font format (style, size, color). These formats are set in the font object. The chunk can also use the member function to change other attributes, such as the background color, top (bottom) Mark, underline, and strikethrough. The example used by the author in the second version of itext in action is based on a database. In order to reduce the complexity, I did not use the database during the learning process, it uses some simple operations to implement the itext function. The following is an example of using Chunk: document DOC = new document (pagesize. b5); writable writer = writable writer. getinstance (Doc, new filestream ("F: \ testmode", filemode. create); writer. initialleading = 30; Doc. open (); Doc. add (New chunk ("China"); Doc. add (chunk. newline); font = new font (font. fontfamily. courier, 12, Font. bold, basecolor. white); chunk content = new chunk ("Beijing", font); content. setbackground (basecolor. blac K, 1f, 1f, 1f, 1f); content. setunderline (1f,-1f); Doc. add (content); Doc. close (); the author said, this example is special, because the chunk object is used to combine into other objects such as phrase or paragraph to represent text, generally, the chunk object is not directly added to the document object, except for some special ones such as chunk. newline. The row spacing (Leading) chunk object does not automatically set the row spacing. The setinitialleading operation of volume writer is used to set the row spacing. However, in itextsharp, it is implemented by directly assigning values to the initialleading attribute of volume writer. In the preceding example, if no row spacing is set, the two chunk objects are written in the same row. The font object itext (including the chunk object) uses the Helvetica font of 12pt by default, and this default setting cannot be changed. If you want to use objects with other fonts, you need to create a factory class to produce objects with the required fonts. Font font = new font (font. fontfamily. Courier, 12, Font. Bold, basecolor. White); Create a font object, use the courier font, number 12,, white. Content. setbackground (basecolor. Black, 1f, 1f, 1f, 1f); set the background color of the font to black. Doc. Add (content); phrase (phrase) phrase object is a list of chunk objects with row spacing. Document Doc = new document (pagesize. b5); writable writer = writable writer. getinstance (Doc, new filestream ("F: \ testmode", filemode. create); Doc. open (); font bold_underlined = new font (font. fontfamily. times_roman, 12, Font. bold | font. underline); font normal = new font (font. fontfamily. times_roman, 12); phrase = new phrase (); phrase. leading = 50; chunk chunk1 = new chunk ("Hello! ", Bold_underlined); chunk chunk2 = new chunk (" how are you? ", Normal); phrase. add (chunk1); phrase. add (chunk. newline); phrase. add (chunk2); Doc. add (phrase); document DOC = new document (pagesize. b5); writable writer = writable writer. getinstance (Doc, new filestream ("F: \ testmode", filemode. create); Doc. open (); font bold_underlined = new font (font. fontfamily. times_roman, 12, Font. bold | font. underline); font normal = new font (font. fontfamily. times_roman, 12); phrase Phra Se = new phrase (); chunk chunk1 = new chunk ("Hello! ", Bold_underlined); chunk chunk2 = new chunk (" how are you? ", Normal); phrase. add (chunk1); phrase. add (chunk. newline); phrase. add (chunk2); Doc. add (phrase); Doc. close (); in the preceding example, you do not need to set the row spacing for the mongowriter object. phrase sets the default row spacing between two chunk objects. In itext, the row spacing is set to 1.5 times the text size in phrase or paragraph. In itextsharp, you can assign values to the leading attribute of phrase to set the row spacing. The available fonts in the embedded font class usually refer to the standard type 1 font set. Itext does not embed these fonts into the document. The stand type 1 font set has been called the built-in or base 14 font set. These fonts do not support character sets except the United States and Western Europe. If other character sets are used, you must use other methods. Using a Chinese font with the basefont object allows itext to find the font at the specified position and determine whether to embed the font into the document. Basefont simheibase = basefont. createfont (@ "C: \ WINDOWS \ fonts \ simhei. TTF ", basefont. identity_h, basefont. embedded); font simheifont = new font (simheibase, 12); If chunk. newline wrap. itext uses the default font. If new chunk ("\ n", normal) is used, the font set for Chunk is used. The paragraph object paragraph can be understood as a phrase object that can be configured with more attributes and a line break. The paragraph class inherits from the phrase class. The method for creating a paragraph instance is the same as that for creating a phrase instance. However, the paragraph instance can be used to set text alignment, indentation, and front and back space. You can set alignment to adjust the alignment of text in the paragraph object. For example: // set the alignment of the paragraph object to contentpara1.alignment = element. align_justified. By default, the left alignment is used. In itextsharp, paragraph has three attributes to set indent: // set paragraph object indent Limit = 20; contentpara1.indentationright = 20; contentpara1.firstlineindent = 20; indentationleft attribute to set the left indent. The indentationright attribute sets the right indent. The firstlineindent attribute sets the indent on the left of the first line. All three values can be set as positive values. In itextsharp, you can set the spacingbefore and spacingafter attributes of paragraph to adjust the spacing between paragraph objects and paragraphs after or between them. For example: // set the spacing between the paragraph object and the following paragraph object contentpara1.spacingafter = 36; text branch problem itext default rule is to put as many words as possible in the same row. In itext, if a space or a hyphen occurs before it is split, you can use a new delimiter (split character) to change this rule. The split character uses nonbreaking space character, (char) 160 instead of ordinary space (char) 32 to put them in the middle of two words, thus avoiding itext putting them in different rows.
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.