|Published (Last):||7 February 2006|
|PDF File Size:||3.95 Mb|
|ePub File Size:||7.59 Mb|
|Price:||Free* [*Free Regsitration Required]|
This is the third in a series of articles that looks at using the open source component, iTextSharp from within ASP. Before going on, if you would like to read earlier articles, they are:. A Chunk is the smallest significant piece of text that you can work with. It's ASP. As with the Label, you need to be careful how you use Chunks. The following snippet shows how to set the text of a Chunk, then write it to the PDF document 3 times:. MapPath "PDFs" ;. Create ;. Chunks have no concept of how to force a new line when the length exceeds the available width in the document.
Really, all they should be used for is to change or set the style of a word or phrase inline. NewLine , or even Chunk. 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.
GetFont "dax-black" ;. The Phrase is the next container in the hierarchy. A phrase is an array of chunks, and will force a newline when the length of its contents exceed the vertical margins of the document. The space between each line actually the measurement taken between the baselines of each line, or "leading" is 1. Since the default font-size was applied by iTextSharp 12pt , the code below will result in a leading of 16pt.
You can set the leading or font as part of initiating a new phrase, as well as pass it a string or chunk to set its content through the phrase's various overloaded constructors. The following snippet shows how the earlier chunk is added to a phrase 3 times, and the result. What we have seen so far is the very basic building blocks for text in PDFs. The object that you will use most often is a Paragraph, which is a sequence of Phrases and Chunks held together. Paragraphs derive from Phrase, so they autommatically fit text within the horizontal boundaries of the document, but they also force a new line for each paragraph just as in any word processing application.
The paragraph earlier in the Chunk section of this article is as good as any to experiment with. It has a number of sentences and some formatted inline text, so we can use that to build a paragraph from chunks and phrases:. Chunks have no concept of how to force a new.
Really, all they should be used for is. Replace Environment. NewLine, String. Empty ;. GetFont "georgia" , 10f ;. NewLine" , courier ;. It didn't take long to start adding Exception handling to the code. Of course, you should always use try There is another source of exceptions that I found to be rather sneaky.
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. This had the effect of setting the font size to 0, which is the value that the constant is set to. 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.
So, exception handling starts to make its appearance, so that at least the document object is released. You will also notice that the font size values are now passed in with the f suffix following them. 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.
The first block of text, which is -quoted, 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.
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. Finally both phrases are added to the single Paragraph object. It is also possible to set the alignment of the paragraph text, using the Paragraph. This accepts a string, with "Left", "Center", "Justify", and "Right" being valid values. The following shows the earlier example with p.
Chapter 6. Working with existing PDFs
In the examples for chapter 1 to 3, we've always created a new PDF document from scratch with iText. In the last couple of examples of chapter 4, we worked with an existing PDF document. We took an existing interactive PDF form and filled it out, either resulting in a pre-filled form, or resulting in a flattened document that was no longer interactive. In this example, we'll continue working with existing PDFs. We'll load an existing file using PdfReader and we'll use the reader object to create a new PdfDocument. In this chapter, we'll take it a step further.
iTextSharp - Adding Text with Chunks, Phrases and Paragraphs
This is the third in a series of articles that looks at using the open source component, iTextSharp from within ASP. Before going on, if you would like to read earlier articles, they are:. A Chunk is the smallest significant piece of text that you can work with. It's ASP. As with the Label, you need to be careful how you use Chunks. The following snippet shows how to set the text of a Chunk, then write it to the PDF document 3 times:. MapPath "PDFs" ;.
Adding content with PdfStamper Part 1 (iText 5)