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. The path parameter is not case-sensitive.įor a list of common I/O tasks, see Common I/O (doc, new FileStream(path "/Blocks.pdf", FileMode.Create)) Ĭhunk c1 = new Chunk( "A chunk represents an isolated string. To obtain the current working directory, see GetCurrentDirectory. Relative path information is interpreted as relative to the current working directory. The path parameter is permitted to specify relative or absolute path information. Additional threads are permitted to read the file while it is open. If the file does exist, write operations to the StreamWriter append text to the file. If the file specified by path does not exist, it is created. This method is equivalent to the StreamWriter(String, Boolean) constructor overload. Let path = This text is added only once to the file. Using (StreamReader sr = File.OpenText(path)) Using (StreamWriter sw = File.AppendText(path)) Using (StreamWriter sw = File.CreateText(path)) String path = This text is added only once to the file. StreamReader^ sr = File::OpenText( path ) StreamWriter^ sw = File::AppendText( path ) This text is always added, making the file longer over time StreamWriter^ sw = File::CreateText( path ) This text is added only once to the file. However, the directory named temp on drive C must exist for the example to complete successfully. The method creates a new file if the file doesn't exist. The following example appends text to a file.
0 Comments
Leave a Reply. |