Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Swing / AWT / SWT
Generating pdf from jtable
Mamatha Rudrappa
Greenhorn
Posts: 2
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am developing a tool in
java
to generate pdf from jtable.
I tried to use itext but I am getting black pdf. Below is the code that I used.
import java.awt.BorderLayout; import java.awt.Graphics2D; import java.awt.Shape; import java.io.FileOutputStream; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTable; //import com.itextpdf.awt.PdfGraphics2D; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfContentByte; //import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; public class Pdf extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JTable table; public Pdf() { getContentPane().setLayout(new BorderLayout()); createTable(); } private void createTable() { Object[][] data = {{ "a", "b", "e", new Integer(5), new Boolean(false) } }; String[] columnNames = { "A", "B", "C", "D", "E" }; table = new JTable(data, columnNames); JPanel tPanel = new JPanel(new BorderLayout()); tPanel.add(table.getTableHeader(), BorderLayout.NORTH); tPanel.add(table, BorderLayout.CENTER); getContentPane().add(tPanel, BorderLayout.CENTER); } private void print() { Document document = new Document(PageSize.A4.rotate()); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("jTable.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); Graphics2D g2 = cb.createGraphicsShapes(500, 500); Shape oldClip = g2.getClip(); g2.clipRect(0, 0, 500, 500); table.print(g2); g2.setClip(oldClip); g2.dispose(); cb.restoreState(); } catch (Exception e) { System.err.println(e.getMessage()); } document.close(); } public static void main(String[] args) { Pdf frame = new Pdf(); frame.pack(); frame.setVisible(true); frame.print(); } }
Can anyone help me please..................?
Also to purchase 3rd party api's or libraries, which ones are good?
Thank you..
Darryl Burke
Bartender
Posts: 5167
11
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
It looks like you took the code from
http://www.java2s.com/Tutorial/Java/0419__PDF/CreatePdfdocumentfromJTable.htm
and modified it. Does the original example on java2s.com work for you?
luck, db
There are no new questions, but there may be new answers.
Mamatha Rudrappa
Greenhorn
Posts: 2
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thank you for the reply.
No, example from java2s.com does not work. It generates a blank pdf.
I am not sure what wrong i am doing..
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JTable ........
JTable
JTable
jtable
JTable
More...