我设计了一个带有可滚动功能的摇摆组件的jpanel。我想将整个Jpanle导出为pdf文件。但我无法导出整个Jpanle。 我使用itext进行pdf生成。 我的问题是我无法将整个jpanel导出为pdf。当我输出一半的Jpanel 组件export.but一半部分不导出。 这是我的代码。
public void PrintFrameToPDF(JPanel c, File file) {
try {
Document d = new Document();
PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream("F://newfile.pdf"));
d.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate template = cb.createTemplate(800, 1600);
Graphics2D g2d = template.createGraphics(800, 1600);
// g2d.translate(1.0, 1.0);
c.paintAll(g2d);
// c.addNotify();
// c.validate();
g2d.dispose();
cb.addTemplate(template, 0, 0);
d.close();
} catch (Exception e) {
//
}
}
请帮助我。 TNX
答案 0 :(得分:3)
首先考虑从面板创建java.awt.Image
(通过将面板绘制到图像上)。然后,您可以使用:
com.itextpdf.text.Image
的实例
com.itextpdf.text.Image.getInstance(PdfWriter writer, java.awt.Image awtImage, float quality)
-
从com.itextpdf.textImage
获取java.awt.Image
的实例。图像以JPEG格式添加,具有用户定义的质量。
然后,您可以使用com.itextpdf.text.Image
API方法scaleToFit
或scalePercent()
(如下例所示)缩放图像。有关使用图像的更多信息(在iText中)可以找到here
以下程序创建一个大小为2000x2000的面板,其中正方形20x20(每个100px)被绘制到面板上。面板包含在滚动窗格内。然后将其绘制到图像上,图像将缩放并添加到pdf文档并打印为pdf。
下图仅显示如何将整个面板绘制到图像上,然后使用前一个面板图像创建另一个面板以绘制到新面板上。然后通过对话框显示新面板。
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
/**
* This example requires iText. I retrieved it from Maven repository
*
* <dependency>
* <groupId>com.itextpdf</groupId>
* <artifactId>itextpdf</artifactId>
* <version>5.5.2</version>
* </dependency>
*
* The program can be run without iText if you comment out the entire
* method printToPDF (and iText imports), along with it's method call in
* the class constructor. The result will be the the image above.
*/
public class LargePanelToImageMCVE {
public LargePanelToImageMCVE() {
LargeImagePanel panel = new LargeImagePanel();
JFrame frame = new JFrame();
frame.add(new JScrollPane(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
final java.awt.Image image = getImageFromPanel(panel);
/* This was just a text panel to make sure the full panel was
* drawn to the panel.
*/
JPanel newPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
/* Print Image to PDF */
String fileName = "D://newfile.pdf";
printToPDF(image, fileName);
/* This was just a test to show the newPanel drew the entire
* original panel (scaled)
*/
JOptionPane.showMessageDialog(null, newPanel);
}
public void printToPDF(java.awt.Image awtImage, String fileName) {
try {
Document d = new Document();
PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(
fileName));
d.open();
Image iTextImage = Image.getInstance(writer, awtImage, 1);
iTextImage.setAbsolutePosition(50, 50);
iTextImage.scalePercent(25);
d.add(iTextImage);
d.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static java.awt.Image getImageFromPanel(Component component) {
BufferedImage image = new BufferedImage(component.getWidth(),
component.getHeight(), BufferedImage.TYPE_INT_RGB);
component.paint(image.getGraphics());
return image;
}
/**
* Demo panel that is 2000x2000 px with alternating squares
* to check all squares are drawn to image
*/
public class LargeImagePanel extends JPanel {
private static final int FULL_SIZE = 2000;
private static final int PER_ROW_COLUMN = 20;
private static final int SQUARE_SIZE = FULL_SIZE / PER_ROW_COLUMN;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int row = 0; row < PER_ROW_COLUMN; row++) {
for (int col = 0; col < PER_ROW_COLUMN; col++) {
if ((row % 2) == (col % 2)) {
g.setColor(Color.BLACK);
} else {
g.setColor(Color.WHITE);
}
g.fillRect(col * SQUARE_SIZE, row * SQUARE_SIZE,
SQUARE_SIZE, SQUARE_SIZE);
}
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(FULL_SIZE, FULL_SIZE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new LargePanelToImageMCVE();
}
});
}
}