How2:将 JPanel 添加到文档然后导出为 PDF
在任何论坛上关于编程的第一篇文章...我通常只是搜索直到找到答案...但这次我真的陷入困境...
这就是问题... 我有一个 JPanel,最近发现 itext 为您提供了一种将 Java GUI 导出到 PDF 的方法...
我似乎无法理解 itext 的语言,也无法理解如何将简单的 JPanel 添加到文档,然后将该文档导出到 PDF。这就是我目前所拥有的一切...
import java.io.FileOutputStream;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.awt.Color;
import javax.swing.*;
public class HelloWorld {
public static void main(String[] args) {
try
{
//Panel creation and setup
JPanel panel = new JPanel();
//just to ensure that the panel has content...
JLabel label = new JLabel("i am a label");
panel.add(label);
panel.setSize(100,100);
//so that even if the label doesnt get added...
//i can see that the panel does
panel.setBackground(Color.red);
//my understanding of the code below: the virtual document
Document document = new Document();
//my interpretation just writes the virtual pdf document to the hdd
PdfWriter writer = PdfWriter.getInstance
(document, new FileOutputStream("C:/test.pdf"));
//begin editing the vpdf
document.open();
//i wanna do something like this
//document.add(panel);
//end editing the vpdf
document.close();
} catch (Exception e)
{
System.out.println(e);
}
}
请帮忙...我试图使代码尽可能短以避免无用的东西...
提前致谢... 克雷格
first post ever in any forum with regard to programming... i usually just search until i find the answer... but this time im really stuck...
here's the problem...
i have a JPanel, and recently discovered that itext provides you with a way to export Java GUI to PDF...
i cant seem to understand itext's language nor how to add a simple JPanel to a document then export that document to a PDF... this is all i have at the moment...
import java.io.FileOutputStream;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.awt.Color;
import javax.swing.*;
public class HelloWorld {
public static void main(String[] args) {
try
{
//Panel creation and setup
JPanel panel = new JPanel();
//just to ensure that the panel has content...
JLabel label = new JLabel("i am a label");
panel.add(label);
panel.setSize(100,100);
//so that even if the label doesnt get added...
//i can see that the panel does
panel.setBackground(Color.red);
//my understanding of the code below: the virtual document
Document document = new Document();
//my interpretation just writes the virtual pdf document to the hdd
PdfWriter writer = PdfWriter.getInstance
(document, new FileOutputStream("C:/test.pdf"));
//begin editing the vpdf
document.open();
//i wanna do something like this
//document.add(panel);
//end editing the vpdf
document.close();
} catch (Exception e)
{
System.out.println(e);
}
}
please please help... i tried to make the code as short as possible to avoid useless stuff...
thanks in advance...
Craig
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在面板上调用
print
并指定要打印到的pdf图形,如下所示:You need to call
print
on the panel and specify the pdf's graphics to print to, as shown below:好的,我所做的就是为我使用导出到 PDF 的代码添加整个类文档...它非常简单,因为它只是将整个面板导出为 Graphics2D 图像,然后也是 PDF...我仍在研究它,因为我打算将文档导出为 FDF PDF...但现在该文档导出得很好...
这里是:
如果您需要帮助,我将非常愿意帮助您:
它有很多代码,但是非常简单...只是一大堆简单的东西...我不是最先进的程序员...
无论如何...让我知道你是否需要帮助 Boro
ok what i've done is add the WHOLE class document for the code in which i used the export to PDF... its very simple in the sense that it just exports the WHOLE panel as a Graphics2D image then that too PDF... im still working on it as i intend to export the document as a FDF PDF... but for now this document exports fine...
here it is:
if you need help, i'll be more than willing to assist you:
its alot of code, however its very easy... just a whole bunch of easy stuff... im not the most advanced programmer...
anyway... lemme know if you need help Boro