The Document Has No Pages Itextsharp C# Download

Copy and paste this link to your website, so they can see this document directly without any plugins.



Keywords

that, with, have, iText, this, create, cell, using, This, text, your, about, StackOverflow, table, page, questions, image, when, Posted, from, different, like, question, want, need, PdfPCell, people, font, some, only

Transcript

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-10-22
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Why StackOverflow? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
How to use this book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Questions about PDF in general . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
What is the difference between iText, JasperReports and Adobe LC? . . . . . . . . . . . . 4
Does a PDF file have styles, headers and footers? . . . . . . . . . . . . . . . . . . . . . . 5
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
How to generate and design PDFs with iText or iTextSharp? . . . . . . . . . . . . . . . . 6
How to create a complex PDF document? . . . . . . . . . . . . . . . . . . . . . . . . . . 8
How to set the page size to Envelope size with Landscape orientation? . . . . . . . . . . 9
Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
How to use the font Verdana in PdfStamper? . . . . . . . . . . . . . . . . . . . . . . . . 11
Why doesn’t FontFactory.GetFont() work for all fonts? . . . . . . . . . . . . . . . . . . . 12
Why aren’t my fonts getting registered? . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Why aren’t images added sequentially? . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
How to get the image DPI in PDF? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
How to preserve high resolution images in PDF? . . . . . . . . . . . . . . . . . . . . . . 17
Absolute positioning of text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
How to write a Zapfdingbats character at a specific location on a page? . . . . . . . . . . 19
How to reduce redundant code when adding content at absolute positions? . . . . . . . . 20
Why does ColumnText ignore the horizontal alignment? . . . . . . . . . . . . . . . . . . 22
Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
How to right-align text in a PdfPCell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
How to use multiple fonts in a single cell? . . . . . . . . . . . . . . . . . . . . . . . . . . 25
How to introduce a rowspan? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
How to change width of single column of table? . . . . . . . . . . . . . . . . . . . . . . . 28
What is the PdfPTable.DefaultCell property used for? . . . . . . . . . . . . . . . . . . 29
CONTENTS
How to draw a borderless table in iTextSharp? . . . . . . . . . . . . . . . . . . . . . . . . 29
Why doesn’t getDefaultCell() .setBorder(PdfPCell.NO_BORDER) have any effect? . . 30
Table events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
How to use a dotted line as a cell border? . . . . . . . . . . . . . . . . . . . . . . . . . . 32
How to create a table with rounded corners? . . . . . . . . . . . . . . . . . . . . . . . . . 33
Page events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
How to add a rectangle to every page of a document? . . . . . . . . . . . . . . . . . . . . 35
How can I add an image to all pages of my PDF? . . . . . . . . . . . . . . . . . . . . . . 36
How to set a fixed background image for all my pages? . . . . . . . . . . . . . . . . . . . 38
Parsing XML and XHTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Why is it so difficult to convert XML to PDF? . . . . . . . . . . . . . . . . . . . . . . . . 40
How to add external CSS while generating PDF? . . . . . . . . . . . . . . . . . . . . . . 41
How to do HTML to XML conversion to generate closed tags? . . . . . . . . . . . . . . . 42
Inspect a PDF with iText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Why do I get an “InvalidPdfException: PDF header signature not found”? . . . . . . . . 44
How to Get PDF page width and height? . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Manipulating existing PDFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
How to update a PDF without creating a new PDF? . . . . . . . . . . . . . . . . . . . . . 46
How to add an image watermark to a PDF file? . . . . . . . . . . . . . . . . . . . . . . . 47
Why does the function to concatenate / merge PDFs cause issues in some cases? . . . . . 48
How to merge documents correctly? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Interactive forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
How to fill out a pdf file programmatically? (AcroForm technology) . . . . . . . . . . . . 54
How to fill out a pdf file programmatically? (Dynamic XFA) . . . . . . . . . . . . . . . . 55
How to fill XFA form using iText without breaking usage rights? . . . . . . . . . . . . . 57
Actions and annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
How to create a link to a specific page number? . . . . . . . . . . . . . . . . . . . . . . . 59
How to insert a “linked rectangle” with iText? . . . . . . . . . . . . . . . . . . . . . . . . 60
How to add a maps with a pointer to a PDF? . . . . . . . . . . . . . . . . . . . . . . . . 61
Extracting text from PDFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
How to read text from a specific position? . . . . . . . . . . . . . . . . . . . . . . . . . . 64
General questions about iText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Unit Testing and Automated Testing Questions . . . . . . . . . . . . . . . . . . . . . . . 66
Why do I get a “Could not find PdfGraphics2D” error? . . . . . . . . . . . . . . . . . . . 67
Why do I get a “getOutputStream() has already been called for this response” error in JSP? 68
CONTENTS
Legal questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
What is the difference between Lowagie and iText? . . . . . . . . . . . . . . . . . . . . . 70
Can iText 2.1.7 or earlier be used commercially? . . . . . . . . . . . . . . . . . . . . . . . 71
To be continued… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
introduction
A couple of years ago, I decided to self-publish new books about iText, as opposed to working with
a publisher as I did before for the “iText in Action” books. This led to a book about digital signatures
that is available for download¹ on the iText site, and a book called “The ABC of PDF”² published on
LeanPub. The goal of “The ABC of PDF” was to start with a book that looks at PDF at the lowest
level, examining the syntax of a PDF file and a PDF page, and then to continue writing a series of
books that explain how to use iText on a higher level, answering questions such as:
• How to create a PDF from scratch?
• How to create PDF from HTML?
• How to fill out PDF forms?
• How to parse a PDF file?
• …
However, in spite of the fact that more than 15,000 people downloaded “The ABC of PDF”, it turned
out that people really wanted me to write a different kind of book. I’ve received many comments
through LeanPub from people who were disappointed that the ABC-book didn’t explain how to
use iText. They expected a book with more practical examples, instead of examples that helps them
understand the PDF specification. Some people even used the feedback form to ask me technical
questions. Unfortunately, I was unable to answer these questions, because the people posting them
didn’t realize that I received these questions anonymously. Even if I knew the answers, I didn’t know
who or where to send them to.
All of this faced me with a dilemma: do I stop writing “The ABC of PDF” and start writing one of
the other books that were planned? If so, which part of iText is most important to iText users? The
plan for the ABC was to write a book of about 150 pages, but much to my surprise, I was only half
way when I finished writing page 150. Didn’t I have other writing priorities?
Then suddenly I had an idea: why not write a book with questions and answers? Why not create a
book entitled “The Best iText Questions on StackOverflow?”
Why StackOverflow?
I joined StackOverflow on August 24, 2012. Up until then, I had been answering many questions
on the iText mailing-list. This mailing-list hosted on SourceForge used to be an important source
¹http://itextpdf.com/book/digitalsignatures
²https://leanpub.com/itext_pdfabc/
introduction 2
of inspiration. I composed two “iText in Action” books for Manning Publications, simply by
reorganizing the many answers and examples written in answer to question into a real book.
However, at some point I got tired of the mailing-list. When I referred to an example in one of my
books, people would accuse me for trying to “trick them into buying my book.” The mailing-list was
also used by people spreading false allegations, such as “iText is no longer open source.” One could
explain that these people were wrong, for instance by providing a link to the source code³, but there
was no way to award people for providing good answers and to discourage people from posting bad
answers. It felt as if the ungrateful were winning the debate.
Then I discovered StackOverflow⁴ where people build a reputation getting reputation points when
they ask good questions and provide good answers, losing points when they post bad questions or
bad answers. I took me 2 years and almost 2 months to become a Trusted User, a status that requires
20,000 reputation points. Since I registered on StackOverflow, I have posted answers to more than
1,000 questions. Looking back at some of the more elaborate answers, I thought it would be a good
idea to bundle those questions and answers that are of “book quality”.
Acknowledgments
I have selected nothing but questions I have answered myself⁵, but it goes without saying that I
can’t answer every single question about iText personally. For instance: when I am travelling, I am
off-line for many hours. As unanswered questions about iText give me stress, I am always happy to
see that other people jump in when I’m away from my keyboard.
I want to thank Alexis Pigeon for editing many iText questions in order to clarify what is asked. I
rely on Chris Haas for answering questions that require the C# skills that I am missing. I notice that
I skip questions about digital signatures, because I know that Michael Klink’s answer will be much
more accurate than mine.
I also want to thank the many people who accepted one of my answers, because that’s how one
builds a reputation on StackOverflow. I know that some people down-vote me because my style can
be harsh at times. Somebody once tweeted: “Spent a lot of time today on StackOverflow and realized
that Bruno Lowagie is kind of a dick.” Ah well, I hope that the balance is positive.
Please understand that it is hard for me when people talk about “Lowagie” as if it’s a thing, not a
person. Sometimes people start by saying that they are using “Lowagie software” and then they start
cursing at me if I give them an answer they don’t like, for instance: please use a more recent version
instead of a version that has been declared “End of Life” more than five years ago. So it goes… Not
every developer realizes that I’m on their side and that their job is much easier if only their boss
would purchase a commercial iText license so that they can use the most recent version.
³https://github.com/itext/itextpdf
⁴http://stackoverflow.com
⁵http://stackoverflow.com/users/1622493/bruno-lowagie
introduction 3
How to use this book?
I’ve tried organizing the questions and answers in different categories. This wasn’t always simple.
If somebody asks a question about adding an image to a table, should this question be categorized
under “images” or under “tables”? If there’s a question about XHTML content that needs to be added
to a column, is that an “XML” or a “ColumnText” question? A book isn’t a web site where you can
easily introduce a taxonomy. That’s why I took great care when creating the table of contents.
In many cases, I rephrased the original question so that you understand what a question is about at
a glance, just by browsing the bookmarks. In some cases, I even had to rewrite the question.
All the questions are marked with a question mark icon like this:
this is a question
At times, I throw in a question of myself to clarify things. These questions are marked with an
information icon:
This is an extra question added by myself
Sometimes, it was important to add a comment that was made on StackOverflow. I have marked
comments like this:
This is a comment
I hope you enjoy this book, and that it helps you solving all your iText problems. If not, please post
a question on StackOverflow⁶ and, who knows, maybe your question will be added to this book.
⁶http://stackoverflow.com
Questions about PDF in general
When posting a question on StackOverflow, people can tag their posts as iText or iTextSharp
questions. This allows me to quickly find those questions by performing a simple query for posts
tagged as itext* questions⁷. This includes the tags itext, itextsharp, itextpdf and itextg.
However, not all questions tagged this way are iText-related. Sometimes, people using iText have
questions that are about PDF in general.
What is the difference between iText, JasperReports
and Adobe LC?
Actually I want to know the difference or comparison between different PDF creation /
generation techniques. For Example: iText, Adobe LC, Jasper Reports, etc.
I would like to know the exact advantage / disadvantage of using each of them.
Currently I am using Adobe LC ES2 and would like to also know the advantage of using
Adobe software over other techniques.
Posted on StackOverflow on Mar 19, 2013 ⁸ by user2185226⁹
That’s a very broad question and I see that it already has a vote to close the question for this reason.
Let me give the nutshell version of the answer. I could easily write a book on this topic (and maybe
one day I will).
• iText is a library that can be used by developers to enhance their web and other applications
with PDF functionality: create PDF, fill out PDF forms, examine and manipulate existing
PDFs.
• JasperReports is a Business Intelligence / Reporting tool that uses an old iText version to create
reports. It is distributed by JasperSoft / TIBCO. JasperReports only uses a limited part of the
complete iText functionality. Creating PDF is just one of many features of JasperReports, and
JasperSoft uses iText to implement that feature.
⁷http://stackoverflow.com/questions/tagged/itext*
⁸http://stackoverflow.com/questions/15492738/difference-between-itext-and-adobe-lc
⁹http://stackoverflow.com/users/2185226/user2185226
Questions about PDF in general 5
• Adobe LC is a suite of modules, some of which can only be provided by Adobe. For instance:
no third party can “Reader enable” PDF documents because Reader enabling requires a private
key that is proprietary to Adobe. However: iText competes with Adobe LC in some areas, for
instance digital signing (read the white paper from the Office of Legislative Counsel on digital
signatures¹⁰) and form filling (iText has an add-on called XFAWorker¹¹ that can convert your
dynamic XFA forms into static PDF, e.g. PDF/A)
Does a PDF file have styles, headers and footers?
Does a PDF file have styles, headers and footers information as is the case with docx files
that have separate xml files with extra information?
Posted on StackOverflow on Jan 21, 2014 ¹² by Prakhar¹³
Regular PDFs don’t have styles, but different fonts (for instance Helvetica is one font, Helvetica-Bold
is another font of the same family). They don’t have headers and footers, just like they don’t have
paragraphs, section titles, table rows or table cells. Everything you see in a PDF page, is just a bunch
of glyphs, paths and shapes drawn on a canvas.
However: if your PDF is a Tagged PDF, the PDF contains something that is known as the
StructTreeRoot. This means that, apart from the presentation of the content, you also have a tree
structure that stores the semantics of the content. This structure contains references to the content
on the different pages, allowing you (for instance) to find out which lines belong together in a
paragraph, which parts of the page are “artifacts” (such as a repeating page header or a footer with
a page number), which content is organized as a table, etc…
Tagged PDF is a requirement for PDF/A Level A and PDF/UA documents. A majority of the PDF
files you can find in the wild aren’t tagged (or aren’t tagged properly).
¹⁰http://www.mnhs.org/preserve/records/legislativerecords/docs_pdfs/CA_Authentication_WhitePaper_Dec2011.pdf
¹¹http://itextpdf.com/product/xfa_worker
¹²http://stackoverflow.com/questions/21259333/does-pdf-has-styles-headers-and-footers-information-as-docx
¹³http://stackoverflow.com/users/1881995/prakhar
Getting started
The most popular iText example is the “Hello World” example, explaining the five steps to create a
PDF from scratch using iText:
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World!"));
// step 5
document.close();
Obviously, iText is capable of doing much more than creating a PDF that shows the words “Hello
World”, but let’s take a look at some basic questions to get started.
How to generate and design PDFs with iText or
iTextSharp?
I’m wondering what is the best/easiest way to design a PDF document? Is it remotely
legit to actually design a whole PDF document with iTextSharp with code (i.e not loading
external files)? I want the final result to look similar to a web page with various colors,
borders, images and everything.
Or do you have to rely on other documents like .doc, .html files to achieve a good design?
Originally I thought that I would use HTML markup to generate a PDF, but why even use
a HTML markup or a template file to create the PDF design when I could just do it right
within the PDF without having to rely on on various files that serves no real purpose.
Is it possible to generate and design big PDF documents using code and are there any more
proper guides or similar with all the various commands to generate texts, images, borders
and everything since I have no real clue about generating PDF with code.
Posted on StackOverflow on Oct 6, 2014 ¹⁴ by HenrikP¹⁵
¹⁴http://stackoverflow.com/questions/26218444/generate-and-design-pdf-with-itextsharp-or-similar
¹⁵http://stackoverflow.com/users/2914876/henrikp
Getting started 7
The question is very broad, so I can only give you a very broad answer.
Option 1: you create your layout by using iText’s high-level objects. There are countless applications
out there that are using PdfPTable to generate complex reports. For instance: the time tables for a
German Railway company are created from scratch through code; the invoices for a Belgian Telco
company are created this way,… The advantage of this approach is that you can really fine-tune the
layout. The disadvantage is that you need to change source code as soon as you want to change the
layout.
Option 2: you create your layout by creating an AcroForm template. Every field in this template
has a name and is visualized at exact positions (defined by its coordinates) on specific pages. The
code to fill out such a form consists of only a handful of lines. Whenever you need to change the
layout, you alter the AcroForm template. You do not need to change your code. The disadvantage is
that AcroForms are very static. Compare it to a paper form: you can’t insert a row in a paper form
either.
Option 3: you create your data in XHTML format and your styles in CSS. A Belgian printing
company responsible for creating invoices for its customers is streaming data into very simple HTML
files involving a sequence of tables that never span more than a handful of pages. These files are
then fed to iText’s XML worker along with a CSS that is different for each of its customers. The
advantage of this approach is that no extra programming is needed when a new customer joins. It’s
just a matter of creating a new CSS. The disadvantage is that you are limited by the HTML format.
Elementary logic also tells you that you shouldn’t expect URL2PDF: have you ever tried printing
a website? Well, the bad quality of that print should give you an indication of the problems you’ll
encounter when trying to convert HTML to PDF. If you anticipate them, you can get good results.
If you don’t: it’s a poor craftsman who blames his tools…
Option 4: define your template using the XML FormsArchitecture (XFA). Such templates are usually
created using Adobe LiveCycle Designer. An XSD is fed into LC Designer and the result is an empty
form where the PDF format acts as a container for an XML stream. You can then use iText to inject
your custom XML containing data that conforms with the XSD into the PDF and you can use XFA
Worker to flatten such a form. XFAWorker is only available as a closed source product (givers need
to set limits because takers rarely do).
Option 5: right now XML Worker is used to convert XHTML+CSS and XFA to PDF (ordinary PDF,
PDF/A, PDF/UA). You could use the generic XMLWorker engine to support your own XML format.
The advantage would be a very powerful engine that you can tune to meet your exact needs. The
disadvantage is that this involves a serious up-front development investment.
Option 6: use a third party tool to define the template and a third party server that uses iText under
the hood to create PDFs based on the template. An example of such a third party tool is Scriptura
developed by Inventive Designers. There are other tools, but Inventive Designers is a customer of
iText and we know that they are using iText correctly whereas we don’t have this guarantee from
other vendors.
Getting started 8
How to create a complex PDF document?
I have an Java/Java EE based application wherein I have a requirement to create PDF
certificates for various services that will be provided to the users. I am looking for a way
to create PDF (no need for digital certificates for now). What is the easiest and convenient
way of doing that? I have tried
1. XSL to PDF conversion
2. HTML to PDF conversion using itext.
3. the crude Java way (using PdfWriter, PdfPTable, etc.)
What is the best way out of these, or is there any other way which is easier and convenient?
Posted on StackOverflow on Jan 4, 2013 ¹⁶ by Ankit¹⁷
When you talk about Certificates, I think of standard sheets that look identical for every receiver of
the certificate, except for:
• the name of the receiver,
• the course that was followed by the receiver,
• a date.
If this is the case, I would use any tool that allows you to create a fancy certificate (Acrobat,
Open Office, Adobe InDesign,…) and create a static form (sometimes referred to as an AcroForm)
containing three fields: name, course, date.
I would then use iText to fill in the fields like this:
PdfReader reader = new PdfReader(pathToCertificateTemplate);
PdfStamper stamper =
new PdfStamper(reader, new FileOutputStream(pathToCertificate));
AcroFields form = stamper.getAcroFields();
form.setField("name", name);
form.setField("course", course);
form.setField("date", date);
stamper.setFormFlattening(true);
stamper.close();
reader.close();
¹⁶http://stackoverflow.com/questions/14151335/creating-complex-pdf-using-java
¹⁷http://stackoverflow.com/users/810176/ankit
Getting started 9
Creating such a certificate from code is “the hard way”; creating such a certificate from XML is “a
pain” (because XML isn’t well-suited for defining a layout), creating a certificate from (HTML +
CSS) is possible with iText’s XML Worker, but all of these solutions have the disadvantage that it’s
hard work to position every item correctly, to make sure everything fits on the same page, etc…
It’s much easier to maintain a template with fixed fields. This way, you only have to code once. If
for some reason you want to move the fields to another place, you only have to change the template,
you don’t have to worry about messing around in code, XML, HTML or CSS.
Please go to the section about interactive forms to learn more about this technology.
How to set the page size to Envelope size with
Landscape orientation?
I create a PDF document using iTextSharp and this code:
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 100f, 0f);
I googled but I couldn’t find the Envelope size. How do I set the page size as Envelope with
Landscape orientation?
Posted on StackOverflow on Sep 17, 2014 ¹⁸ by King_Fisher¹⁹
You are creating an A4 document in landscape format with this line:
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 100f, 0f);
If you want to create a document in envelope format, you shouldn’t create an A4 page, instead you
should do this:
Document pdfDoc = new Document(envelope, 10f, 10f, 100f, 0f);
In this line, envelope is an object of type Rectangle.
There is no such thing as the envelope size. There are different envelope sizes to choose from. Take
a look at the envelope size chart²⁰.
For instance, if you want to create a page with the size of a 6-1/4 Commercial Envelope²¹, then you
need to create a rectangle that measures 6 by 3.5 inch. The measurement system in PDF doesn’t use
inches, but user units. By default, 1 user unit = 1 point, and 1 inch = 72 points.
Hence you’d define the envelope variable like this:
¹⁸http://stackoverflow.com/questions/25886909/how-do-i-set-page-size-as-envelope-landscape-in-itextsharp
¹⁹http://stackoverflow.com/users/3141617/king-fisher
²⁰http://www.paper-papers.com/envelope-size-chart.html
²¹http://www.paper-papers.com/6-14-Commercial-Envelopes-24lb-WHITE-WOVE-35-x-6.html
Getting started 10
Rectangle envelope = new Rectangle(432, 252);
Because:
6 inch x 72 points = 432 points (the width)
3.5 inch x 252 points = 252 points (the height)
If you want a different envelope type, you have to do the Math with the dimensions of that envelope
format.
Fonts
Simple fonts, composite fonts, embedded fonts, encoding, ttf files, special characters, right to left
writing systems,… This chapter is where all your questions about fonts belong.
How to use the font Verdana in PdfStamper?
I want to use Verdana as a font while stamping a PDF file with iText PDF. The original file
uses Verdana, which isn’t an option in the class Basefont.
Here is the function to create my font right now:
def standardStampFont() {
return BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false)
}
I’d like to change that to the Verdana Font, but simply exchanging the Part
BaseFont.HELVETICA with "Verdana" doesn’t work.
Posted on StackOverflow on Oct 16, 2014 ²² by Alain Sarti²³
iText can support the Standard Type 1 fonts, because iText ships with AFM file (Adobe Font Metrics
files). iText has no idea about the font metrics of other fonts (Verdana isn’t a Standard Type 1 font).
You need to provide the path to the Verdana font file.
BaseFont.createFont("c:/windows/fonts/verdana.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED)
Note that I change false to BaseFont.EMBEDDED because the same problem you have on your side,
will also occur on the side of the person who looks at your file: his PDF viewer can render Standard
Type 1 fonts, but may not be able to render other fonts such as Verdana.
Caveat: The hard coded path "c:/windows/fonts/verdana.ttf"works for me onmy local machine
because the font file can be found using that path on my local machine. This code won’t work on
the server where I host the iText site, though (which is a Linux server that doesn’t even have a
c:/windows/fonts directory). I am using this hard coded path by way of example. You should make
sure that the font is present and available when you deploy your application.
²²http://stackoverflow.com/questions/26404418/how-to-use-verdana-font-in-stamper-itext-pdf
²³http://stackoverflow.com/users/1817618/alain-sarti
Fonts 12
Why doesn’t FontFactory.GetFont() work for all fonts?
If I say:
var georgia = FontFactory.GetFont("Georgia Regular", 10f);
it doesn’t work. When I check the state of the variable georgia, it has its Family property
set to the value UNDEFINED and its FamilyName property set to Unknown.
It only works if I actually load and register the font file and then get it like so:
FontFactory.Register("C:\\Windows\\Fonts\\georgia.ttf", "Georgia");
var georgia = FontFactory.GetFont("Georgia", 20f);
Why is that?
Posted on StackOverflow on Jun 3, 2014 ²⁴ by Water Cooler v2²⁵
iText is written in Java, whichmeans it’s platform-independent. It ships with 14 AFM files containing
themetrics of the 14 Standard Type 1 fonts (4 flavors of Helvetica, 4 flavors of Times Roman, 4 flavors
of Courier, Symbol and ZapfDingbats).
As soon as you need other fonts, you need to register the font files by passing the path to the
font directory or the path to an actual font. The font directory on Linux is different from the
font directory on Windows (there is no “C:/Windows/fonts” on Linux). There’s also a method
registerDirectories() that looks at the operating system you’re currently using and that registers
all the ‘usual suspects’ (iText guesses the font path based on the OS). This method is expensive: it
registers all fonts it finds and this costs time and memory.
Once fonts are registered, you can ask the FontFactory for the registered names. This is shown in
the FontFactoryExample²⁶. You’ll notice the difference between the getRegisteredFonts()method
and the getRegisteredFamilies() method.
Additional note: the original question is about iTextSharp, written in C#. iTextSharp is ported from
Java and tries to stay as close as possible to the original version written in Java. Nevertheless, the
same rationale applies: starting up an application would be much slower if iTextSharp would have
to scan the fonts directory. In most applications, you only need a handful of fonts; registering all
fonts available in the Windows fonts directory would be overkill.
²⁴http://stackoverflow.com/questions/24007492/why-doesnt-fontfactory-getfontknown-font-name-floatsize-work
²⁵http://stackoverflow.com/users/303685/water-cooler-v2
²⁶http://itextpdf.com/examples/iia.php?id=212
Fonts 13
Why aren’t my fonts getting registered?
I have a program using iTextSharp that includes the code
FontFactory.RegisterDirectories();
foreach (string fontname in FontFactory.RegisteredFonts) {
Log.Info("**** Found registered font: " + fontname);
}
When I run it (using Mono on a CentOS box), the log shows only the core PostScript fonts:
• zapfdingbats
• times-roman
• times-italic
• helvetica-boldoblique
• courier-boldoblique
• helvetica-bold
• helvetica
• courier-oblique
• helvetica-oblique
• courier-bold
• times-bolditalic
• courier
• times-bold
• symbol
But I have 156 TTF files under my /usr/share/fonts directory tree (which is one of the
directoriesmentioned in the code for the RegisterDirectories function).Why aren’t these
being registered?
Posted on StackOverflow on Nov 29, 2013 ²⁷ by dan04²⁸
There are subtle differences between iText and iTextSharp.
In iText, registerDirectories() looks like this:
²⁷http://stackoverflow.com/questions/13635212/why-arent-my-fonts-getting-registered
²⁸http://stackoverflow.com/users/287586/dan04
Fonts 14
public int registerDirectories() {
int count = 0;
String windir = System.getenv("windir");
String fileseparator = System.getProperty("file.separator");
if (windir != null && fileseparator != null) {
count += registerDirectory(windir + fileseparator + "fonts");
}
count += registerDirectory("/usr/share/X11/fonts", true);
count += registerDirectory("/usr/X/lib/X11/fonts", true);
count += registerDirectory("/usr/openwin/lib/X11/fonts", true);
count += registerDirectory("/usr/share/fonts", true);
count += registerDirectory("/usr/X11R6/lib/X11/fonts", true);
count += registerDirectory("/Library/Fonts");
count += registerDirectory("/System/Library/Fonts");
return count;
}
In iTextSharp however, the method looks like this:
public virtual int RegisterDirectories() {
string dir = Path.Combine(
Path.GetDirectoryName(Environment.GetFolderPath(
Environment.SpecialFolder.System)), "Fonts");
return RegisterDirectory(dir);
}
Java is platform independent, so we have to look for the ‘usual suspects’. C# is Windows specific,
so we can depend on the environment to tell us where to find fonts. Your question tells us that Mono
doesn’t support this, so you’ll have to use FontFactory.RegisterDirectory("/usr/share/fonts");
Images
In this section, you’ll find the questions related to raster images, such as JPG, PNG, GIF, and so on.
Why aren’t images added sequentially?
I am working on a pdf report that contains topics and images (charts). The document is
formatted this way:
NR. TOPIC TITLE FOR TOPIC 1
CHART IMAGE for topic 1 (from bytearray)
NR. TOPIC TITLE FOR TOPIC 2
CHART IMAGE for topic 2 …
Let’s assume that I add this information in a loop, and that the loop runs 10 times. I expect
10 topic titles all directly followed by the image.
However, if the page end is reached and a new image should be added, I notice that the
image is moved to the next page and the next topic title is printed on the previous page.
So on paper we have:
page 1: topic 1
image topic 1
topic 2
image topic 2
topic 3
topic 4
page 2: image topic 3
image topic 4
topic 5
image topic 5
So the order of the elements on paper, is NOT the same as the order that I used to put the
element in the document via the ‘document.add() method. This is really strange. Anyone
has any idea?
Posted on StackOverflow on Mar 26, 2014 ²⁹ by wim boone³⁰
²⁹http://stackoverflow.com/questions/22664126/image-not-sequentialy-added-in-pdf-document-itextsharp-wrong-order-of-elements
³⁰http://stackoverflow.com/users/3149519/wim-boone
Images 16
If you have a PdfWriter instance (for instance writer), you need to force iText to use strict image
sequence like this:
writer.setStrictImageSequence(true);
Otherwise, iText will postpone adding images until there’s sufficient space on the page to add the
image.
How to get the image DPI in PDF?
I’m trying to get information about scanned images that are saved into PDF files through
iText (using Java). I can get width and height (either through Matrix, or through BufferedImage). The idea was to use the answer here to calculate the DPI, but I am a bit lost. Are
these values (width and height) in pixels or points? Is there any other way to achieve this?
There are a lot of answers on how to scale and save an image to a PDF file, but I didn’t find
any on how to read the width/height/scale of an image and be confident about the result.
Posted on StackOverflow on Aug 28, 2014 ³¹ by Finik³²
Let’s split this problem into two separate problems. To calculate the DPI, you need two sets of values:
a number of pixels and a distance in inch.
1. Number of pixels: you obtain the image and the image consists of pixels. You can retrieve the
width and height of the image in pixels from the image. Let’s say these values are wPx and
wPx.
2. Distance in inch: you obtain the matrix which gives you values expressed in points. As 72
points equal 1 inch, you need to divide these values by 72. Let’s say these values are wInch
and hInch.
Now you can calculate the DPI in the x direction like this: wPx / wInch and the DPI in the y direction
like this: hPx / hInch.
³¹http://stackoverflow.com/questions/25550000/getting-image-dpi-in-pdf-files-using-itext
³²http://stackoverflow.com/users/1300529/finik
Images 17
How to preserve high resolution images in PDF?
I’m trying to put high quality images into PDF (one per page). But if I set page size to a4, I
have to resize my pictures, because they’re too large. Then they loose their quality. Is there
any way to put big image to a4 page without loosing quality?
I’m using iTextSharp library, firstly I’m creating the document
document = new Document(PageSize.A4, 0, 0, 0, 0);
FileStream output = new FileStream(pdfPath + "document.pdf", FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(document, output);
document.Open();
then I’m adding each picture
document.Add(iTextSharp.text.Image.GetInstance(toSaveImage, System.Drawing.Imaging.ImageFormat.Tiff));
and closing the document
document.Close();
Posted on StackOverflow on jun 6, 2013 ³³ by Com Piler³⁴
First let me clear a couple of misunderstandings:
• a PDF document as such doesn’t have a resolution. There is no such thing as DPI in PDF. The
resolution only comes into play when a PDF is rendered (to the screen, to paper,…) and that’s
why there may be a DPI in a PDF viewer (but that’s something completely different).
• when you scale an Image object in iTextSharp, you don’t lose any information: the number
of pixels remains the same. Whereas PDF doesn’t have a resolution, the images inside a PDF
do. When you the image scale down (that is: you put the same number of pixels on a smaller
canvas), the resolution increases; when you scale up, the resolution decreases.
Now for your question: you’re not obliged to create A4 pages:
³³http://stackoverflow.com/questions/16970106/c-sharp-high-resolution-images-in-pdf
³⁴http://stackoverflow.com/users/2411220/com-piler
Images 18
Image img =
iTextSharp.text.Image.GetInstance(toSaveImage,
System.Drawing.Imaging.ImageFormat.Tiff);
Rectangle pagesize = new Rectangle(img.ScaledWidth, img.ScaledHeight);
Document document = new Document(pagesize);
img.SetAbsolutePosition(0, 0);
document.Add(img);
I created the Document based on the scaled dimensions of the Image. Don’t let the method names
mislead you: ScaledWidth and ScaledHeight are the safest methods to use when getting the
dimensions of an Image. Not only do they include any scaling operations, you may have done on
the image, the also take into account the space needed for the image after rotating it.
Observe that I’ve set the absolute position to the lower-left corner. That’s safer than setting the page
margins to 0.
If you don’t want to change the page size, then you have to use the ScaleToFit() method:
Image img =
iTextSharp.text.Image.GetInstance(toSaveImage,
System.Drawing.Imaging.ImageFormat.Tiff);
img.ScaleToFit(PageSize.A4);
Scale to fit will keep the aspect ratio of the image. If the aspect ratio of the image is different from
the aspect ratio of the page, the page will have a margin.
Absolute positioning of text
In this section, we’ll discuss problems that can occur when adding text at absolute positions.
How to write a Zapfdingbats character at a specific
location on a page?
I want to put a check mark using Zapfdingbats on a specific location in my PDF document.
What I achieved so far is this: I can show the checkmark but it’s on the side of the document
and not on the specific X, Y coordinate that I want it to be.
Posted on StackOverflow on May 4, 2013 ³⁵ by δοῦλος Ἰησοῦς Χριστός³⁶
Let’s start with a Font object that knows how to draw a Zapfdingbats character:
Font font = new Font(Font.FontFamily.ZAPFDINGBATS, 12);
Once you have a Font object, you can create a Phrase:
Phrase phrase = new Phrase(zapfstring, font);
Where zapfstring is a string containing any Zapfdingbats character you want.
To add this Phrase at an absolute position, you can use the ShowTextAligned() method and
PdfWriter’s direct content :
PdfContentByte canvas = writer.DirectContent;
ColumnText.ShowTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 500, 0);
Where 200 and 500 are an X and Y coordinate and 0 is an angle expressed in degrees. Instead of
ALIGN_CENTER, you can also choose ALIGN_RIGHT or ALIGN_LEFT.
³⁵http://stackoverflow.com/questions/16370428/how-to-write-in-a-specific-location-the-zapfdingbatslist-in-a-pdf-document-using
³⁶http://stackoverflow.com/users/2170392/
Absolute positioning of text 20
How to reduce redundant code when adding content
at absolute positions?
This is part of a vb.net app that uses the itextsharp library:
Dim cb As PdfContentByte = writer.DirectContent
cb.BeginText()
cb.SetFontAndSize(Californian, 36)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
"CERTIFICATE OF COMPLETION", 396, 397.91, 0)
cb.SetFontAndSize(Bold_Times, 22)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, name, 396, 322.35, 0)
cb.SetFontAndSize(Bold_Times, 16)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
_hours + " Hours", 297.05, 285.44, 0)
cb.SetFontAndSize(Bold_Times, 16)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
_dates, 494.95, 285.44, 0)
cb.SetFontAndSize(Bold_Times, 16)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _class1, 396, 250.34, 0)
If Not String.IsNullOrWhiteSpace(_class2) Then
cb.SetFontAndSize(Bold_Times, 16)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _class2, 396, 235.34, 0)
End If
cb.SetFontAndSize(Copper, 16)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
_conf_num + _prefix + " Annual Conference " + _dates, 396, 193.89, 0)
cb.SetFontAndSize(Bold_Times, 13)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Some Name", 396, 175.69, 0)
cb.SetFontAndSize(Bold_Times, 10)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
"Some Company Manager", 396, 162.64, 0)
cb.EndText()
Plenty of lines in this snippet look awfully redundant and in my opinion, this can’t be the
cleanest way to do things. Unfortunately, I can’t figure out how to create a separate function
to which I can simply pass some parameters, such as string, x_Cord, y_Cord, tilt. Such a
function would then perform the necessary operations on the PdfContentByte.
Posted on StackOverflow on Nov 23, 2012 ³⁷ by Skindeep2366³⁸
³⁷http://stackoverflow.com/questions/13523099/separating-redundant-code-from-pdf-generator-function
³⁸http://stackoverflow.com/users/969487/skindeep2366
Absolute positioning of text 21
You’re adding content “the hard way”. If I were you, I’d write a separate class/factory/method that
creates either a Phrase or a Paragraph with the content. For instance:
protected Font f1 = new Font(Californian, 36);
protected Font f2 = new Font(Bold_times, 16);
public Phrase getCustomPhrase(String name, int hours, ...) {
Phrase p = new Phrase();
p.add(new Chunk("...", f1));
p.add(new Chunk(name, f2);
...
return p;
}
Then I would use ColumnText to add the Phrase or Paragraph at the correct position. In the case
of a Phrase, I’d use the ColumnText.showTextAligned() method. In the case of Paragraph, I’d use
this construction:
ColumnText ct = new ColumnText(writer.DirectContent);
ct.setSimpleColumn(rectangle);
ct.addElement(getCustomParagraph(name, hours, ...));
ct.go();
The former (using a Phrase) is best if you only need towrite one line that doesn’t need to bewrapped,
oriented in any direction you want.
The latter (using a Paragraph in composite mode) is best if you want to add text inside a specific
rectangle (defined by the coordinates of the lower-left corner and the upper-right corner).
The approach you’ve taken works, but… it involves writing PDF syntax almost “manually”. That’s
more difficult and therefore more error-prone. You already discovered that, otherwise you wouldn’t
ask the question ;-)
Absolute positioning of text 22
Why does ColumnText ignore the horizontal
alignment?
I’m trying to get some rows of text on the left side and some on the right side. For some
reason iText seems to ignore the alignment entirely. For example:
// create 200x100 column
ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(0, 0, 200, 100);
ct.AddElement(new Paragraph("entry1"));
ct.AddElement(new Paragraph("entry2"));
ct.AddElement(new Paragraph("entry3"));
ret = ct.Go();
ct.SetSimpleColumn(0, 0, 200, 100);
ct.Alignment = Element.ALIGN_RIGHT;
ct.AddElement(new Paragraph("entry4"));
ct.AddElement(new Paragraph("entry5"));
ct.AddElement(new Paragraph("entry6"));
ret = ct.Go();
I’ve set the alignment of the 2nd column to Element.ALIGN_RIGHT but the text appears
printed on top of column one, rendering unreadable text. Like the alignment was still set
to left
Posted on StackOverflow on Aug 9, 2013 ³⁹ by Chuck⁴⁰
To understand what happens, you should learn about the concepts “text mode” and “composite
mode”.
If you work in text mode, you can define the alignment at the level of the ColumnText object. In
other words ct.Alignment = Element.ALIGN_RIGHT; will work in text mode.
If you work in composite mode, the alignment at the column level will be ignored in favor of the
alignment of the elements added to the column. In your case, iText will ignore the ALIGN_RIGHT in
favor of the alignment of the Paragraph objects added to the column. Looking at your code, I see
that you didn’t define an alignment for the paragraphs, so the default alignment ALIGN_LEFT is used.
How do you know if you’re working in text mode or in composite mode?
By default, ColumnText uses text mode but it switches to composite mode (removing all previously
added text) the moment you invoke the AddElement() method.
³⁹http://stackoverflow.com/questions/18142623/itext-columntext-ignores-alignment
⁴⁰http://stackoverflow.com/users/1280511/chuck
Absolute positioning of text 23
The concepts text mode and composite mode also applies to PdfPCell.
Tables
The PdfPTable class is one of the most popular classes in the context of document creation. Let’s
take a look at some questions and answers regarding tables, rows and cells.
How to right-align text in a PdfPCell?
I have a C# application that generates a PDF invoice. In this invoice is a table of items and
prices. This is generated using a PdfPTable and PdfPCells.
I want to be able to right-align the price column but I cannot seem to be able to - the text
always comes out left-aligned in the cell.
Here is my code for creating the table:
PdfPTable table = new PdfPTable(2);
table.TotalWidth = invoice.PageSize.Width;
float[] widths = { invoice.PageSize.Width - 70f, 70f };
table.SetWidths(widths);
table.AddCell(new Phrase("Item Name", tableHeadFont));
table.AddCell(new Phrase("Price", tableHeadFont));
SqlCommand cmdItems = new SqlCommand("SELECT...", con);
using (SqlDataReader rdrItems = cmdItems.ExecuteReader())
{
while (rdrItems.Read())
{
table.AddCell(new Phrase(rdrItems["itemName"].ToString(), tableFont));
double price = Convert.ToDouble(rdrItems["price"]);
PdfPCell pcell = new PdfPCell();
pcell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
pcell.AddElement(new Phrase(price.ToString("0.00"), tableFont));
table.AddCell(pcell);
}
}
Can anyone help?
Posted on StackOverflow on Nov 28, 2012 ⁴¹ by colincameron⁴²
⁴¹http://stackoverflow.com/questions/13607970/right-aligning-text-in-pdfpcell
⁴²http://stackoverflow.com/users/970789/colincameron
Tables 25
You’re mixing text mode and composite mode.
In text mode, you create the PdfPCell with a Phrase as the parameter of the constructor, and you
define the alignment at the level of the cell. However, you’re working in composite mode. This
mode is triggered as soon as you use the addElement()method. In composite mode, the alignment
defined at the level of the cell is ignored (which explains your problem). Instead, the alignment of
the separate elements is used.
How to solve your problem?
Either work in text mode by adding your Phrase to the cell in a different way. Or work in composite
mode and use a Paragraph for which you define the alignment.
The advantage of composite mode over text mode is that different paragraphs in the same cell
can have different alignments, whereas you can only have one alignment in text mode. Another
advantage is that you can add more than just text: you can also add images, lists, tables,… An
advantage of text mode is speed: it takes less processing time to deal with the content of a cell.
How to use multiple fonts in a single cell?
I’m making a windows form for a friend that delivers packages. So I want to transfer his
current paper form, into a .pdf with the library iTextSharp.
What I need:
I want the table to have a little headline, “Company name” for example, the text should be
a little smaller than the text input from the windows form. Currently I’m using cells and
was wondering if I can use 2 different font sizes within the same cell?
What I have:
table.AddCell("Static headline" + Chunk.NEWLINE + richTextBox1.Text);
What I “want”:
var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 9);
var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);
table.AddCell("Static headline", boldFont + Chunk.NEWLINE + richTextBox1.Text, normalFont);
Posted on StackOverflow on Feb 13, 2014 ⁴³ by Frederik Kiel⁴⁴
⁴³http://stackoverflow.com/questions/21750597/c-sharp-itextsharp-multi-fonts-in-a-single-cell
⁴⁴http://stackoverflow.com/users/3300515/frederik-kiel
Tables 26
You’re passing a String and a Font to the AddCell() method. That’s not going to work. You need
the AddCell() method that takes a Phrase object or a PdfPCell object as parameter.
A Phrase is an object that consists of different Chunks, and the different Chunks can have different
font sizes. For instance:
Phrase phrase = new Phrase();
phrase.Add(
new Chunk("Some BOLD text", new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD))
);
phrase.Add(new Chunk(", some normal text", new Font()));
table.AddCell(phrase);
A PdfPCell is an object to which you can add different objects, such as Phrases, Paragraphs,
Images,…
PdfPCell cell = new PdfPCell();
cell.AddElement(new Paragraph("Hello"));
cell.AddElement(list);
cell.AddElement(image);
In this snippet list is of type List and image is of type Image.
The first snippet uses text mode; the second snippet uses composite mode. Cells behave very
differently depending on the mode you use.
Tables 27
How to introduce a rowspan?
I am adding a table to a PDF file. I have 3 rows and 3 columns. I want the first column
to appear only once as a single cell for all the rows. The result should be like where
it says Deloitte in the column of company as shown in the image:
Table showing desired result
Posted on StackOverflow on Apr 5, 2014 ⁴⁵ by sindhu jampani⁴⁶
The MyFirstTable⁴⁷ example from my book does exactly what you need. Ported to C#, it looks like
this:
PdfPTable table = new PdfPTable(3);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.Rowspan = 2;
table.AddCell(cell);
// we add the four remaining cells with addCell()
table.AddCell("row 1; cell 1");
table.AddCell("row 1; cell 2");
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");
⁴⁵http://stackoverflow.com/questions/22878410/constant-column-for-multiple-rows
⁴⁶http://stackoverflow.com/users/3191619/sindhu-jampani
⁴⁷http://itextpdf.com/examples/iia.php?id=75
Tables 28
You can look at the resulting PDF here⁴⁸. In your case you’d need
cell.Rowspan = 6;
For the cell with value Deloitte.
How to change width of single column of table?
hello I have created table in a PDF file using iText. The heading of my table columns are
“Medicine Name”, “Doses”, and “time: This is what my columns look like:
|Medicin|Doses|time|
| e name| | |
As you can see the word “Medicine” is split into “Medicin” and “e”. I want to avoid this by
changing the width of the first column, but I don’t know how to do that.
Posted on StackOverflow on Jun 10, 2014 ⁴⁹ by DCoder⁵⁰
The ColumnWidths⁵¹ example demonstrates different ways of changing the width of a column. This
is one specific way:
PdfPTable table = new PdfPTable(3);
table.setWidths(new int[]{2, 1, 1});
Now the width of the first column is double the size of the second and third column.
⁴⁸http://examples.itextpdf.com/results/part1/chapter04/first_table.pdf
⁴⁹http://stackoverflow.com/questions/24141791/how-to-change-width-of-single-coloumn-of-table-itext-android
⁵⁰http://stackoverflow.com/users/2843856/dcoder
⁵¹http://itextpdf.com/examples/iia.php?id=76
Tables 29
What is the PdfPTable.DefaultCell property used
for?
What is the DefaultCell property used for? The Java documentation for
PdfPTable.getDefaultCell() reads:
Gets the default PdfPCell that will be used as reference for all the addCell
methods except addCell(PdfPCell).
I don’t understand this.
Posted on StackOverflow on Jun 3, 2014 ⁵² by Water Cooler v2⁵³
When creating a PdfPTable, you add cells.
One way is to create a PdfPCell object and to add that cell with the addCell() method.
Another way is to use a short-cut: you don’t create a PdfPCell, but you add a String or a Phrase
to the table with the addCell()method. In this case, a PdfPCell is created internally using default
properties. You can change the default properties by changing the properties of the default cell.
The default cell is obtained using the getDefaultCell() method.
This is what the Javadoc information is about: this default PdfPCell will be used as reference
for all the addCell() methods except addCell(PdfPCell). (Because when adding a PdfPCell, the
properties of that PdfPCell will be used, not the properties of the default cell.)
How to draw a borderless table in iTextSharp?
It appears as though the PDfPCell class does have a border property on it but not the
PdfPTable class.
Is there some property on the PdfPTable class to set the borders of all its contained cells in
one statement?
Posted on StackOverflow on Jun 3, 2014 ⁵⁴ by Water Cooler v2⁵⁵
Borders are defined at the level of the cell, not at the level of the table. Hence: if you want to remove
the borders of the table, you need to remove the borders of each cell.
⁵²http://stackoverflow.com/questions/24006618/what-is-the-pdfptable-defaultcell-property-used-for
⁵³http://stackoverflow.com/users/303685/water-cooler-v2
⁵⁴http://stackoverflow.com/questions/24006547/draw-a-borderless-table-in-itextsharp
⁵⁵http://stackoverflow.com/users/303685/water-cooler-v2
Tables 30
By default, each cell has a border. You can change this default behavior by changing the border of
each cell. For instance: if you create PdfPCell objects, you use:
cell.setBorder(Rectangle.NO_BORDER);
In case the cells are created internally, you need to change that property at the level of the default
cell.
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
For special borders, for instance borders with rounded corners or a single border for the whole table,
or double borders, you can use either cell events or table events, or a combination of both.
Why doesn’t getDefaultCell()
.setBorder(PdfPCell.NO_BORDER) have any effect?
I’m new with iText and I’m trying to build a table. For some reason
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER) has no effect: my table
has still borders.
Here is my code:
PdfPTable table = new PdfPTable(new float[] { 1, 1, 1, 1, 1 });
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
Font tfont = new Font(Font.FontFamily.UNDEFINED, 10, Font.BOLD);
table.setWidthPercentage(100);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Menge", tfont));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Beschreibung", tfont));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Einzelpreis", tfont));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Gesamtpreis", tfont));
table.addCell(cell);
cell = new PdfPCell(new Phrase("MwSt", tfont));
table.addCell(cell);
document.add(table);
Do you have any idea what I am doing wrong?
Posted on StackOverflow on Nov 30, 2014 ⁵⁶ by hiasl⁵⁷
⁵⁶http://stackoverflow.com/questions/27212695/itext-5-getdefaultcell-setborderpdfpcell-no-border-has-no-effect
⁵⁷http://stackoverflow.com/users/4308508/hiasl
Tables 31
You are mixing two different concepts.
Concept 1: you define every PdfPCell manually, for instance:
PdfPCell cell = new PdfPCell(new Phrase("Menge", tfont));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
In this case, you define every aspect, every property of the cell on the cell itself.
Concept 2: you allow iText to create the PdfPCell implicitly, for instance:
table.addCell("Adding a String");
table.addCell(new Phrase("Adding a phrase"));
In this case, you can define properties at the level of the default cell. These properties will be used
internally when iText creates a PdfPCell in your place.
Conclusion: either you define the border for all the PdfPCell instances separately, or you let iText
create the PdfPCell instances in which case you can define the border at the level of the default cell.
If you choose the second option, you can adapt your code like this:
PdfPTable table = new PdfPTable(new float[] { 1, 1, 1, 1, 1 });
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
Font tfont = new Font(Font.FontFamily.UNDEFINED, 10, Font.BOLD);
table.setWidthPercentage(100);
table.addCell(new Phrase("Menge", tfont));
table.addCell(new Phrase("Beschreibung", tfont));
table.addCell(new Phrase("Einzelpreis", tfont));
table.addCell(new Phrase("Gesamtpreis", tfont));
table.addCell(new Phrase("MwSt", tfont));
document.add(table);
This decision was made by design, based on experience: it offers the most flexible to work with cells
and properties.
Table events
We continue with some questions about tables of which the answer involves table or cell events.
How to use a dotted line as a cell border?
I am trying to create a table with cells that have a dotted line for a border. How can I do
this?
Posted on StackOverflow on Nov 21, 2013 ⁵⁸ with user1913695⁵⁹
I’ve made an example that solves your problem: DottedLineCell⁶⁰; The resulting PDF is a document
with two tables: dotted_line_cell.pdf⁶¹
For the first table, we use a table event:
class DottedCells implements PdfPTableEvent {
@Override
public void tableLayout(PdfPTable table, float[][] widths,
float[] heights, int headerRows, int rowStart,
PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.setLineDash(3f, 3f);
float llx = widths[0][0];
float urx = widths[0][widths[0].length -1];
for (int i = 0; i < heights.length; i++) {
canvas.moveTo(llx, heights[i]);
canvas.lineTo(urx, heights[i]);
}
for (int i = 0; i < widths.length; i++) {
for (int j = 0; j < widths[i].length; j++) {
canvas.moveTo(widths[i][j], heights[i]);
canvas.lineTo(widths[i][j], heights[i+1]);
}
⁵⁸http://stackoverflow.com/questions/20117321/dotted-line-for-cell-border
⁵⁹http://stackoverflow.com/users/1913695/user1913695
⁶⁰http://itextpdf.com/sandbox/tables/DottedLineCell
⁶¹http://itextpdf.com/sites/default/files/dotted_line_cell.pdf
Table events 33
}
canvas.stroke();
}
}
This is the most elegant way to draw the cell borders, as it uses only one stroke() operator for all
the lines. Unfortunately, this solution isn’t an option if you have tables with rowspans.
The second table uses a cell event:
class DottedCell implements PdfPCellEvent {
@Override
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.setLineDash(3f, 3f);
canvas.rectangle(position.getLeft(), position.getBottom(),
position.getWidth(), position.getHeight());
canvas.stroke();
}
}
With a cell event, a border is drawn around every cell. This means you’ll have multiple stroke()
operators and overlapping lines. However: this solution always works, also when the table has cells
with a rowspan greater than one.
How to create a table with rounded corners?
I have to create a table having rounded corners, something like it:
Cell with rounded border
Can I do it with iTextSharp?
Posted on StackOverflow on May 14, 2014 ⁶² by AndreaNobili⁶³
⁶²http://stackoverflow.com/questions/23650957/how-to-create-a-rounded-corner-table-using-itext-itextsharp
⁶³http://stackoverflow.com/users/1833945/andreanobili
Table events 34
This is done using cell events.
Make sure that you don’t add any “automated” borders to the cell, but draw the borders yourself in
a cell event:
table.DefaultCell.Border = PdfPCell.NO_BORDER;
table.DefaultCell.CellEvent = new RoundedBorder();
The RoundedBorder class would then look like this:
class RoundedBorder : IPdfPCellEvent {
public void CellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas){
PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
cb.RoundRectangle(
rect.Left + 1.5f,
rect.Bottom + 1.5f,
rect.Width - 3,
rect.Height - 3, 4
);
cb.Stroke();
}
}
You can of course fine-tune the values 1.5, 3 and 4 to get different effects.
Page events
When creating a document from scratch, iText can trigger a series of events for instance when a
new page starts or ends, when a paragraph is added, and so on…
How to add a rectangle to every page of a document?
I’m using iText to create a PDF document. Right now I am trying to get a rectangle on
every single page of the document but I’m not sure how to do this. I tried adding this at
the end of my code:
PdfContentByte cb = writer.getDirectContent();
for (int pgCnt = 1; pgCnt <= writer.getPageNumber(); pgCnt++) {
cb.saveState();
cb.setColorStroke(new CMYKColor(1f, 0f, 0f, 0f));
cb.setColorFill(new CMYKColor(1f, 0f, 0f, 0f));
cb.rectangle(20,10,10,820);
cb.fill();
cb.restoreState();
}
but this only adds the rectangle on the last page and it kind of make sense because I’m not
using the pgCnt anywhere. How can I specify that I want the rectangle on page number
pgCnt, so I can add the rectangle on every page?
Posted on StackOverflow on Mar 19, 2013 ⁶⁴ by Carla Stabille⁶⁵
Please take a look at the entries for the keyword Page events⁶⁶ on the official iText site. You need to
extend the PdfPageEventHelper⁶⁷ class and add your code to the onEndPage() method.
⁶⁴http://stackoverflow.com/questions/16638406/how-can-i-add-rectangle-on-every-page-of-a-document-using-itext
⁶⁵http://stackoverflow.com/users/1883606/carla-stabile
⁶⁶http://itextpdf.com/themes/keyword.php?id=204
⁶⁷http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPageEventHelper.html
Page events 36
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.setColorStroke(new CMYKColor(1f, 0f, 0f, 0f));
cb.setColorFill(new CMYKColor(1f, 0f, 0f, 0f));
cb.rectangle(20,10,10,820);
cb.fill();
cb.restoreState();
}
Create an instance of your custom page event class, and declare it to the writer before opening the
document:
writer.setPageEvent(myPageEventInstance);
Now your rectangle will be drawn on every page, on top of the existing content. If you want the
rectangle under the existing content: replace getDirectContent()with getDirectContentUnder().
How can I add an image to all pages of my PDF?
I have been trying to add an image to all pages using iTextSharp. The image needs
to be OVER all content of every page. I have used the following code below all the
otherdoc.add()
Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 30, 1);
PdfWriter writer = PdfWriter.GetInstance(doc,
new FileStream(Server.MapPath("~/pdf/" + fname), FileMode.Create));
doc.Open();
Image image = Image.GetInstance(Server.MapPath("~/images/draft.png"));
image.SetAbsolutePosition(12, 300);
writer.DirectContent.AddImage(image, false);
doc.Close();
The above code only inserts an image in the last page. Is there any way to insert the image
in the same way in all pages?
Posted on StackOverflow on Feb 20, 2014 ⁶⁸ by Neville Nazerane⁶⁹
It’s normal that the image is only added once; after all: you’re adding it only once.
You should create a document in 5 steps and add an event in step 2:
⁶⁸http://stackoverflow.com/questions/21908651/add-an-image-in-all-pages-of-pdf
⁶⁹http://stackoverflow.com/users/991609/neville-nazerane
Page events 37
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.GetInstance(document, stream);
MyEvent event = new MyEvent();
writer.PageEvent = event;
// step 3
document.Open();
// step 4
// Add whatever content you want to add
// step 5
document.Close();
You have to write the MyEvent class yourself:
protected class MyEvent : PdfPageEventHelper {
Image image;
public override void OnOpenDocument(PdfWriter writer, Document document) {
image = Image.GetInstance(Server.MapPath("~/images/draft.png"));
image.SetAbsolutePosition(12, 300);
}
public override void OnEndPage(PdfWriter writer, Document document) {
writer.DirectContent.AddImage(image);
}
}
The OnEndPage() in class MyEvent will be triggered every time the PdfWriter has finished a page.
Hence the image will be added on every page.
Caveat: it is important to create the image object outside the OnEndPage() method, otherwise the
image bytes risk being added as many times as there are pages in your PDF (leading to a bloated
PDF).
Page events 38
How to set a fixed background image for all my pages?
On Button Click, I generate 4 pages on my PDF, i added this image to provide a background
image
string imageFilePath = parent + "/Images/bg_image.jpg";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(1700, 1000);
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
jpg.SetAbsolutePosition(0, 0);
document.Add(jpg);
It works only with 1 page, but when I generate a PDF that contains many records and have
several pages, the bg image is only at the last page. I want to apply the background image
to all of the pages.
Posted on StackOverflow on Nov 1, 2014 ⁷⁰ by dandy⁷¹
It is normal that the background is added only once, because you’re adding it only once.
If you want to add content to every page, you should not do thatmanually because you don’t know
when a new page will be created by iText. Instead you should use a page event.
The idea is to create an implementation of the PdfPageEvent interface, for instance by extending
the PdfPageEventHelper class and overriding the OnEndPage() method:
class TemplateHelper : PdfPageEventHelper {
private Stationery instance;
public TemplateHelper() { }
public TemplateHelper(Stationery instance) {
this.instance = instance;
}
/**
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public override void OnEndPage(PdfWriter writer, Document document) {
writer.DirectContentUnder.AddTemplate(instance.page, 0, 0);
}
}
⁷⁰http://stackoverflow.com/questions/26688288/set-a-fix-background-image-for-all-my-pages-in-pdf-itext-asp-c-sharp
⁷¹http://stackoverflow.com/users/4131886/dandy
Page events 39
In this case, we add a PdfTemplate, but it is very easy to add an Image replacing the Stationery
instance with an Image instance and replacing the AddTemplate() method with the AddImage()
method.
Once you have an instance of your custom page event, you need to declare it to the PdfWriter
instance:
writer.PageEvent = new TemplateHelper(this);
From that moment on, your OnEndPage() method will be executed each time a page is finalized.
Warning: as documented you shall not use the OnStartPage() method to add content in a page
event!
If we adapt the above example to your requirement, the final result would look more or less like
this:
class ImageBackgroundHelper : PdfPageEventHelper {
private Image img;
public ImageBackgroundHelper(Image img) {
this.img = img;
}
/**
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public override void OnEndPage(PdfWriter writer, Document document) {
writer.DirectContentUnder.AddImage(img);
}
}
Now you can use this event like this:
string imageFilePath = parent + "/Images/bg_image.jpg";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(1700, 1000);
jpg.SetAbsolutePosition(0, 0);
writer.PageEvent = new ImageBackgroundHelper(jpg);
Note that 1700 and 1000 seems quite big. Are you sure those are the dimensions of your page?
Parsing XML and XHTML
There are a lot of questions about HTMLWorker on StackOverflow. Many of these questions remain
unanswered as HTMLWorker has been abandoned in favor of XML Worker. HTMLWorker was initially
meant as a parser for a small selection of HTML tags. People started using it as if it were a full-blown
HTML to PDF converter and then complained because HTMLWorker doesn’t support CSS parsing. The
HTMLWorker code grew organically up until a point where it was no longer maintainable.
We started another project, called XML Worker. It can be used to convert XHTML to PDF. It’s not
an URL to PDF converter in the sense that it won’t “print your web site to PDF”. In HTML, you can
encounter content at the end of the file that needs to be added at the start of the document. When
this happens, one would expect that the start of the document is the first page. That isn’t possible
with iText as iText flushes finished pages to the OutputStream as soon as possible and there is no
way to return to a previous page to add the extra content.
XML Worker is meant to create simple reports using an easy language such as HTML (and some
CSS). It won’t resolve ASP pages, nor execute JavaScript. It will only deal with finished XHTML.
Why is it so difficult to convert XML to PDF?
Could anybody explain to me why is it so complicated to create a pdf file from xml sheet?
Acrobat can create XML File but when I want to do this other way round it suddenly gets
complicated. I would like to find some simple application which would allow me to create
a pdf file out of xml. Is it possible?
Posted on StackOverflow on Jun 13, 2013 ⁷² by DDEX⁷³
XML is a bunch of ingredients, PDF is the finished meal.
He or she who knows how to cook can create a wide variety of meals using the same ingredients.
With a potato, he can create soup, mashed potatoes, crisps, french fries,… There’s an almost endless
list of possibilities.
He or she who can’t cook, will stare at the potato and wonder: How on earth can I turn this ugly
vegetable into a nice croquette?
The answer is: you need a recipe. That recipe could be an XSL:FO file, the XHTML specification, a
DocBook implementation, an XFA template,…Without that recipe, you’ll never be able to turn your
XML into PDF.
⁷²http://stackoverflow.com/questions/17081907/why-is-it-so-difficult-to-convert-xml-to-pdf
⁷³http://stackoverflow.com/users/1934834/ddex
Parsing XML and XHTML 41
How to add external CSS while generating PDF?
Currently i am using following code to generate PDF in a JSP file:
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;filename=reports.pdf");
Document document = new Document();
document.setPageSize(PageSize.A1);
PdfWriter writer = null;
writer = PdfWriter.getInstance(document, response.getOutputStream());
document.open();
ByteArrayInputStream bis
= new ByteArrayInputStream(htmlSource.toString().getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, bis);
document.close();
With this code, I’m able to generate PDF, but I would like to add a CSS file while generating
PDF.
Posted on StackOverflow on Jul 16, 2014 ⁷⁴ by Yella Goud⁷⁵
Please take a look at the ParseHtmlTable1⁷⁶ example. In this example, we have HTML stored in a
StringBuilder object and some CSS stored in a String. In my example, I convert the sb object and
the CSS object to an InputStream. If you have files with the HTML and the CSS, you could easily
use a FileInputStream.
Once you have an InputStream for the HTML and the CSS, you can use this code:
// CSS
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
cssResolver.addCss(cssFile);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
⁷⁴http://stackoverflow.com/questions/24777549/how-to-add-external-css-while-generating-pdf
⁷⁵http://stackoverflow.com/users/2436481/yella-goud
⁷⁶http://itextpdf.com/sandbox/xmlworker/ParseHtmlTable1
Parsing XML and XHTML 42
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.parse(new ByteArrayInputStream(sb.toString().getBytes()));
Or, if you don’t like all that code:
ByteArrayInputStream bis =
new ByteArrayInputStream(htmlSource.toString().getBytes());
ByteArrayInputStream cis =
new ByteArrayInputStream(cssSource.toString().getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, bis, cis);
How to do HTML to XML conversion to generate closed
tags?
When I try converting html to pdf using iText and XML Worker, I’m asked to give the
closing tag for
and
tags. It works if I do this manually, but I don’t want to add
each closing tag manually. How can I do this in an automated way?
Posted on StackOverflow on Oct 30, 2014 ⁷⁷ by Kannu Verma⁷⁸
You are experiencing this problem because you are feeding HTML to iText’s XML Worker. XML
Worker requires XML, so you need to convert your HTML into XHTML.
There is an example on how to do this on the official iText site: D00_XHTML⁷⁹
public static void tidyUp(String path) throws IOException {
File html = new File(path);
byte[] xhtml = Jsoup.parse(html, "US-ASCII").html().getBytes();
File dir = new File("results/xml");
dir.mkdirs();
FileOutputStream fos = new FileOutputStream(new File(dir, html.getName()));
fos.write(xhtml);
fos.close();
}
⁷⁷http://stackoverflow.com/questions/26652029/how-to-do-xml-to-html-conversion-to-generate-closed-tags
⁷⁸http://stackoverflow.com/users/4197576/kannu-verma
⁷⁹http://itextpdf.com/sandbox/xmlworker/D00_XHTML
Parsing XML and XHTML 43
In this example, we get a path to an ordinary HTML file (similar to what you have). We then use
the Jsoup⁸⁰ library to parse the HTML into an XHTML byte array. In this example, we use that byte
array to write an XHTML file to disk. You can use the byte array directly as input for XML Worker.
⁸⁰http://jsoup.org/
Inspect a PDF with iText
iText can tell you more about a PDF. What is the size of a page? Which measurement unit is used.
All of these questions can be answered with a simple example using PdfReader.
Why do I get an “InvalidPdfException: PDF header
signature not found”?
I have some code that reads pdf files. The code fails at the line :
iTextSharp.text.pdf.PRTokeniser.CheckPdfHeader() at
iTextSharp.text.pdf.PdfReader.ReadPdf()
I know from other entries that this issue is coming from some invalid formatting in the
PDF. However I’m not in a position to tell my users to redo their PDFs. Is there some other
way around this issue, that can allow reading of the pdf despite this problem?
Posted on StackOverflow on Sep 10, 2012 ⁸¹ by David Choi⁸²
If a file doesn’t start with %PDF- then there’s nothing to fix: the file isn’t a PDF file.
However, theremay be another problem: maybe you’re trying to access a file that has zero length due
to some problem while creating the InputStream. Another context in which I’ve seen this happen,
is a PDF loaded from a server, where the server returned a 404 message in HTML instead of a PDF
file ;-)
Whenever that exception happens, you should store the bytes somewhere, and examine them.
Without those bytes, nobody will be able to give you useful advice.
⁸¹http://stackoverflow.com/questions/12357126/invalidpdfexception-pdf-header-signature-not-found
⁸²http://stackoverflow.com/users/1426199/david-choi
Inspect a PDF with iText 45
How to Get PDF page width and height?
I have a PDF, and I want to get the width and height of each page using iTextSharp. This
is what I have so far:
string source=@"D:\pdf\test.pdf";
PdfReader reader = new PdfReader(source);
Posted on StackOverflow on Aug 13, 2013 ⁸³ by Mohamed Kamal⁸⁴
Do you want the MediaBox?
Rectangle mediabox = reader.GetPageSize(page);
Do you want the rotation?
int rotation = reader.GetPageRotation(page);
Do you want the combination of both?
Rectangle pagesize = reader.GetPageSizeWithRotation(page);
Do you want the CropBox?
Rectangle cropbox = reader.GetCropBox(page);
These are some methods that will give you information about the dimensions of a page. Most of
them return an object of type Rectangle that has methods such as getWidth() and getHeight() to
get the width and the height of the page. Other useful methods are getLeft() and getRight() as
well as getTop() and getBottom(). These four methods return the x and y coordinates that define
the boundaries of your page.
⁸³http://stackoverflow.com/questions/18202660/how-to-get-pdf-page-width-and-height
⁸⁴http://stackoverflow.com/users/2677532/mohamed-kamal
Manipulating existing PDFs
In this chapter, we’re going to solve some problems whenworking with existing PDFs that need to be
split into different files, merged or stamped. Usually, we are going to use a combination of PdfReader
to read the document and PdfStamper, PdfCopy or PdfSmartCopy to create a new PDF. Note that we’ll
skip filling out interactive forms for now. We’ll deal with AcroForm and XFA technology in the next
chapter.
How to update a PDF without creating a new PDF?
I need to change the value of a field in an existing PDF file. I am using PdfReader,
PdfStamper and AcroFields and that’s working fine. But, in doing so, it is required to
create a new PDF and I would like the change to be reflected in the existing PDF itself.
If I am setting the destination filename to be the same as the original filename, then my
application fails.
Posted on StackOverflow on Apr 18, 2013 ⁸⁵ by tk2013⁸⁶
You can’t read a file and write to it simultaneously. Think of how Microsoft Word works: you can’t
open a Word document and write directly to it. Word always creates a temporary file, writes the
changes to it, then replaces the original file with it, and then throws away the temporary file.
You can do that too:
• read the original file with PdfReader;
• create a temporary file for PdfStamper, and when you’re done,
• replace the original file with the temporary file.
Or:
• read the original file into a byte[],
• create PdfReader with this byte[], and
• use the path to the original file for PdfStamper.
The latter option is more dangerous, as you’ll lose the original file if you do something that causes
an exception in PdfStamper. If I were you, I’d create a temporary file.
⁸⁵http://stackoverflow.com/questions/16081831/using-itextsharp-stamper-required-to-update-in-the-same-pdf
⁸⁶http://stackoverflow.com/users/2239456/tk2013
Manipulating existing PDFs 47
How to add an image watermark to a PDF file?
I’m using C# and iTextSharp to add a watermark to my PDF files:
Document document = new Document();
PdfReader pdfReader = new PdfReader(strFileLocation);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(strFileLocationOut, FileMode.Create, FileAccess.Write, FileShare.None));
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(WatermarkLocation);
img.SetAbsolutePosition(100, 300);
PdfContentByte waterMark;
for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++) {
waterMark = pdfStamper.GetOverContent(pageIndex);
waterMark.AddImage(img);
}
pdfStamper.FormFlattening = true;
pdfStamper.Close();
It works fine, but my problem is that in some PDF files no watermark is added although
the file size increased, any idea?
Posted on StackOverflow on Jul 8, 2013 ⁸⁷ by Abady⁸⁸
The fact that the file size increases is a good indication that the watermark is added. The main
problem is that you’re adding the watermark outside the visible area of the page. See my answer to
the question How to position text relative to page using iText?
You need something like this:
Rectangle pagesize = reader.getCropBox(pageIndex);
if (pagesize == null)
pagesize = reader.getMediaBox(pageIndex);
img.SetAbsolutePosition(
pagesize.GetLeft(),
pagesize.GetBottom());
That is: if you want to add the image in the lower-left corner of the page. You can add an offset, but
make sure the offset in the x direction doesn’t exceed the width of the page, and the offset in the y
direction doesn’t exceed the height of the page.
⁸⁷http://stackoverflow.com/questions/17522965/how-to-add-a-watermark-to-a-pdf-file
⁸⁸http://stackoverflow.com/users/1450667/abady
Manipulating existing PDFs 48
Why does the function to concatenate / merge PDFs
cause issues in some cases?
I’m using the following code to merge PDFs together using iText:
public static void concatenatePdfs(List listOfPdfFiles, File outputFile)
throws DocumentException, IOException {
Document document = new Document();
FileOutputStream outputStream = new FileOutputStream(outputFile);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
for (File inFile : listOfPdfFiles) {
PdfReader reader = new PdfReader(inFile.getAbsolutePath());
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
document.newPage();
PdfImportedPage page = writer.getImportedPage(reader, i);
cb.addTemplate(page, 0, 0);
}
}
document.close();
}
This usually works great! But once and a while, it’s rotating some of the pages by 90
degrees? Anyone ever have this happen?
Posted on StackOverflow on Apr 14, 2014 ⁸⁹ by Nicholas DiPiazza⁹⁰
There are errors once in a while because you are using the wrong method to concatenate documents.
You should not use PdfWriter to concatenate (or merge) PDF documents. That is wrong because:
• You completely ignore the page size of the pages in the original document (you assume they
are all of size A4),
• You ignore page boundaries such as the crop box (if present),
• You ignore the rotation value stored in the page dictionary,
• You throw away all interactivity that is present in the original document, and so on.
Concatenating PDFs is done using PdfCopy, see for instance:
⁸⁹http://stackoverflow.com/questions/23062345/function-that-can-use-itext-to-concatenate-merge-pdfs-together-causing-some
⁹⁰http://stackoverflow.com/users/1174024/nicholas-dipiazza
Manipulating existing PDFs 49
Document document = new Document();
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
document.open();
PdfReader reader;
String line = br.readLine();
// loop over readers
// add the PDF to PdfCopy
reader = new PdfReader(baos.toByteArray());
copy.addDocument(reader);
reader.close();
// end loop
document.close();
If you are merging documents that contain fields, you need to add the following line:
copy.SetMergeFields();
Manipulating existing PDFs 50
How to merge documents correctly?
I would like to add a link to an existing pdf that jumps to a coordinate on another page.
I have the following problem when printing the PDF file after merge, the PDF documents
get cut off. Sometimes this happens because the documents aren’t 8.5 x 11 whereas the page
size might be 11 x 17.
Is there some way to detect the page size and then use that same page size for those
documents? Or, if not, is it possible to have it fit to page? This is my code:
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page;
int currentPageNumber = 0;
int pageOfCurrentReaderPDF = 0;
Iterator iteratorPDFReader = readers.iterator();
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
Rectangle r = pdfReader.getPageSize(
pdfReader.getPageN(pageOfCurrentReaderPDF + 1));
if(r.getWidth()==792.0 && r.getHeight()==612.0)
document.setPageSize(PageSize.A4.rotate());
else
document.setPageSize(PageSize.A4);
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
cb.addTemplate(page, 0, 0);
cb.beginText();
cb.setFontAndSize(bf, 9);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
+ currentPageNumber + " of " + totalPages, 520, 5, 0);
cb.endText();
}
pageOfCurrentReaderPDF = 0;
}
document.close();
Manipulating existing PDFs 51
Screen shot
Posted on StackOverflow on Feb 12, 2014 ⁹¹ by Sumit Vaidya⁹²
Using PdfWriter to merge documents is a bad idea. This has been explained on StackOverflowmany
times!
Merging documents is done using PdfCopy (or PdfSmartCopy).
If you need an example, see for instance the FillFlattenMerge2⁹³ example:
Document document = new Document();
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
document.open();
PdfReader reader;
String line = br.readLine();
// loop over readers
// add the PDF to PdfCopy
reader = new PdfReader(baos.toByteArray());
copy.addDocument(reader);
⁹¹http://stackoverflow.com/questions/21731439/pdf-page-cutting-through-itext-api
⁹²http://stackoverflow.com/users/2853641/sumit-vaidya
⁹³http://itextpdf.com/sandbox/acroforms/reporting/FillFlattenMerge2
Manipulating existing PDFs 52
reader.close();
// end loop
document.close();
In your case, you also need to add page numbers, you can do this in a second go, as is done in the
StampPageXofY⁹⁴ example:
PdfReader reader = new PdfReader(src);
int n = reader.getNumberOfPages();
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfContentByte pagecontent;
for (int i = 0; i < n; ) {
pagecontent = stamper.getOverContent(++i);
ColumnText.showTextAligned(pagecontent, Element.ALIGN_RIGHT,
new Phrase(String.format("page %s of %s", i, n)), 559, 806, 0);
}
stamper.close();
reader.close();
Or you can add them while merging, as is done in the MergeWithToc⁹⁵ example.
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(filename));
PageStamp stamp;
document.open();
int n;
int pageNo = 0;
PdfImportedPage page;
Chunk chunk;
for (Map.Entry entry : filesToMerge.entrySet()) {
n = entry.getValue().getNumberOfPages();
for (int i = 0; i < n; ) {
pageNo++;
page = copy.getImportedPage(entry.getValue(), ++i);
stamp = copy.createPageStamp(page);
chunk = new Chunk(String.format("Page %d", pageNo));
if (i == 1)
chunk.setLocalDestination("p" + pageNo);
ColumnText.showTextAligned(stamp.getUnderContent(),
⁹⁴http://itextpdf.com/sandbox/stamper/StampPageXofY
⁹⁵http://itextpdf.com/sandbox/merge/MergeWithToc
Manipulating existing PDFs 53
Element.ALIGN_RIGHT, new Phrase(chunk),
559, 810, 0);
stamp.alterContents();
copy.addPage(page);
}
}
document.close();
for (PdfReader r : filesToMerge.values()) {
r.close();
}
reader.close();
I strongly advise against using PdfWriter to merge documents! As documented, you are throwing
away all annotations by adding PdfImportedPage instances to a document using addTemplate().
This is typically not what you want. You’re only making it harder on yourself if you insist on using
that class. I don’t understand why so many people use the wrong approach to merge documents. I
blame the unofficial documentation for the popularity of this wrong approach.
Interactive forms
Is your form based on AcroForm technology or is it based on the XML Forms Architecture? That’s
a common counter-question you’ll be confronted with when asking a question about forms. In any
case, these answers should help you solving the most common problems with respect to forms.
How to fill out a pdf file programmatically? (AcroForm
technology)
What techniques available to fill a pdf form automatically using external data and save
them. I have to use data from a database to fill a template PDF and save a copy of it on
disk with that data. Language and platform is not issue but it would be good if it can run
on windows and Linux.
Posted on StackOverflow on Jun 24, 2010 ⁹⁶ by affan⁹⁷
If your form is based on AcroForm technology, you can use iText to fill it out like this:
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.setField(key, value);
stamper.setFormFlattening(true);
stamper.close();
reader.close();
In this snippet, src is the source of a PDF file (could be a path to a file, could be a byte[]) and dest
is the path to the resulting PDF. The key corresponds with the name of a field in your template. The
value corresponds with the value you want to fill in. If you want the form to keep its interactivity,
you need to remove the line stamper.setFormFlattening(true); otherwise all form fields will be
removed, resulting in a flat PDF.
⁹⁶http://stackoverflow.com/questions/3108704/how-to-fill-out-a-pdf-file-programatically
⁹⁷http://stackoverflow.com/users/109769/affan
Interactive forms 55
How to fill out a pdf file programmatically? (Dynamic
XFA)
I have a dynamic XFA Form that I can fill out manually using Adobe Acrobat on my
computer. Using iTextSharp I can read what the XFA XML data is and see the structure of
the data. I am essentially trying to mimic that with iText using the following code:
PdfReader pdfReader = new PdfReader(sourceFilePath);
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(pdfReader, ms)) {
XfaForm xfaForm = new XfaForm(pdfReader);
XmlDocument doc = new XmlDocument();
doc.Load(replacementXmlFilePath);
xfaForm.DomDocument = doc;
xfaForm.Changed = true;
XfaForm.SetXfa(xfaForm, stamper.Reader, stamper.Writer);
}
var bytes = ms.ToArray();
File.WriteAllBytes(destinationtFilePath, bytes);
}
For some reason this code doesn’t work.
Posted on StackOverflow on May 11, 2013 ⁹⁸ by jon333⁹⁹
This question was answered by the person who posted the question:
I found the issue. The replacement DomDocument needs to be the entire merged XML of
the new document, not just the data or datasets portion.
I upvoted this answer, because it’s not incorrect, but now that I think it’s better to use the example
from the book¹⁰⁰:
⁹⁸http://stackoverflow.com/questions/16502427/how-can-i-set-xfa-data-in-a-static-xfa-form-in-itextsharp-and-get-it-to-save
⁹⁹http://stackoverflow.com/users/511518/jon333
¹⁰⁰http://itextpdf.com/examples/iia.php?id=165
Interactive forms 56
public byte[] ManipulatePdf(String src, String xml) {
PdfReader reader = new PdfReader(src);
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
AcroFields form = stamper.AcroFields;
XfaForm xfa = form.Xfa;
xfa.FillXfaForm(XmlReader.Create(new StringReader(xml)));
}
return ms.ToArray();
}
}
As you can see, it’s not necessary to replace thewhole XFAXML. If you use the FillXfaFormmethod,
the data is sufficient.
Interactive forms 57
How to fill XFA form using iText without breaking
usage rights?
This is my code:
using (FileStream pdf = new FileStream("C:/test.pdf", FileMode.Open))
using (FileStream xml = new FileStream("C:/test.xml", FileMode.Open))
using (FileStream filledPdf = new FileStream("C:/test_f.pdf", FileMode.Create))
{
PdfReader pdfReader = new PdfReader(pdf);
PdfStamper stamper = new PdfStamper(pdfReader, filledPdf);
stamper.AcroFields.Xfa.FillXfaForm(xml);
stamper.Close();
pdfReader.Close();
}
This code throws no exception and everything seems to be OK, but if I open filled pdf,
Adobe Reader says something like this:
This document enabled extended features. This document was changed since
it was created and using extended features isn’t possible anymore.
If I choose xml manually by clicking ‘Import data’ from Adobe Reader, form is filled
properly, so I guess there is no error in xml.
Posted on StackOverflow on Oct 29, 2014 ¹⁰¹ by paldir¹⁰²
You are not creating the PdfStamper object correctly. Use:
PdfStamper stamper = new PdfStamper(pdfReader, filledPdf, '\0', true)
In your code, you are not using PdfStamper in append mode. This means that iText will reorganize
the different objects in your PDF. Usually that isn’t a problem.
However: your PDF is Reader-enabled, whichmeans that your PDF is digitally signed using a private
key owned by Adobe. By reorganizing the objects inside the PDF, that signature is broken. This is
made clear by the message you already mentioned:
This document enabled extended features. This document was changed since it was
created and using extended features isn’t possible anymore.
¹⁰¹http://stackoverflow.com/questions/26629498/how-to-fill-xfa-form-using-itext
¹⁰²http://stackoverflow.com/users/4148435/paldir
Interactive forms 58
To avoid breaking the signature, you need to use PdfStamper in append mode. Instead of reorganizing the original content, iText will now keep the original file intact and append new content after
the end of the original file.
Actions and annotations
All things interactive are discussed here. Except for forms, we’ve already covered these.
How to create a link to a specific page number?
I know how to target any text of any PDF page using code:
Anchor click = new Anchor("Click to go to Target");
click.Reference = "#target";
Paragraph p1 = new Paragraph();
p1.Add(click);
doc.Add(p1);
Anchor target = new Anchor("Target");
target.Name = "target";
doc.Add(target);
My question is how to target a page based on its number. For example if targeted page
number is 6, clicking on the Anchor text should take to 6th page.
Posted on StackOverflow on Feb 20, 2014 ¹⁰³ by Yogesh¹⁰⁴
Instead of an Anchor, you need a Chunk. To this Chunk you need to add a PdfAction. The action
needs to be a gotoLocalPage()¹⁰⁵ action.
For instance:
Chunk chunk = New Chunk("Go to page 5");
PdfAction action = PdfAction.GotoLocalPage(5, New PdfDestination(0), writer);
chunk.SetAction(action);
¹⁰³http://stackoverflow.com/questions/21907184/itextsharp-how-to-target-pdf-page-number
¹⁰⁴http://stackoverflow.com/users/532384/yogesh
¹⁰⁵http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfAction.html#gotoLocalPage%28int,%20com.itextpdf.text.pdf.PdfDestination,%20com.
itextpdf.text.pdf.PdfWriter%29
Actions and annotations 60
How to insert a “linked rectangle” with iText?
I want to insert a hyperlink into an existing PDF at a position I know in advance: I already
have the coordinates of a rectangle on a given page. I want to link this rectangle to another
page of the same PDF (which I also know in advance). How do I achieve this?
Posted on StackOverflow on Nov 7, 2013 ¹⁰⁶ by Hans Stricker¹⁰⁷
Please take a look at the AddLinkAnnotation¹⁰⁸ example.
As you (should) already know (but you didn’t show what you’ve already tried, which is kind of
mandatory on StackOverflow), you can use PdfStamper to manipulate an existing PDF. Adding a
rectangular link on one page to another page, is as simple as adding a link annotation to that page:
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Rectangle linkLocation = new Rectangle(523, 770, 559, 806);
PdfDestination destination = new PdfDestination(PdfDestination.FIT);
PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
3, destination);
link.setBorder(new PdfBorderArray(0, 0, 0));
stamper.addAnnotation(link, 1);
stamper.close();
The link object is created using:
• the writer instance tied to the stamper,
• the rectangle (the position you say you know in advance,
• a highlighting option (pick one: HIGHLIGHT_NONE, HIGHLIGHT_INVERT, HIGHLIGHT_OUTLINE,
HIGHLIGHT_PUSH, HIGHLIGHT_TOGGLE),
• the page you want to link to,
• a destination.
Once you have an instance of PdfAnnotation, you can add it to a specific page using the
addAnnotation() method.
¹⁰⁶http://stackoverflow.com/questions/22194844/inserting-a-linked-rectangle-with-itext
¹⁰⁷http://stackoverflow.com/users/363429/hans-stricker
¹⁰⁸http://itextpdf.com/sandbox/annotations/AddLinkAnnotation
Actions and annotations 61
How to add a maps with a pointer to a PDF?
I am using java and iText to create a pdf. Is it possible to add a map with a pointer on it so
the user will know where the starting point is?
Posted on StackOverflow on Nov 6, 2014 ¹⁰⁹ by user2487493¹¹⁰
What do you mean by “a map with a pointer so the user knows where the starting point is”? If you
have a map in your PDF, you could add an annotation that looks like an arrow. Is that what you’re
looking for?
Since you didn’t answer my counter-question added in comment, I’m providing two examples. If
these are not what you’re looking for, you really should clarify your question.
Example 1: add a custom shape as extra content on top of a map
This is demonstrated in the AddPointer¹¹¹ example:
PdfContentByte canvas = writer.getDirectContent();
canvas.setColorStroke(BaseColor.RED);
canvas.setLineWidth(3);
canvas.moveTo(220, 330);
canvas.lineTo(240, 370);
canvas.arc(200, 350, 240, 390, 0, (float) 180);
canvas.lineTo(220, 330);
canvas.closePathStroke();
canvas.setColorFill(BaseColor.RED);
canvas.circle(220, 370, 10);
canvas.fill();
If we know the coordinates of the pointer, we can draw lines and curves that result in a the red
pointer shown here (see the red pin near the Cambridge Innovation Center):
¹⁰⁹http://stackoverflow.com/questions/26752663/adding-maps-at-itext-java
¹¹⁰http://stackoverflow.com/users/2487493/user2487493
¹¹¹http://itextpdf.com/sandbox/objects/AddPointer
Actions and annotations 62
Map with a pin
Example 2: add a line annotation on top of a map
This is demonstrated in the AddPointerAnnotation¹¹² example:
Rectangle rect = new Rectangle(220, 350, 475, 595);
PdfAnnotation annotation = PdfAnnotation.createLine(writer, rect,
"Cambridge Innovation Center", 225, 355, 470, 590);
PdfArray le = new PdfArray();
le.add(new PdfName("OpenArrow"));
le.add(new PdfName("None"));
annotation.setTitle("You are here:");
annotation.setColor(BaseColor.RED);
annotation.setFlags(PdfAnnotation.FLAGS_PRINT);
annotation.setBorderStyle(
new PdfBorderDictionary(5, PdfBorderDictionary.STYLE_SOLID));
annotation.put(new PdfName("LE"), le);
annotation.put(new PdfName("IT"), new PdfName("LineArrow"));
writer.addAnnotation(annotation);
The result is an annotation (which isn’t part of the real content, but part of an interactive layer on
top of the real content):
¹¹²http://itextpdf.com/sandbox/annotations/AddPointerAnnotation
Actions and annotations 63
Map with an annotation
It is interactive in the sense that extra info is shown when the user clicks the annotation:
Map with an annotation that has been opened
Many other options are possible, but once again: your question wasn’t entirely clear.
Extracting text from PDFs
iText can parse PDFs to extract the content of a page. As there are many different ways to create a
PDF file, and as the text on a page usually isn’t more than a bunch of characters drawn on a page,
it’s not trivial to extract text correctly.
How to read text from a specific position?
I have a problem using iTextSharpwhen reading data fromPDF File.What I want to achieve
is to read only specific part of PDF page (I want to only retrieve Address Information, which
is located at constant position). I have seen using iTextSharp for reading all pages such as
following:
StringBuilder text = new StringBuilder();
if (File.Exists(fileName)) {
PdfReader pdfReader = new PdfReader(fileName);
for (int page = 1; page <= pdfReader.NumberOfPages; page++) {
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
text.Append(currentText);
}
pdfReader.Close();
}
return text.ToString();
But how can I only restrict it to a specific location?
Posted on StackOverflow on Jun 12, 2014 ¹¹³ by Robert J.¹¹⁴
You are using a SimpleTextExtractionStrategy instead of a LocationTextExtractionStrategy. If
rect is a rectangle based on the coordinates of your address, you need:
¹¹³http://stackoverflow.com/questions/24185066/itextsharp-read-from-specific-position
¹¹⁴http://stackoverflow.com/users/1539189/robert-j
Extracting text from PDFs 65
RenderFilter[] filter = {new RegionTextRenderFilter(rect)};
ITextExtractionStrategy strategy;
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++) {
strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter);
sb.AppendLine(PdfTextExtractor.GetTextFromPage(reader, i, strategy));
}
Now you’ll get all the text snippets that intersect with the rect (so part of the text may be outside
rect, iText doesn’t cut text snippets in pieces).
Note that you can get the MediaBox of a page using:
Rectangle mediabox = reader.GetPageSize(pagenum);
The coordinate of the lower-left corner is x = mediabox.Left and y = mediabox.Bottom; the
coordinate of the upper-right corner is x = mediabox.Right and y = mediabox.Top.
The values of x increase from left to right; the values of y increase from bottom to top. The unit of
the measurement system in PDF is called “user unit”. By default one user unit coincides with one
point (this can change, but you won’t find many PDFs with a different UserUnit value). In normal
circumstances, 72 user units = 1 inch.
General questions about iText
These are some questions about iText in general. They aren’t always about a technical problem, but
they can be about a basic concept that is explained in more detail in one of the later chapters.
Unit Testing and Automated Testing Questions
I have been searching for some unit tests for the program iText with no luck. Is anyone
aware of any such tests? Also, does anyone know if the developers use any automatic
testing tools on iText, such as Jenkins?
Posted on StackOverflow on Feb 21, 2014 ¹¹⁵ by user3338813¹¹⁶
Internally, we use Jenkins as well as TeamCity.
We have two types of tests:
1. The tests that are added when new core functionality is added. You can find these where
Maven expects them: each Maven project has a src directory with 2 sub directories: main and
test. For instance: if you look at iText core, you’ll find the released stuff here¹¹⁷ and the tests
here¹¹⁸. Most of these tests are built on top of our testutils¹¹⁹.
2. The tests that are added when we get questions on SO or when we create code samples for
the books. For these we use a generic test classes such as GenericTest¹²⁰ and SandboxSampleWrapper¹²¹. The wrapper class makes creating a test a no-brainer. All you need to do to
turn a sample into a test is adding the @WrapToTest annotation. Well, actually there’s more
involved: you need to follow a specific pattern when writing a sample: always use SRC and
DEST for source PDFs and resulting PDFs, always use a createPdf() or manipulatePdf()
method, and always give the cmp file the same name as the DEST file prefixed with cmp_.
In both cases, you’ll find PDF files of which the name starts with cmp_, see for instance the cmpfiles
folder¹²² for the examples. In both cases, you’ll find references to Ghostscript and a compare tool
(you’ll need to configure these).
¹¹⁵http://stackoverflow.com/questions/21944424/itext-unit-testing-and-automated-testing-questions
¹¹⁶http://stackoverflow.com/users/3338813/user3338813
¹¹⁷http://sourceforge.net/p/itext/code/HEAD/tree/trunk/itext/src/main/
¹¹⁸http://sourceforge.net/p/itext/code/HEAD/tree/trunk/itext/src/test/
¹¹⁹http://sourceforge.net/p/itext/code/HEAD/tree/trunk/itext/src/main/java/com/itextpdf/testutils/
¹²⁰http://sourceforge.net/p/itext/code/HEAD/tree/trunk/sandbox/src/test/java/sandbox/GenericTest.java
¹²¹http://sourceforge.net/p/itext/code/HEAD/tree/trunk/sandbox/src/test/java/sandbox/SandboxSampleWrapper.java
¹²²http://sourceforge.net/p/itext/code/HEAD/tree/trunk/sandbox/cmpfiles/
General questions about iText 67
Why do I get a “Could not find PdfGraphics2D” error?
I have come across a runtime exception “Could not find class
com.itextpdf.awt.PdfGraphics2D”. I wanted to create a PDF document from android
device. For that I used iText library. This my code for creating PDF:
Document document = new Document();
PdfWriter.getInstance(document, outStream);
document.open();
document.add(new Paragraph(data));
document.close();
The code works fine. It is creating PDF successfully. but it gives me a runtime exception:
06-14 10:09:20.491: W/dalvikvm(764):
Unable to resolve superclass of Lcom/itextpdf/awt/PdfGraphics2D; (1251)
06-14 10:09:20.491: W/dalvikvm(764):
Link of class 'Lcom/itextpdf/awt/PdfGraphics2D;' failed
06-14 10:09:20.491: E/dalvikvm(764):
Could not find class 'com.itextpdf.awt.PdfGraphics2D',
referenced from method com.itextpdf.text.pdf.PdfContentByte.createGraphics
06-14 10:09:20.491: W/dalvikvm(764):
VFY: unable to resolve new-instance 480
(Lcom/itextpdf/awt/PdfGraphics2D;) in Lcom/itextpdf/text/pdf/PdfContentByte;
06-14 10:09:25.280: E/dalvikvm(764):
Could not find class 'org.bouncycastle.cert.X509CertificateHolder',
referenced from method com.itextpdf.text.pdf.PdfReader.readDecryptedDocObj
06-14 10:09:25.280:
W/dalvikvm(764): VFY: unable to resolve new-instance 1612
(Lorg/bouncycastle/cert/X509CertificateHolder;) in Lcom/itextpdf/text/pdf/PdfReader;
I have done clean and build, added jar to libs folder and make it selected on order and
export and i done lot of research for past 2 days. but nothing helped me. Based upon my
knowledge there should be these possibilities: (1) the external jar isn’t loaded properly,
or (2) the class PdfGraphics2D extends java.awt.Graphics2D which is not available on
Android.
Posted on StackOverflow on Jun 14, 2013 ¹²³ by R9J¹²⁴
¹²³http://stackoverflow.com/questions/17102533/could-not-find-class-com-itextpdf-awt-pdfgraphics2d
¹²⁴http://stackoverflow.com/users/1912085/r9j
General questions about iText 68
You’ve discovered that PdfGraphics2D extends java.awt.Graphics2D, and as you already know
Graphics2D is a forbidden class on Android.
You’ve also encountered problems related to BouncyCastle.
This tells me that you’re using the Java version of iText instead of the Android port¹²⁵. In the
Android port, we replaced BouncyCastle by SpongyCastle (as recommended when using encryption
on Android) and we removed all references to forbidden classes (for instance in the awt and nio
packages).
Please switch to using the Android port of iText. It is called iTextG¹²⁶.
Why do I get a “getOutputStream() has already been
called for this response” error in JSP?
I’m using JDBC to fetch data from database and then I use iText to create a PDF file which
can be downloaded on client machine. The application is coded in HTML/JSP and runs on
Apache Tomcat.
I use the response.getOutputStream to create an output PDF file immediately. However,
I get the following error:
getOutputStream() has already been called for this response
How can I generate a dynamic PDF file which can be downloaded by client machine?
Posted on StackOverflow on Jun 13, 2013 ¹²⁷ by Sahil Sharma¹²⁸
When you write JSP, you probably like white space and indentation, for instance:
¹²⁵http://itextpdf.com/product/itextg
¹²⁶http://itextpdf.com/product/itextg
¹²⁷http://stackoverflow.com/questions/17083318/how-to-insert-image-in-pdf-using-itext-and-download-to-client-machine
¹²⁸http://stackoverflow.com/users/2367475/sahil-sharma
General questions about iText 69
<% //a line of code %>
<%
// some more code
%>
<% // another line of code %>
<%
response.getOutputStream();
%>
This will always cause the exception "getOutputStream() has already been called for this
response" regardless if you’re using iText or not. The getOutputStream() method was called the
moment you introduced your first white space character in your JSP script.
To fix this, you need to remove all white space:
<% //a line of code %><%
// some more code
%><% // another line of code %><%
response.getOutputStream();
%>
Not a single character is accepted outside the <% and %> markers. As explained in the better JSP
manuals, you shouldn’t use JSP to create binary files. Why not? Because JSP introduces white space
characters at arbitrary places in your binary file. That results in corrupt files. Use Servlets instead!
Legal questions
Although StackOverflow is a forum where developers post technical questions and technical
questions only, we notice that some developers also want to know more about the legal aspects of
using open source, more specifically: is it legal to use iText for free? Is there a license fee involved?
What is the difference between Lowagie and iText?
What is the difference between lowagie and iText. Is this just version difference or upgradation to library. Which one recommended to be used.
Posted on StackOverflow on Nov 22, 2012 ¹²⁹ by Adeeb Cheulkar¹³⁰
I am Lowagie, the lowagie you refer to. I’m the original author of iText and the author of the “iText
in Action” books.
As explained in the Sales FAQ¹³¹, you should use the latest version of iText.
The differences between old versions of iText (iText 2.x.y dates from July 2009 or earlier) and newer
versions of iText can be found in the changelogs¹³².
The 5.0.0 version had the following substantial changes:
• iText and iTextSharp started using the same version numbers
• the iText.jar is compiled using Java 5 (instead of with the JDK 1.4).
• The F/OSS license has been upgraded from MPL/LGPL to AGPL.
• The package names have changed from com.lowagie to com.itextpdf.
• The toolbox and RTF support have been removed: they are now in a separate project at
SourceForge.
Numerous bugs have been fixed since July 2009. Functionality that makes your PDFs future-proof
such as updates regarding new digital signature standards and new standards such as PDF/UA,
PDF/A-2 and PDF/A-3 is only available in the more recent iText versions.
¹²⁹http://stackoverflow.com/questions/13515210/difference-between-lowagie-and-itext
¹³⁰http://stackoverflow.com/users/1771109/adeeb-cheulkar
¹³¹http://itextpdf.com/salesfaq
¹³²http://itextpdf.com/changelog
Legal questions 71
Can iText 2.1.7 or earlier be used commercially?
Can iText 2.1.7 (MPL/GPL) licence be used in commercial projects? I am not a legal guy but
lots of discussion threads suggest that there is no issue using the earlier version (2.1.7) of
iText in commercial projects as that version is bounded with terms & conditions governed
by MPL¹³³/GPL license.
However, if we look at iText’s official website¹³⁴, it says as the licence has been upgraded
to AGPL licence, one has to buy the software before commercially using it. See the topic
entitled Why shouldn’t I use iText 2.x (or iTextSharp 4.x)?
LEGAL REASONS: Older versions of iText under the free model may contain code fragments that infringe other people’s copyrights or intellectual
property rights. iText Software Group has done a significant investment in
identifying and eliminating all those cases as of version 5.1. which is one of
the reason why it is now a paying commercial version.We do not recommend
the use of versions prior to 5.1 for commercial projects as your company could
be liable for copyright or IP infringements.
Of course, this seems a warning only. Discouragement of not using iText with earlier
version due to Technical reasons could be understood but Legal reasons are not worth.
What about the commercial projects who have been using iText 2.1.7 before the licence
upgrade happened in iText? Would they now have to change their whole project planning
because iText has now change his mind to not to distribute it commercially? Of course
iText might has done significant investment in upgrading the version technically but what
about the investment one might have done in his commercial project using iText 2.1.7 or
earlier?
Please someone who understands legal implications of both the licences clarify this
confusion. iText can use suchwarning to encourage its sale but is there anything substantial
in such warning? Can one use iText with version 2.1.7 or earlier commercially? Comments
from Mr. Bruno Lowagie, the original author of iText are highly appreciated.
Posted on StackOverflow on Sep 6, 2014 ¹³⁵ by Devendra Sharma¹³⁶
The first iText company was founded in 2008. The purpose of this company was to put all the
Intellectual Property of the code into one legal entity. This was achieved by identifying [1.] every
third party project from which code was borrowed, as well as [2.] every individual developer who
contributed code.
¹³³https://www.mozilla.org/MPL/1.1/
¹³⁴http://itextpdf.com/salesfaq
¹³⁵http://stackoverflow.com/questions/25696851/can-itext-2-1-7-or-earlier-can-be-used-commercially
¹³⁶http://stackoverflow.com/users/2881228/devendra-sharma
Legal questions 72
[1.] Some code snippets were borrowed from projects with an ambiguous license. For instance: we
had a snippet that was released under Sun’s Example License (which allowed us to use the code), but
in the comment section of the class, it said that the code was proprietary to SUN (which prevented
us to use the code). Which of both prevailed? Being an ignorant developer at that time, I thought
the Example License was the one I could use, just like some people claim that you can use iText 2.1.7
today. Lawyers however, disagreed: they said that the most strict license was the valid one.
We solved these problems by (1) asking permission to use code with ambiguous licenses, (2)
refactoring code if we didn’t get permission, (3) removing code we couldn’t refactor.
We did the same with contributions from individual developers.
[2.] The IP from individual developers was transferred to iText GroupNV (formerly known as 1T3XT
BVBA) by asking every developer who contributed 20 lines of code or more to sign a Contributor
License Agreement.
Two problems arose:
1. Individual developers could not be reached. For example: we dropped the RTF package
completely because we couldn’t find a couple of the core developers of the RTF functionality.
2. In a couple of cases, we had to negotiate about the CLA. For example: one company didn’t
like the CLA. Instead, this company released the contribution of its employees under an MIT
license, so that we could use it anyway. Another organization was really slow in agreeing with
the CLA. It took us until September 2009 before we received formal approval. Only after this
approval, we switched to the AGPL. I can’t disclose the document (it was different from the
CLA), nor the name of the organization (I hope I don’t break the NDA just by writing this). I
can only say that we only had full coverage of the code base after that document was signed.
Ignorant developers claim that the LGPL/MPL header “protects” them, but what if some proprietary
code was accidentally added to a class with such a header? Does this make that proprietary code
“available under the MPL/LGPL”? If it did, it would be sufficient to take proprietary code, add an
MPL/LGPL header and publish it. Doing this on purposewould be illegal. Doing this out of ignorance
can be pardoned if there is a willingness to fix the issue.
In the early years of open source, it did occur that proprietary code got mixed into an open source
project by accident. At iText, we have invested a lot of time and effort into cleaning up the code
base. Since that exercise, we are very disciplined with respect to code contributions. This is one of
the core tasks of a professional open source company.
After we fixed all the issues, we removed all copies of those old iText versions from our servers to
make sure we were in the clear. If a company decides to use some rogue version of iText 2.1.7 that is
outside of our control, this company does so willingly and knowingly, in other words: at its own
risk! There is no way such a company can claim: “We didn’t know there was a possible IP issue with
the code.”
If you want to use iText 2.1.7, you need to do the exercise we have done between 2007-2009 at
your own expense. This will cost you more than the price of a license. For instance: the individual
Legal questions 73
developers gave permission to iText Group NV to do business with iText, but will they give that
permission to you? How will you identify those individual developers?
Moreover: iText 2.1.7 dates from July 2009, meaning that it is more than 5 years old. Many bugs have
been fixed since that date. Should you knowingly introduce those bugs into the code base of your
customer, then your customer may claim that you had an alternative: you could have used a more
recent version of iText…
As for your question “what about the investment onemight have done in his commercial project using
iText 2.1.7 or earlier?” That investment must have been done at least 3 years ago, because we’ve been
informing people that they should upgrade for at least that long. Upgrading to a recent version is an
investment that should be categorized as a maintenance cost. It should be an affordable cost because
whoever has been using iText 2.1.7 for that long in a commercial project has been making money
thanks to iText for that long. Claiming that “iText has now changed its mind” is not correct unless
now is marked as a synonym of 5 years ago in your dictionary.
To be continued…
All the answers and the many code samples I have provided on StackOverflow were written in the
hope that they are helpful. I leave it up to the reader of this “Best of” selection to decide whether
or not “I’m kind of a dick” as the people who down-voted some of my answers claim. I just love
answering questions, and where love is involved, there’s also pain, for instance the pain if the love
isn’t returned. Some people seem to make a sport out of it to beg for an answer and then to thank me
by saying: we’re never going to be a customer of iText Software. Somehow that doesn’t compute. I
hope you understand.
Obviously, a book like this is never finished. New questions about iText are posted every day. I expect
that this book will grow over the years. Some answers may become obsolete, some new functionality
will require more clarification. This clarification may be provided in the form of an answer to a new
question, or as a topic in one of the other upcoming books:
• The ABC of PDF¹³⁷
• Create your PDFs with iText¹³⁸
• Update your PDFs with iText¹³⁹
• Sign your PDFs with iText¹⁴⁰
All of these books are available for free. No donation is expected.
¹³⁷https://leanpub.com/itext_pdfabce
¹³⁸https://leanpub.com/itext_pdfcreate
¹³⁹https://leanpub.com/itext_pdfupdate
¹⁴⁰https://leanpub.com/itext_pdfsign

Online Document Converter

This website help webmasters to achieve a better user experience. Instead of putting a link to download their prices lists and another type of documents, you can simply send a special link to this service, and we will show your document to your users directly without the need of downloading a special application or installing another browsers plugin. Currently, we can read about hundred the most used database files. Moreover, your users can share this document directly on social networks, giving your document additional page views. By using this service, you can save costs by not overloading your own web server, give your users a better way to read documents online without any problems, and allow them to easily download converted copy for offline reading in a format they like.


Previous 10

Next 10