Paragraph.FirstLineIndent //allows you to apply a float value to indent the first line The Paragraph class has a number of other useful properties for styling including: The following shows the earlier example with p.setAlignment( "Justify" ) This accepts a string, with "Left", "Center", "Justify", and "Right" being valid values. It is also possible to set the alignment of the paragraph text, using the tAlignment() method. Finally both phrases are added to the single Paragraph object. Other than that, each individually styled string is applied to its own Chunk object, and then added to a Phrase to ensure that lines are wrapped in the PDF. ![]() The first block of text, which is or a verbatim string literal, needs to have all the whitespace and newlines removed from it, otherwise it will appear with them preserved in the resulting PDF. That explicitly tells the compiler that the value is to be treated as a float, and prevents the sort of mistake I experienced happening again. ![]() You will also notice that the font size values are now passed in with the f suffix following them. So, exception handling starts to make its appearance, so that at least the document object is released. An exception is thrown when trying to call doc.Close(), and I have to shut down VS to release its hold on the document object. This had the effect of setting the font size to 0, which is the value that the constant is set to. When testing the code to generate the PDF file, I inadvertently transposed two arguments in the constructor for the font I called lightblue, in that I passed in the value Font. There is another source of exceptions that I found to be rather sneaky. catch when performing IO operations, and with iTextSharp Document objects, there is also a DocumentException object to manage. It didn't take long to start adding Exception handling to the code. Text = text.Replace( Environment.NewLine, String.Empty).Replace( " ", String.Empty) įont brown = new Font( Font.COURIER, 9f, Font.NORMAL, new Color(163, 21, 21)) įont lightblue = new Font( Font.COURIER, 9f, Font.NORMAL, new Color(43, 145, 175)) įont courier = new Font( Font.COURIER, 9f) įont georgia = FontFactory.GetFont( "georgia", 10f) Ĭhunk beginning = new Chunk(text, georgia) Ĭhunk c1 = new Chunk( "You can of course force a newline using \"", georgia) Ĭhunk c3 = new Chunk( "\" or ", georgia) Ĭhunk c4 = new Chunk( "Environment", lightblue) Ĭhunk c5 = new Chunk( ".NewLine", courier) Ĭhunk c6 = new Chunk( ", or even ", georgia) Ĭhunk c7 = new Chunk( "Chunk", lightblue) Ĭhunk c8 = new Chunk( ".NEWLINE", courier) Ĭhunk c9 = new Chunk( " as part of the string you give a chunk.", georgia) įirst, the result, then some notes about the code: To change or set the style of a word or phrase inline. Line when the length exceeds the available width in Chunks have no concept of how to force a new Having been written to the document but it looks a String text = result can be seen below, which shows the text PdfWriter.GetInstance(doc, new FileStream(path "/Blocks2.pdf", FileMode.Create)) The chunk has a number of methods to allow you to do this, such as setUnderLine(), setBackGround(), and setTextRise(), as well as a number of constructors that permit you to set the font and its styles. ![]() NEWLINE as part of the string you give a chunk. You can of course force a newline using " \n " or Environment. ![]() Really, all they should be used for is to change or set the style of a word or phrase inline. Chunks have no concept of how to force a new line when the length exceeds the available width in the document. The result can be seen below, which shows the text having been written to the document but it looks a mess. PdfWriter.GetInstance(doc, new FileStream(path "/Blocks.pdf", FileMode.Create)) Ĭhunk c1 = new Chunk( "A chunk represents an isolated string.
0 Comments
Leave a Reply. |