iText摆动组件超过一页 [英] iText swing component to more then one page

查看:76
本文介绍了iText摆动组件超过一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jpanel,它有一个相当大的高度,我想画成一个pdf,类似2-3页,有时甚至更多。
我的问题是它不会传递到下一页,它只会尝试将所有内容插入第一页,当页面结束时它将不会传递给下一页。
我搜索了一下,尝试了我能想到的一切,没什么。
我怎么能这样做,我怎么能告诉iText它需要开始一个新页面并继续使用那里的jpanel?
这是我的代码:

I have a jpanel that will have a pretty big height that i want to paint into a pdf,something like 2-3 pages,sometimes even more. My issue is that it will not pass on to the next page,it will just try and insert everything into the first page and when the page ends it won't pass to the next one. I searched a bit,tried everything i could figure out,nothing. How can i do that,how can i tell iText that it needs to start a new page and continue with the jpanel there ? Here's my code:

try {
        Document document = new Document(PageSize.A4);

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\HelloWorld.pdf"));
        document.open();
        jPanel1.addNotify();
        jPanel1.validate();         
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(600, 840);
        Graphics2D g2d = tp.createGraphics(595, 840);
        g2d.translate(4, 4);
        g2d.scale(0.708, 0.8);
        jPanel1.paint(g2d);
        cb.addTemplate(tp, 0, 0);
        g2d.dispose();
        document.close();
    } catch (DocumentException ex) {

    } catch (FileNotFoundException ex) {
    }


推荐答案

您可以创建足够大的模板并将所有JPanel内容绘制到该模板。然后使用不同的偏移量将此单个模板添加到不同的pdf页面,从而在各个页面上显示模板的不同部分。

You can create a big enough template and draw all the JPanel content to that template. Then you add this single template to different pdf pages using different offsets and, consequently, display different sections of the template on the individual pages.

例如。如果是样本面板(3页高)

E.g. in case of a sample panel (3 pages high)

class MyPanel extends JPanel {

    public MyPanel(Rectangle baseSize) {
        setBorder(BorderFactory.createLineBorder(Color.black));
        this.baseSize = baseSize;
    }

    public Dimension getPreferredSize() {
        return new Dimension((int)baseSize.getWidth(), ((int)baseSize.getHeight()) * 3);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);       

        g.drawString("Page 1", 10, 20);
        g.drawRoundRect(5, 5, (int)baseSize.getWidth() - 10, (int)baseSize.getHeight() - 10, 15, 15);
        g.drawString("Page 2", 10, 20 + (int)baseSize.getHeight());
        g.drawRoundRect(5, 5 + (int)baseSize.getHeight(), (int)baseSize.getWidth() - 10, (int)baseSize.getHeight() - 10, 15, 15);
        g.drawString("Page 3", 10, 20 + 2 * (int)baseSize.getHeight());
        g.drawRoundRect(5, 5 + 2 * (int)baseSize.getHeight(), (int)baseSize.getWidth() - 10, (int)baseSize.getHeight() - 10, 15, 15);
    }

    final Rectangle baseSize;
}

您可以在以下多个PDF图片上传播信息:

you can spread the information on multiple PDF pges like this:

public void test() throws FileNotFoundException, DocumentException
{
    MyPanel panel = new MyPanel(PageSize.A4);
    panel.setSize(panel.getPreferredSize());

    Document document = new Document(PageSize.A4);

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("target/test-outputs/big-panel.pdf"));
    document.open();
    panel.addNotify();
    panel.validate();         
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(panel.getWidth(), panel.getHeight());
    Graphics2D g2d = new PdfGraphics2D(tp, panel.getWidth(), panel.getHeight());
    panel.paint(g2d);
    g2d.dispose();
    cb.addTemplate(tp, 0, -2 * PageSize.A4.getHeight());
    document.newPage();
    cb.addTemplate(tp, 0, -PageSize.A4.getHeight());
    document.newPage();
    cb.addTemplate(tp, 0, 0);
    document.newPage();
    cb.addTemplate(tp, 0.3333f, 0, 0, 0.3333f, PageSize.A4.getWidth() / 3, 0);
    document.close();
}

如您所见,面板的某些部分可以显示为单独的页面;或者整个面板可以缩小一页显示。当然,您还可以显示小组的小部分。或旋转。或者......

As you see parts of the panel can be shown as individual pages; or the whole panel can be shown on one page scaled down. Of course you could also show small sections of the panel scaled up. Or rotated. Or ...

这篇关于iText摆动组件超过一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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