2
我有一個Java應用程序,它具有一個帶有我想輸出爲PDF(使用iText)信息的JPanel。唯一的麻煩就是我需要在PDF上給這個JPanel一個不同的格式。由於JPanel在GUI上顯示,我無法將其更改爲我想要顯示在PDF上的方式,因爲這會改變顯示的JPanel。所以我做的是用我的PDF格式創建一個新的JPanel,用原始信息填充它,然後輸出這個新的PDF到PDF。到目前爲止沒有問題,除了我只能將這個JPanel輸出到PDF,如果它被添加到容器。顯然,我不希望這樣,因爲我不希望顯示這個新的JPanel。我甚至試圖設置它不可見,但這種方式也不會輸出。iText Java to PDF:如何輸出JPanel而不必將其添加到容器
的新JPanel:
JPanel myFormattedPanel = new JPanel(); {
myFormattedPanel.setLayout(new GridLayout(2, 3, 0, 2));
myFormattedPanel.setBackground(Color.WHITE);
(...)
getContentPane().add(myFormattedPanel); //How to not need this??
輸出爲PDF格式:
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, out);
document.open();
PdfContentByte cb = writer.getDirectContent();
float width = PageSize.A4.getWidth();
float height = PageSize.A4.getHeight()/2;
PdfTemplate resultsPanelPdfTemplate = cb.createTemplate(width, height);
Graphics2D g2d2 = new PdfGraphics2D(resultsPanelPdfTemplate, width, height);
myFormattedPanel.paint(g2d2);
g2d2.dispose();
cb.addTemplate(resultsPanelPdfTemplate, 0, 0);
document.close();
所以基本上,什麼方式輸出一個JPanel使用的iText,PDF格式,而無需將其添加到容器,或者至少,而不必使其可見?
非常感謝。
乾杯!
見遭受['LabelRenderTest'(http://stackoverflow.com/a/5853992/418556)[爲什麼JTable頭不出現在圖像中?](http://stackoverflow.com/q/7369814/418556)獲取提示。 – 2013-03-07 11:01:05