gpt4 book ai didi

java - 将 JScrollPane 内容打印为 pdf

转载 作者:行者123 更新时间:2023-12-01 10:05:58 24 4
gpt4 key购买 nike

我发现这段代码将 JPanel 传输到图像,然后将其打印到 PDF 文件,但因为我的 JPanel 位于 JScrollPane 内,并且不包含任何布局或尺寸,因为它取决于 JScrollPane 布局,所以我可以不要打印它。错误说无法打印高度为 0 和宽度为 0 的图片。当我尝试传递 JScrollPane 本身时,我只打印了 JScrollPane 的框架,而没有内容 JPanel。

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(100,300);
iTextImage.scalePercent(100);
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;
}

这是主要方法:

         JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(0, 0, 600, 510);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
getContentPane().add(scrollPane);

GridBagConstraints c = new GridBagConstraints();
JPanel borderpanel = new JPanel();
scrollPane.setViewportView(borderpanel);

Color veryLight = new Color(239,230,230);
JPanel container = new JPanel();
borderpanel.add(container, BorderLayout.CENTER);
container.setLayout(new GridLayout(0,1,0,1));
container.setBackground(Color.WHITE);

JPanel test = new JPanel();
test.setLayout(new GridLayout(0,1,0,1));
container.add(test, BorderLayout.CENTER);

JPanel rowPanel = new JPanel();
rowPanel.setPreferredSize(new Dimension(580,50));
test.add(rowPanel, BorderLayout.WEST);
rowPanel.setLayout(null);

JLabel auctionTitle = new JLabel("Auction Title");
auctionTitle.setForeground(Color.WHITE);
auctionTitle.setFont(new Font("Tahoma", Font.PLAIN, 14));
auctionTitle.setBounds(102, 0, 82, 20);
rowPanel.add(auctionTitle);
rowPanel.setBackground(SystemColor.GRAY);

JTextArea textArea = new JTextArea();
textArea.setBackground(veryLight);
textArea.setBounds(191, 0, 386, 50);
rowPanel.add(textArea);


final java.awt.Image image = getImageFromPanel(borderpanel);

String fileName = "C:\\Users\\Test\\Desktop\\newfile.pdf";
printToPDF(image, fileName);

我想打印borderpanel

最佳答案

使用java.awt.Robot获取面板图像:

public static java.awt.Image getImageFromPanel(Component component) {

Point p = new Point(0, 0);
SwingUtilities.convertPointToScreen(p, component);

int width = component.getSize().width;
int height = component.getSize().height;
Rectangle screenBounds = new Rectangle(p.x, p.y, width, height);

Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenBounds);

component.getGraphics().drawImage(image, 0, 0, component);

return image;
}

关于java - 将 JScrollPane 内容打印为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36476464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com