{oapdf_gg}
the third part of iText high-level applicationsIX FontOriginalthis chapter stresses the use of many fonts skills, but did not say how to use the Chinese, therefore, has very little meaning, say, if we do not support the Chinese, in front of the translation will be white, so, according to the original text referred to a number of knowledge, I explored the use of Chinese characters, write their own content in this chapter should be regarded as "original" of it ^ _ ^ (Oh! who threw eggs, I get ... ...). Windows in general, the use of TrueType fonts, each of the Chinese version of Windows operating system are installed by default Song, Font, bold and italics are four fonts, you can also install other third-party fonts, such as after the installation of Office 2000 ,Ðп¬will automatically install the Chinese fonts, etc., rather strange, in the PDF document to insert a font is the computer, open the PDF document in a computer does not the font, but still able to display! This is different from the Word document, Word document to the current computer is not all fonts used in place of song, and it is perhaps right windfall. Generalfont file stored in the windir \ Fonts directory, extension TTF, also known as the expansion of the TTC font files as well as TrueType fonts, but is a collection, that is, there are a variety of fonts. The following is a list of four windows2000 standard simplified Chinese version of the font file name: SIMSUN.TTC:Song and the new song SIMKAI.TTF:italics SIMHEI.TTF:bold SIMFANG.TTF:Font body TrueType fonts Applicationby the following method to write text in bold type, size for 32 pounds: BaseFont bfHei = BaseFont.createFont (@ "c:\ winnt \ fonts \ SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font font = new Font (bfHei, 32); String text = "This is a bold test !"; document.Add (new Paragraph (text, font )); BaseFont.createFont methods leaveSecondly, the meaning of the three parameters, on the list, the first parameter is the font file shows the location of storage. behind a very good understanding of code, not repeat them. TruType fonts The application poolfont collection is almost the same above, but in Fangzhong createFont To specify which font to use. Such as: BaseFont bfSun = BaseFont.createFont (@ "c:\ winnt \ fonts \ SIMSUN.TTC, 1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); font = new Font (bfSun, 16); text = "This is a new font collection testing Song !"; document.Add (new Paragraph (text, font )); is not difficult to see, in the use of BaseFont.createFont method, the first parameter @ "c:\ winnt \ fonts \ SIMSUN.TTC, 1" in more than a "1", that the use of the serial number of 1 fonts, serial number is 0 for Song of the font. After all, we are not doingpublishing software, with the approach above is basically good enough for us, the real complexity of the production of PDF documents, XML documents may wish to make (the most simple way is to use Word Publishing, and then save for web page), and then approach the conversion of Chapter VII. sample code, see code 0901. Chapter X map as the absolute position and textpdfContentByteSo far, we have been using a simple iText, we have added the text, pictures, paragraphs, sections, lists, forms, etc., there is no problem related to the layout. Itext segmentation of text to each page and each word, sentence, paragraph to the page layout, but sometimes we do not need this kind of auto-format, and sometimes we want to put some image or the text of a page in the specified location, To achieve this feature, we will use PdfContentByte category. chapter in lieu of, only PdfWriter class method getInstance is not enough, you must have a PdfWriter real objects, you can use the Writer object through the use of getDirectContent () to get the object side. Cases: PdfWriter writer = PdfWriter.getInstance (document, new FileOutputStream ( "test.pdf ")); PdfContentByte cb = writer.DirectContent; Description:When you add high-level object (such as forms), the two objects will be PdfContentByte internal use:one for text, a use for the image (for example, the cell border or background). Rendering text in the image floating above. When youthrough getDirectContent () method of direct access to PdfContentByte object, you add all the objects will float in the text and images. If you want to avoid this situation and want to add content behind the image or text, you need to use with getDirectContentUnder () side. words, when the page is completed, 4 layer in accordance with the following order of overlap: 1, through getDirectContentUnder () received PdfContentByte 2, contains the image or object within the High PdfContentByte 3, ill text or object within the High PdfContentByte 4, through getDirectContent () received PdfContentByte simple graphicsin the sample code in 1001, drawing a number of simple graphics, we use the moveTo and lineTo as ways to move to the page in its current location and then draw a straight line to a different location. We use a method such as setLineWidth and setLineDash to change the appearance of a straight line, such as: cb.LineWidth = 10f; cb.moveTo (100, 700); cb.lineTo (200, 800); cb.stroke (); Description:When you change, such as color, line width and other properties, only methods you call when the role of stroke. Draw the triangle in the case, we set the color to green, in the use of the methodology we have to change the stroke color to red, while the results of the triangle drawn in red rather than green, which cases are rectangular, round, etc. to use. textwhen you want to write ContentByte in the text, you must use the method beginText () and endText, you must also set the font and size. The same graphics as the example, there are many ways to write and place the text, but you need most is showText methods and means showTextAligned with setTextMatrix. case 1: BaseFont bf = BaseFont.createFont (BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText (); cb.setFontAndSize (bf, 12); cb.showTextAligned (PdfContentByte.ALIGN_CENTER, text + "This text is centered", 250, 700, 0); cb.endText (); cases of 2: BaseFont bf = BaseFont.createFont (BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText (); cb.setFontAndSize (bf, 12); cb.setTextMatrix (100, 400); cb.showText ( "Text at position 100,400 ."); cb.endText (); please see the sample code 1002. |