Download presentation
Presentation is loading. Please wait.
Published byPatience Gibson Modified over 9 years ago
1
Field Trip # 21 Creating PDFs with Java By Keith Lynn
2
Java Frames A JFrame is a top-level container It can run in a stand alone program It can contain independent panels The JFrame has a title
3
LayoutManager LayoutManagers define how components are placed on a container There are predefined layout managers, we can use. FlowLayout (components arranged linearly), GridLayout (components arranged in a grid), GridBagLayout (we can assign weights to components so they can take up more than one row or column)
4
JButton A JButton is a component that we can place on a JFrame It is a push button We can place a label in the button A JButton has a margin. If the label won't display, we can obtain the insets of the button and change the margin
5
Handling Events In order to handle events like button clicks, we attach a listener to the button We implement the ActionListener interface which contains the method actionPerformed If the listener is attached properly to the button, then when the button is clicked, actionPerformed is called
6
Importing Packages Packages are a way of arranging similar classes In order to use a package, we import it We can import a single class or we can import a group of classes using the wildcard (*) Note that import is not recursive If we use a wildcard to import classes, it will not import subpackages
7
iText Itext is a package written by Bruno Lowagie It allows us to directly create a PDF document It also allows us to also read PDF documents We can place text on the PDF We can also place images We can load PDF forms and fill them in
8
Creating a Basic PDF Document We begin by creating a Document object We next create a FileOutputStream object and use the FileOutputStream to create a PdfWriter Before we put anything in the Document, we open it After we have created the Document, we close it It is necessary to close the Document when we are done
9
Things we can do with a PDF Document Drawing a graph Drawing an image Drawing a Table Drawing Graphics Drawing Chart There are other things that can be done with a PdfContentByte, but these are the ones we will consider
10
Drawing a Graph To draw the graph, we will consider points on the coordinate system where (0,0) is the origin and translate them to a system where the original is at the middle of the PDF (420,300) We compute the value of the function and translate it to this system Each time we place a new point on the graph, we use the method lineTo Once we have moved to all points, we call the method stroke
11
The graph
12
Drawing an Image We will create something similar to the graph We will compute the Mandlebrot set which is a special fractal After the set has been computed, we create a BufferedImage We then use then add the image to the document with the method addImage
13
The image
14
Drawing a Table We can easily creating columnar text by using a PdfPTable We specify the number of columns in the table Then we one by one place the elements in the table One interesting use of the PdfPTable is to connect to a Wikipedia site and download information about Presidents and Vice Presidents and put them in a table
15
The table
16
Downloading information We can connect to a URL with the URL object We can openStream on it We can then read from it using a FileInputStream We can wrap the FileInputStream in a Scanner and read the whole contents using the method useDelimiter(ā\\Zā)
17
Drawing Graphics We can also draw graphics on a PDF like we can a Java program We can obtain a reference to a Graphics object by calling the method createGraphics and specifying a width and height This gives us a Graphics2D object We can then use any draw method we would use in the paint method of an applet
18
The graphics
19
Creating a Chart Another interesting thing we can do in a PDF is create a chart There is a special third party library called JfreeChart Using JFreeChart, we can easily create bar charts and pie charts In order to draw the bar and pie charts we create a Graphics2D object and draw on that object
20
The Bar Chart
21
The Pie Chart
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.