2

我正在使用 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();
    }
  }
}

但这会给我输出

在此处输入图像描述

所需的输出是

在此处输入图像描述

它是创建占位符的正确方法..还是我需要做任何其他事情来创建占位符?如果是,请建议我。

4

1 回答 1

1

< 和 > 符号被视为 html 标记。因此,它不会显示在您的 pdf 中。

您可以定义名字并签名如下..

public class HtmlToPDF2 {
public static void main(String ... args ) {
....
....
String firstName = "&lt;name&gt;" ;
String sign = "&lt;sign&gt;";
....
....
}
}
于 2013-06-11T17:42:52.443 回答