[英]How to Write HTML Content in PDF file using PDFBox
我想使用JAVA使用PDFBox編寫PDF中的HTML內容。 我該怎么寫? 有什么方法可以添加HTML內容? 有不同的添加方法,但不能添加HTML內容。
截至2.0.6,pdfbox中尚無html渲染支持。 但是,在以后的版本中,幾乎沒有關於此功能的承諾。
您也可以使用IText做同樣的事情,使用此代碼。
import java.io.FileOutputStream;
import java.io.StringReader;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.pdf.PdfWriter;
public class Test {
public static void main(String ... args ) {
try {
Document document = new Document(PageSize.LETTER);
PdfWriter.getInstance(document, new FileOutputStream("E:\\yogesh\\test.pdf"));
document.open();
document.addAuthor("author");
document.addSubject("subject");
document.addCreationDate();
document.addTitle("title");
HTMLWorker htmlWorker = new HTMLWorker(document);
String str = "<html><head></head><body>"+
"<table border='1'><tr><td>Demo<td>" +
"<td bgcolor='red'>DEMO<td></tr>DEMO</table>" +
"</body></html>";
htmlWorker.parse(new StringReader(str));
document.close();
System.out.println("Done");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.