package example;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.html.simpleparser.StyleSheet;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/*import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPTable;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.PdfWriter;*/
public class clas3 {
public static void main(String[] args) throws Exception, IOException {
Document document = new Document();
StyleSheet st = new StyleSheet();
st.loadTagStyle("body", "encoding", "30,10");
PdfWriter.getInstance(document, new FileOutputStream("C:/Users/Jason/Desktop/html4.pdf"));
document.open();
try{HTMLWorker htmlWorker = new HTMLWorker(document);
FileReader xy=new FileReader("C:/Users/Jason/Desktop/SELECT Operation.htm");
//String zy=String.format("<table><tr>"+xy+"</tr></table>");
//StringReader hy= new StringReader(zy);
PdfPCell cell;
List p = new ArrayList();
p = htmlWorker.parseToList(xy, st);
Phrase p1 = new Phrase();
for (int k = 0; k < p.size(); ++k){
PdfPTable tb = (PdfPTable)((Element)p.get(k));
tb.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
System.out.println("1");
document.add(tb);
System.out.println(((Element)p.get(k)).toString());
document.addTitle("jack");
//p1.add((com.itextpdf.text.Element)p.get(k));
}
//cell = new PdfPCell(p1);
//document.add(cell);
}catch(IOException e){}catch(Exception e){}
//document.add((Element) p.get(k));}
document.close();
}
}
这是我的 HTML 到 PDF 代码。我收到此错误
Exception in thread "main" 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:1217)
at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:787)
at com.itextpdf.text.Document.close(Document.java:420)
at example.clas3.main(clas3.java:57)
请帮忙。