Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:16:32+00:00 2026-06-12T14:16:32+00:00

In my project I have to print JTable components in a pdf with all

  • 0

In my project I have to print JTable components in a pdf with all the customizations(like borders,colors etc). For this requirement I searched a little bit and found one code to print JTable in pdfs. But after printing I found that table’s border for 1st row and 1st columns are missing plus I didn’t find a way to print at some specific coordinate. Please look at the specific code below:-

package com.jpmorgan.dqreport;

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.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class JTable2Pdf extends JFrame {
  private JTable table;

  public JTable2Pdf() {
    getContentPane().setLayout(new BorderLayout());
    createTable();
  }
  private void createTable() {
      Object[][] data = {
                {"Kathy", "Smith",
                 "SnowboardingXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", new Integer(5), new Boolean(false)},
                {"John", "Doe",
                 "Rowing", new Integer(3), new Boolean(true)},
                {"Sue", "Black",
                 "Knitting", new Integer(2), new Boolean(false)},
                {"Jane", "White",
                 "Speed reading", new Integer(20), new Boolean(true)},
                {"Joe", "Brown",
                 "Pool", new Integer(10), new Boolean(false)}
                };
      String[] columnNames = {"First Name",
              "Last Name",
              "Sport",
              "# of Years",
              "Vegetarian"};

    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("C:\\jTable.pdf"));

      document.open();
      PdfContentByte cb = writer.getDirectContent();

      cb.saveState();
      Graphics2D g2 = cb.createGraphics(500, 500);

      Shape oldClip = g2.getClip();
      g2.clipRect(10, 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) {
    JTable2Pdf frame = new JTable2Pdf();
    frame.pack();
    frame.setVisible(true);
    frame.print();
  }
}

Please suggest someway to print the entire JTable at a specific coordinate.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:16:34+00:00Added an answer on June 12, 2026 at 2:16 pm

    Hi I resolved the code by adding like below:-

    cb.saveState();
    
    PdfTemplate pdfTemplate = cb.createTemplate(table.getWidth(), table.getHeight());
    Graphics2D g2 = pdfTemplate.createGraphics(table.getWidth(), table.getHeight());
    /*g2.setColor(Color.BLACK);
    g2.drawRect(x-2, y-2, table.getWidth()+2, table.getHeight()+2);*/
    table.print(g2);
    System.out.println("x="+x + "," + "y=" + y);
    cb.addTemplate(pdfTemplate, x, y);
    g2.dispose();
    cb.restoreState();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view in a Django 1.4 project: def index(request): print reverse('menus_index') latest_menu_list
I am using php language. I project i have the application to be print,
I am using php language. I project i have the application to be print,
I have a project that is organized something like project/ __init__.py builder.py component/ __init__.py
I have an exam and I would like to print my Java (in NetBeans
i am developing a project in which i have to print a bill on
I have a project written in C# in VS2010. I am attempting to print
I have a project in Python 2.6 and I'd like to write a utf-8
I have to print out some PDFs for a project at work. Is there
I have to print the whole sourcecode of a java-project. The final version should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.