iText PDF:文档没有页面
iText PDF: the document has no pages
我正在尝试使用基于日期范围的过滤器将报告导出为 PDF;但是,当我单击导出 PDF 时,我收到以下消息:
ExceptionConverter: java.io.IOException: 文档没有页面。
这是我的 PDF class' 代码:
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class PDFPrinter {
private String date1;
private String date2;
public void printpdf(){
try {
String file_name = JOptionPane.showInputDialog("Digite o nome do arquivo: ");
String file_place = "C:\";
String file_name_and_place = "C:\File\" + file_name + ".pdf";
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(file_name_and_place));
document.open();
ConnectionFactory myConn = new ConnectionFactory();
Connection connection = myConn.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "SELECT * FROM tb_pacientes";
ps = connection.prepareStatement(query);
rs=ps.executeQuery();
while(rs.next()){
PdfPTable table = new PdfPTable(3);
Paragraph para = new Paragraph(rs.getString("UID")+" "+rs.getString("Name")+" "+rs.getString("Address")+" "+rs.getString("Age")+" "+rs.getString("Frontline")+" "+rs.getString("VaccinationDate")+" "+rs.getString("Priority"));
document.add(para);
document.add(new Paragraph(" "));
}
document.close();
JOptionPane.showMessageDialog(null, "Seu arquivo foi criado com sucesso em "+file_name_and_place);
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Ocorreu um erro, tente novamente, por favor.");
e.printStackTrace();
}
}
public void printPDFWithFilter(String date1, String date2){
this.date1 = date1;
this.date2 = date2;
ConnectionFactory factory= new ConnectionFactory();
try(Connection c = factory.getConnection()){
String file_name = JOptionPane.showInputDialog("Digite o nome do arquivo: ");
String file_place = "C:\";
String file_name_and_place = "C:\File\" + file_name + ".pdf";
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(file_name_and_place));
document.open();
ConnectionFactory myConn = new ConnectionFactory();
Connection connection = myConn.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "SELECT * FROM tb_pacientes WHERE VaccinationDate BETWEEN ? AND ?";
ps = connection.prepareStatement(query);
ps.setString(1, "%" +date1+ "%");
ps.setString(2, "%" +date2+ "%");
rs=ps.executeQuery();
while(rs.next()){
PdfPTable table = new PdfPTable(3);
Paragraph para = new Paragraph(rs.getString("UID")+" "+rs.getString("Name")+" "+rs.getString("Address")+" "+rs.getString("Age")+" "+rs.getString("Frontline")+" "+rs.getString("VaccinationDate")+" "+rs.getString("Priority"));
document.add(para);
document.add(new Paragraph(" "));
}
document.close();
JOptionPane.showMessageDialog(null, "Seu arquivo foi criado com sucesso em "+file_name_and_place);
}
catch(Exception e){
e.printStackTrace();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String sStackTrace = sw.toString();
System.out.println(sStackTrace);
if (sStackTrace == "ExceptionConverter: java.io.IOException: The document has no pages."){
JOptionPane.showMessageDialog (null,"Selecione as datas novamente; com as configurações atuais, o documento não teria páginas.");
}
else{
JOptionPane.showMessageDialog(null, "Ocorreu um erro ao filtrar o PDF; por favor, tente novamente.");
}
}
}
}
下面是 PDF 生成按钮的代码:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String date1 = jDateChooser1.getDate().toString();
String date2 = jDateChooser2.getDate().toString();
PDFPrinter pdfp = new PDFPrinter();
pdfp.printPDFWithFilter(date1, date2);
}
我认为 SQL 和 JDateChooser 有问题,因为它的日期格式似乎与 SQL 的 DATE 格式不兼容,但我不确定,我还没有找不到任何改变它的方法,所以,我在这里寻求帮助。
您的 SQL 请求似乎没有 return 任何行,
将您的 PdfTable(3) 从 while() 中删除;
好吧,在bigtheo的回答和一点点毅力的帮助下,我能够解决问题哈哈;好吧,问题出在第二种方法中优先级列表中的百分比符号上。我只是擦掉了它们,它起作用了!
我正在尝试使用基于日期范围的过滤器将报告导出为 PDF;但是,当我单击导出 PDF 时,我收到以下消息: ExceptionConverter: java.io.IOException: 文档没有页面。 这是我的 PDF class' 代码:
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class PDFPrinter {
private String date1;
private String date2;
public void printpdf(){
try {
String file_name = JOptionPane.showInputDialog("Digite o nome do arquivo: ");
String file_place = "C:\";
String file_name_and_place = "C:\File\" + file_name + ".pdf";
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(file_name_and_place));
document.open();
ConnectionFactory myConn = new ConnectionFactory();
Connection connection = myConn.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "SELECT * FROM tb_pacientes";
ps = connection.prepareStatement(query);
rs=ps.executeQuery();
while(rs.next()){
PdfPTable table = new PdfPTable(3);
Paragraph para = new Paragraph(rs.getString("UID")+" "+rs.getString("Name")+" "+rs.getString("Address")+" "+rs.getString("Age")+" "+rs.getString("Frontline")+" "+rs.getString("VaccinationDate")+" "+rs.getString("Priority"));
document.add(para);
document.add(new Paragraph(" "));
}
document.close();
JOptionPane.showMessageDialog(null, "Seu arquivo foi criado com sucesso em "+file_name_and_place);
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Ocorreu um erro, tente novamente, por favor.");
e.printStackTrace();
}
}
public void printPDFWithFilter(String date1, String date2){
this.date1 = date1;
this.date2 = date2;
ConnectionFactory factory= new ConnectionFactory();
try(Connection c = factory.getConnection()){
String file_name = JOptionPane.showInputDialog("Digite o nome do arquivo: ");
String file_place = "C:\";
String file_name_and_place = "C:\File\" + file_name + ".pdf";
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(file_name_and_place));
document.open();
ConnectionFactory myConn = new ConnectionFactory();
Connection connection = myConn.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "SELECT * FROM tb_pacientes WHERE VaccinationDate BETWEEN ? AND ?";
ps = connection.prepareStatement(query);
ps.setString(1, "%" +date1+ "%");
ps.setString(2, "%" +date2+ "%");
rs=ps.executeQuery();
while(rs.next()){
PdfPTable table = new PdfPTable(3);
Paragraph para = new Paragraph(rs.getString("UID")+" "+rs.getString("Name")+" "+rs.getString("Address")+" "+rs.getString("Age")+" "+rs.getString("Frontline")+" "+rs.getString("VaccinationDate")+" "+rs.getString("Priority"));
document.add(para);
document.add(new Paragraph(" "));
}
document.close();
JOptionPane.showMessageDialog(null, "Seu arquivo foi criado com sucesso em "+file_name_and_place);
}
catch(Exception e){
e.printStackTrace();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String sStackTrace = sw.toString();
System.out.println(sStackTrace);
if (sStackTrace == "ExceptionConverter: java.io.IOException: The document has no pages."){
JOptionPane.showMessageDialog (null,"Selecione as datas novamente; com as configurações atuais, o documento não teria páginas.");
}
else{
JOptionPane.showMessageDialog(null, "Ocorreu um erro ao filtrar o PDF; por favor, tente novamente.");
}
}
}
}
下面是 PDF 生成按钮的代码:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String date1 = jDateChooser1.getDate().toString();
String date2 = jDateChooser2.getDate().toString();
PDFPrinter pdfp = new PDFPrinter();
pdfp.printPDFWithFilter(date1, date2);
}
我认为 SQL 和 JDateChooser 有问题,因为它的日期格式似乎与 SQL 的 DATE 格式不兼容,但我不确定,我还没有找不到任何改变它的方法,所以,我在这里寻求帮助。
您的 SQL 请求似乎没有 return 任何行,
将您的 PdfTable(3) 从 while() 中删除;
好吧,在bigtheo的回答和一点点毅力的帮助下,我能够解决问题哈哈;好吧,问题出在第二种方法中优先级列表中的百分比符号上。我只是擦掉了它们,它起作用了!