我正在使用itext并将html转换为pdf,因为我正在使用此代码
import java.io.FileOutputStream;
import java.io.StringReader;
import javax.sql.rowset.spi.XmlWriter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;
public class HtmlToPDF2 {
// itextpdf-5.4.1.jar http://sourceforge.net/projects/itext/files/iText/
public static void main(String ... args ) {
try {
Document document = new Document(PageSize.LETTER);
PdfWriter.getInstance(document, new FileOutputStream("testpdf1.pdf"));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
String firstName = "<name>" ;
String sign = "<sign>";
String str = "<html> " +
"<body>" +
"<form>" +
"<div><strong>Dear</strong> "+firstName +",</div><br/>"+
"<div>"+
"<P> It is informed that you are selected in your interview<br/>"+
" and please report on the <b>20 may</b> with your all original <br/>"+
" document on our head office at jaipur.>"+
" </P>"+
" </div><br/>"+
" <div>"+
" <p>Yours sincierly </p><br/>"+sign+"</div>"+
" </form>"+
"<body>"+
"<html>";
htmlWorker.parse(new StringReader(str));
document.close();
System.out.println("Done");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
但这会给我输出
期望的输出是
是创建占位符的正确方法..还是我需要做任何其他事情来创建占位符?如果是,请建议我。
答案 0 :(得分:1)
&LT;和&gt;标志视为html标签。因此,它不会显示在你的PDF格式中。
你可以定义firstName并签名如下..
public class HtmlToPDF2 {
public static void main(String ... args ) {
....
....
String firstName = "<name>" ;
String sign = "<sign>";
....
....
}
}