无法在java中生成PDF文件的完整数据 [英] Can't generate complete data to PDF file in java

查看:391
本文介绍了无法在java中生成PDF文件的完整数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 jtable ,我用来显示一些数据。假设我有大约200行数据。我可以使用 iText库生成 pdf ,但我面临的问题是所有行不会生成。如何动态添加新页面以便生成所有行?请看看下面的代码,请在这里帮助我。

I have a jtable that i'm using to display some data. Say I have around 200 rows of data. I am able to generate the pdf, by using the iText library, but the problem i'm facing is that all the rows aren't generated. How can I add a new page dynamically so that I generate all the rows? Kindly have a look at the code below and please help me out here.

Document doc = new Document(new Rectangle(1350, 1450));
  PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, 800, 0.50f);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    java.util.Date date = new java.util.Date();
    String generatedDate = formatter.format(date);try {

        PdfWriter writer;
        writer = PdfWriter.getInstance(doc, new FileOutputStream(save_pdf.getSelectedFile().getAbsoluteFile() + ".pdf"));
    writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);

        doc.open();

        PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, writer);
        writer.setOpenAction(action);
        doc.add(new Paragraph("REPORTS", f));

        doc.add(new Paragraph("Document Generated On - " + generatedDate, f));

        PdfContentByte cb = writer.getDirectContent();

        cb.saveState();

              Graphics2D g2;

        g2 = cb.createGraphics(1350, 1275);
        Shape oldClip = g2.getClip();
        g2.clipRect(0, 0, 1350, 1275);//1275


        table1.print(g2);
        JTableHeader h = table1.getTableHeader();
        h.print(g2);

        g2.setClip(oldClip);
          writer.newPage();
        g2.dispose();
        cb.restoreState();

    } catch (DocumentException | FileNotFoundException e) {
    }
    doc.close();


推荐答案

好的,这是非常基本的例子......

Okay, so this is pretty basic example...

JTable 支持打印,通过各种打印方法,基本上归结为获取 JTable Printable 接口的实例并将其传递给print API,需要 Graphics2D 上下文来绘制... ...

JTable supports printing already, through it's various print methods, basically this boils down to getting an instance of the JTable Printable interface and passing it off to the print API, which needs a Graphics2D context to paint to...

奇怪的是,你有一个 Graphics2D 上下文,所以这里的技巧是行动作为打印机并调用 JTable 可打印 打印方法自己...

Oddly enough, you have a Graphics2D context, so the trick here is to "act" as the printer and call the JTable's Printable print method yourself...

DefaultTableModel model = new DefaultTableModel(0, 10);
for (int row = 0; row < 400; row++) {
    Object[] values = new Object[10];
    for (int col = 0; col < 10; col++) {
        values[col] = ((char) ('A' + col)) + "x" + row;
    }
    model.addRow(values);
}

JTable table = new JTable(model);
table.setSize(table.getPreferredSize());

JTableHeader tableHeader = table.getTableHeader();
tableHeader.setSize(tableHeader.getPreferredSize());

Document doc = new Document(new Rectangle(1350, 1450));
PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, 800, 0.50f);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = new java.util.Date();
String generatedDate = formatter.format(date);

Paper paper = new Paper();
paper.setSize(1350, 1450);
paper.setImageableArea(10, 10, 1350 - 20, 1450 - 20);

PageFormat pf = new PageFormat();
pf.setPaper(paper);

Printable printable = table.getPrintable(JTable.PrintMode.NORMAL, null, null);
try {

    PdfWriter writer;
    writer = PdfWriter.getInstance(doc, new FileOutputStream("test.pdf"));
    writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);

    doc.open();

    // Use this to "test" if there is page
    // available for printing, otherwise it prints
    // a empty page and I can't figure out
    // how to remove it :P
    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = img.createGraphics();

    Font f = new Font(Font.TIMES_ROMAN, 12f);
    int page = 0;
    int result = Printable.NO_SUCH_PAGE;
    PdfContentByte cb = writer.getDirectContent();
    do {

        result = printable.print(g, pf, page);

        if (result == Printable.PAGE_EXISTS) {

            cb.saveState();
            Graphics2D g2 = cb.createGraphics(1350, 1450);

            System.out.println(page);
            result = printable.print(g2, pf, page);

            g2.dispose();
            cb.restoreState();

            doc.add(new Paragraph("REPORTS", f));
            doc.add(new Paragraph("Document Generated On - " + generatedDate, f));

            page++;
            doc.newPage();

        }

    } while (result == Printable.PAGE_EXISTS);
    g.dispose();

} catch (DocumentException | PrinterException | FileNotFoundException e) {
    e.printStackTrace();
} finally {
    doc.close();
}

现在,我需要显示表格才能获得行标题显示,但可能有另一种解决方法。

Now, I need to display the table in order to get the row headers to display, but there might be another work around for this.

这篇关于无法在java中生成PDF文件的完整数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆