Sei sulla pagina 1di 512

The Best iText Questions on StackOverflow

iText Software

This book is for sale at http://leanpub.com/itext_so

This version was published on 2015-06-23

This version was published on 2015-06-23 This is a Leanpub book. Leanpub empowers authors and

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.

©2014 - 2015 iText Software

This book is written by a developer for developers. It is dedicated to all the developers who take pride in writing good code.

Contents

introduction

. Why StackOverflow?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1

1

Acknowledgments .

How to use this book?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2

3

Questions about PDF in general .

.

.

.

.

.

.

.

.

.

.

. What is the difference between iText, JasperReports and Adobe LC? Does a PDF file have styles, headers and footers?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

4

4

5

How to extract a page number from a PDF file?

5

How to disable the save button and hide the menu bar in Adobe Reader?

.

.

.

.

.

.

.

.

.

7

How to close a PDF file to recreate it? (File in use problem)

8

How to hide the Adobe floating toolbar when showing a PDF in browser? .

.

.

.

.

.

.

.

.

9

Why are PDF files different even if the content is the same?

 

10

Why do PDFs change when processing them?

12

How to find out if a PDF file compressed or not?

14

How to protect a PDF with a username and password?

15

. How to protect an already existing PDF with a password?

How to encrypt PDF using a certificate? .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

16

17

How to allow page extraction when setting password security?

18

How do I rotate a PDF page to an arbitrary angle?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

20

What is the size limit of pdf file?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

21

Can I change the page count by changing internal metadata?

22

How to get the page number of an arbitrary PDF object?

24

. How should I interpret the coordinates of a rectangle in PDF?

.

Where is the origin (x,y) of a PDF page? .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

25

27

Getting started

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

29

How to generate and design PDFs with iText or iTextSharp?

Argument not specified in iTextSharp

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

29

31

How to create a complex PDF document?

.

.

.

.

.

.

.

.

.

32

How to align two paragraphs to the left and right on the same line? .

.

.

.

.

.

.

.

.

.

.

.

.

33

. how to match the size of graphics with the size of a page?

How to align a label versus its content?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

35

37

How to set the page size to Envelope size with Landscape orientation? .

.

.

.

.

.

.

.

.

.

39

CONTENTS

How to use the full size of a page?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

40

How to create a document with unequal page sizes?

41

How to change the line spacing of text?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

42

Does anyone have any idea how TabStop works?

43

How to strike through text using iText?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

43

How to underline text with a dotted line?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

45

. How to create a custom dashed line separator?

How to add a full line break?

.

.

.

.

.

.

Fonts

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

46

47

How can I generate a PDF/UA compatible PDF with iText?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

49

52

How to use the font Verdana in PdfStamper?

.

.

.

.

.

.

.

.

.

.

52

Why doesn’t FontFactory.GetFont() work for all fonts?

53

Why aren’t my fonts getting registered? .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

54

How to use Cyrillic characters in a PDF?

Why is iText embedding a font even when I specify not to embed?

.

.

.

.

.

.

.

.

.

.

56

59

How to identify a single font that can print all the characters in a string?

.

.

.

.

.

.

.

.

.

.

60

How to change the font color and size when using FontSelector?

.

.

.

.

.

.

.

.

.

.

.

.

.

61

How to create Persian content in PDF?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

62

How to solve an UnsupportedCharsetException when using itext-asian.jar?

 

64

How to choose the optimal size for a font?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

65

How to restrict the number of characters on a single line?

66

How to calculate the height of an element?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

70

How to calculate/set font line distance?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

71

Why can’t I set the font of a Phrase?

How to use two different colors in a single String?

.

.

.

.

.

.

.

.

.

.

72

73

How to apply color to Strings in a Paragraph?

74

How to introduce a custom font weight?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

76

How to make a single letter bold within a word?

77

. How to allow a Unicode subscript symbol in a PDF without using setTextRise()?

How to introduce superscript?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

81

84

How to display indian rupee symbol?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

85

Why isn’t the Rupee symbol showing?

.

.

.

.

.

.

.

.

.

.

86

How to use System Font in iTextSharp with VB.net?

88

Do I need a license for Windows fonts when using iText?

93

How to get a font from an array?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

96

Images

. How to add multiple images into a single PDF?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

97

97

How to get the image DPI in PDF?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

98

Why aren’t images added sequentially?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

99

How to add text to an image?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

100

How to draw lines on an image?

.

.

.

.

.

.

.

.

.

.

103

CONTENTS

How to preserve high resolution images in PDF?

105

How to add JPEG images that are multi-stage filtered (DCTDecode and FlateDecode)?

 

107

How to avoid an exception when importing a TIFF file?

109

How to show an image with large dimensions across multiple pages?

109

. How to convert colored images to black And white?

How to give an image rounded corners? .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

111

112

How to make an image a qualified mask candidate in itext?

115

How to create unique images with Image.getInstance()?

117

How to generate 2D barcode as vector image?

120

How to change a background Image into a watermark by altering the opacity?

123

Absolute positioning of text

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

125

How to write a Zapfdingbats character at a specific location on a page? .

.

.

.

.

.

.

.

.

.

.

125

How to reduce redundant code when adding content at absolute positions?

126

How to truncate text within a bounding box?

128

How to continue an ordered list on a second page?

128

Why does ColumnText ignore the horizontal alignment?

131

. Why does using ColumnText result in “The document has no pages” exception?

How to fit a String inside a rectangle?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

132

134

How to divide a page in N parts so we can fill each with a different source? .

.

.

.

.

.

.

.

136

How to rotate a single line of text?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

140

. How to draw a rectangle around multiline text?

How to rotate a paragraph? .

. What is causing syntax errors in a page created with iText?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

141

143

146

How to adapt the position of the first line in ColumnText?

148

Absolute positioning of lines and shapes

150

How to add a border to a PDF page? .

. Why does the cell background color affect the color of other lines?

. How to create a PDF with a Cartesian grid? .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Tables

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

150

151

152

How do I set parameters back to the default value?

153

How to add a shading pattern to a custom shape?

How to right-align text in a PdfPCell?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

154

156

156

. How to use multiple fonts in a single cell?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

157

How to change width of single column of table?

How to introduce a rowspan?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

158

159

How to maintain a paragraph’s indentation inside a table cell?

.

.

.

.

.

.

.

160

What is the PdfPTable.DefaultCell property used for?

162

How to draw a borderless table in iTextSharp?

162

Why doesn’t getDefaultCell() .setBorder(PdfPCell.NO_BORDER) have any effect?

 

163

CONTENTS

How can I convert a CSV file to a table with a repeating header row?

How to split a row over multiple pages? .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

166

How to create a table based on a two-dimensional array?

168

How to nest tables without extending the inner table?

170

How to add an inner table surrounded by text?

172

How to resize an Image to fit it into a PdfPCell?

174

How to display barcodes in a matrix-like structure?

How does a PdfPCell’s height relate to the font size?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

176

177

177

How to add a table to the bottom of the last page?

178

How do setMinimumSize() and setFixedSize() interact?

179

How to get the rendered dimensions of text? How to tell iText how to clip text to fit in a cell?

.

.

.

.

.

.

.

.

.

.

.

180

182

How to resize a PdfPTable to fit the page?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

183

 

185

What’s an easy to print “first right, then down”? How to invoke a page break for nested tables?

. How to make a footer stick to bottom of every pdf page?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

187

189

How to add continue on next page / continued from next page to a table? .

.

.

.

.

.

.

.

.

190

Table events

. How to use a dotted line as a cell border?

. How to create a table with rounded corners?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

194

194

195

How to introduce rounded cells with a background color?

196

How to set background image in PdfPCell in iText?

198

How to get text and image in the same cell? .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

199

Is it possible to attach multiple layout events to a PdfPCell?

200

How to precisely position an image on top of a PdfPTable?

202

Why is my cell event not triggered?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

204

Page events

. How to add text as a header or footer?

. How to add a rectangle to every page of a document?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

206

206

208

How can I add an image to all pages of my PDF?

209

How to set a fixed background image for all my pages?

.

.

.

.

.

.

.

.

.

.

.

210

Why do I get a System.StackOverflowException in the OnEndPage() event handler?