Kiran Kodimela wrote:
> 
> Hi All,
> 
> I got a question:
> 
> I am using iText library for exporting Swing Component (JTable, in my
> case)
> to PDF.
> 
> While doing so, i get the PDF generated with all the data elements. But i
> do
> not see the
> 
> Column Headers. Please advise, if i need to do any changes in the code.
> 
> ex:
> 
> XYZ  |  12344
> BHF | 334343
> 5HJJ | 453343
> 
> The above is my resultant table which i see in PDF. But the column headers
> itself,   "Prod", "Amt" are missing in the table. Can any body answer to
> this question.
> 
> I appreciate your help.
> 
> Thanks,
> Kiran
> 
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> iText-questions mailing list
> iText-questions@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/itext-questions
> Buy the iText book: http://itext.ugent.be/itext-in-action/
> 
> 

I did like this.

        class PrintThread extends Thread {

                PrintThread() {
                }

                @Override
                public void run() {
                        
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                        Document document = new Document(pageSize);
                        document.addAuthor("author");
                        document.addTitle("title");
                        document.addSubject("subject");
                        document.addKeywords("keyWord");
                        document.addCreator("creator");
                        try {
                                JTableHeader columnHeader = 
table.getTableHeader();
                                Dimension columnHeaderSize = 
columnHeader.getSize();
                                int pageWidth = (int) pageSize.getWidth();
                                int pageHeight = (int) pageSize.getHeight();
                                int headerHeight = columnHeaderSize.height;
                                int usableListHeight = pageHeight - 
headerHeight;
                                int rowHeight = table.getRowHeight();
                                int mod = usableListHeight % rowHeight;
                                int effectiveListHeight = usableListHeight - 
mod;
                                int numPages = (table.getRowCount() * rowHeight)
                                                / effectiveListHeight;
                                if (mod != 0) {
                                        numPages++;
                                }
                                PdfWriter writer = 
PdfWriter.getInstance(document,
                                                new FileOutputStream(filePath));
                                document.open();
                                PdfContentByte cb = writer.getDirectContent();
                                int vOffset = 0;
                                //
                                BaseFont bf = BaseFont.createFont(fontName, 
fontEncoding,
                                                BaseFont.NOT_EMBEDDED);
                                parentView.getProgress().setMaximum(numPages - 
1);
                                for (int i = 0; i < numPages; i++) {
                                        if (i > 0) {
                                                document.newPage();
                                        }
                                        cb.saveState();
                                        Graphics2D g2 = null;
                                        if (shapes) {
                                                // Create the graphics as shapes
                                                g2 = 
cb.createGraphicsShapes(pageWidth, headerHeight
                                                                + 
effectiveListHeight);
                                        } else {
                                                // Create the graphics with pdf 
fonts
                                                cb.setFontAndSize(bf, fontSize);
                                                g2 = 
cb.createGraphics(pageWidth, headerHeight
                                                                + 
effectiveListHeight);
                                        }
                                        // Print the table to the graphics
                                        Shape oldClip = g2.getClip();
                                        // g2.translate(0, 0);
                                        g2.clipRect(0, 0, pageWidth, 
headerHeight);
                                        columnHeader.print(g2);
                                        // Secondary clipRect() doesn't work 
without calling
                                        // setClip().
                                        g2.setClip(oldClip);
                                        g2.clipRect(0, headerHeight, pageWidth, 
headerHeight
                                                        + effectiveListHeight);
                                        g2.translate(0, -vOffset + 
headerHeight);
                                        table.print(g2);
                                        g2.setClip(oldClip);
                                        g2.dispose();
                                        cb.restoreState();
                                        vOffset += effectiveListHeight;
                                        parentView.getProgress().setValue(i);
                                        parentView.getProgress().repaint();
                                }
                        } catch (DocumentException e) {
                                e.printStackTrace();
                                System.err.println(e.getMessage());
                        } catch (IOException e) {
                                e.printStackTrace();
                                System.err.println(e.getMessage());
                        } catch (Exception e) {
                                e.printStackTrace();
                                System.err.println(e.getMessage());
                        }
                        document.close();

                        parentView.getProgress().setValue(0);
                        parentView.getProgress().repaint();
                        
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                }
        }

Thanks,

tmatsugaki
-- 
View this message in context: 
http://www.nabble.com/Question-on-Swing-component-to-PDF-using-iText-tp15837303p21015944.html
Sent from the iText - General mailing list archive at Nabble.com.


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to