我想将 Enite Jframe UI 转换为 PDF 文件。我在这里得到了一份代码表格。但它抛出异常说
Error in Print Module The document has no pages.
ExceptionConverter: java.io.IOException: The document has no pages.
at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:113)
at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1221)
at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:810)
at com.itextpdf.text.Document.close(Document.java:416)
我的代码用于打印 JFrame
public static void PrintFrameToPDF(JDialog bill)
{
try {
Document d = new Document();
PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream ("sample.pdf"));
d.open ();
PdfContentByte cb = writer.getDirectContent( );
PdfTemplate template = cb.createTemplate(bill.getWidth(),bill.getHeight());
Graphics2D g2d = template.createGraphics(bill.getWidth(),bill.getHeight());
bill.print(g2d);
bill.addNotify();
bill.validate();
d.close ();
}
catch(Exception e) {
System.out.println("Error in Print Module " + e.getMessage());
e.printStackTrace();
}
调用 PDF 的用法
PrintModule.PrintFrameToPDF(this);
// 当前 JFrame 表单中存在此行