简体   繁体   English

iText swing组件超过一页

[英]iText swing component to more then one page

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. 我有一个jpanel,它的高度很大,我想画成pdf,大约2-3页,有时甚至更多。 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 ? 我该怎么办,我如何告诉iText它需要开始一个新页面并在其中继续使用jpanel? 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) {
    }

You use the JPanel only to create the graphics (to paint on g2d ), it has nothing to do with your problem per se. 您仅使用JPanel创建图形(在g2dg2d ),这与您自己的问题无关。

As images don't have "natural" wrap boundaries (like words or paragraphs) you need to do this yourself. 由于图像没有“自然的”环绕边界(例如单词或段落),因此您需要自己执行此操作。 Split the image, ie create several images manual at useful boundaries (depending on the content, probably you need to adapt the paint method) and place them on separate pages. 分割图像,即在有用的边界(取决于内容,可能需要修改paint方法)上创建几幅图像,并将它们放置在单独的页面上。

You can create a big enough template and draw all the JPanel content to that template. 您可以创建一个足够大的模板并将所有JPanel内容绘制到该模板。 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. 然后,您可以使用不同的偏移量将此模板添加到不同的pdf页面,并因此在各个页面上显示模板的不同部分。

Eg in case of a sample panel (3 pages high) 例如,如果是示例面板(3页高)

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;
}

you can spread the information on multiple PDF pges like this: 您可以像这样在多个PDF页面上分发信息:

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 ... 要么 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM