public class FontItext {
public static void main(String[] args) throws IOException, DocumentException {
com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4,55f,30f,30f,30f);
document.setMarginMirroring(true);
PdfWriter.getInstance(document, new FileOutputStream("D://AAA.pdf"));
document.open();
BaseFont bf = BaseFont.createFont("D:\\times.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf);
Paragraph _p = new Paragraph();
ArrayList htmlObjs = (ArrayList)HTMLWorker.parseToList(new StringReader("<table border=1><tr><td>Test</td><td>Test</td></tr></table>"),
null);
for (int k = 0; k < htmlObjs.size(); ++k) {
ArrayList<Chunk> chunk = (ArrayList<Chunk>)((Paragraph)htmlObjs.get(k)).getChunks();
for (int l = 0; l < chunk.size(); l++) {
Font _original_chunk_font = chunk.get(l).getFont();
Font _newchunk_font = new Font(bf);
_newchunk_font.setFamily(_original_chunk_font.getFamilyname());
_newchunk_font.setStyle(_original_chunk_font.getStyle());
_newchunk_font.setSize(_original_chunk_font.getSize());
_newchunk_font.setColor(_original_chunk_font.getColor());
chunk.get(l).setFont(_newchunk_font);
}
_p.add((Element)htmlObjs.get(k));
document.add((Element)htmlObjs.get(k));
}
document.close();
}
}
错误......
Exception in thread "main" java.lang.ClassCastException: com.itextpdf.text.pdf.PdfPTable cannot be cast to com.itextpdf.text.Paragraph
at Font.FontItext.main(FontItext.java:46)
嘿伙计帮助我......
答案 0 :(得分:0)
我对IText没有那么多经验,但例外说:
取代:
ArrayList<Chunk> chunk = (ArrayList<Chunk>)((Paragraph)htmlObjs.get(k)).getChunks();
通过
ArrayList<Chunk> chunk = (ArrayList<Chunk>)((PdfPTable)htmlObjs.get(k)).getChunks();