使用 itext 的 XML 工作者

时间:2023-03-18
本文介绍了使用 itext 的 XML 工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.io.FileOutputStream;
import java.io.StringReader;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class HtmlToPDF2 {

      // itextpdf-5.4.1.jar  http://sourceforge.net/projects/itext/files/iText/
      // xmlworker-5.4.1.jar http://sourceforge.net/projects/xmlworker/files/
      public static void main(String[] args ) {
            try {
              Document document = new Document(PageSize.LETTER);
              PdfWriter pdfWriter = PdfWriter.getInstance
                   (document, new FileOutputStream("C:/Users/Jason/Desktop/testpdf.pdf"));
              document.open();
              document.addAuthor("Real Gagnon");
              document.addCreator("Real's HowTo");
              document.addSubject("Thanks for your support");
              document.addCreationDate();
              document.addTitle("Please read this");

              XMLWorkerHelper worker = XMLWorkerHelper.getInstance();

              String str = "<html><head></head><body>"+
                "<a href='http://www.rgagnon.com/howto.html'><b>Real's HowTo</b></a>" +
                "<h1>Show your support</h1>" +
                "<p>It DOES cost a lot to produce this site - in ISP storage and transfer fees, " +
                "in personal hardware and software costs to set up test environments, and above all," +
                "the huge amounts of time it takes for one person to design and write the actual content.</p>" +
                "<p>If you feel that effort has been useful to you, perhaps you will consider giving something back?</p>" +
                "<p>Donate using PayPal® to real@rgagnon.com.</p>" +
                "<p>Contributions via PayPal are accepted in any amount</p>" +
                "<P><br/><table border='1'><tr><td>Java HowTo</td></tr><tr>" +
                "<td style='background-color:red;'>Javascript HowTo</td></tr>" +
                "<tr><td>Powerbuilder HowTo</td></tr></table></p>" +
                "</body></html>";
              worker.parseXHtml(pdfWriter, document, new StringReader(str));
              document.close();
              System.out.println("Done.");
              }
            catch (Exception e) {
              e.printStackTrace();
            }
          }

}

这是我在网上找到的示例代码.当我尝试执行它时,我得到了这个错误:

This is a sample code that I found online.When I try executing it I get this error:

Exception in thread "main" java.lang.NoSuchMethodError: com.itextpdf.text.Paragraph.trim()Z
at com.itextpdf.tool.xml.html.AbstractTagProcessor.currentContentToParagraph(AbstractTagProcessor.java:257)
at com.itextpdf.tool.xml.html.Header.end(Header.java:93)
at com.itextpdf.tool.xml.html.AbstractTagProcessor.endElement(AbstractTagProcessor.java:189)
at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.close(HtmlPipeline.java:206)
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:141)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:395)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:235)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:213)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:175)
at xml.HtmlToPDF2.main(HtmlToPDF2.java:41)

我正在使用 itextpdf-5.1.2.jar 和 xmlworker-5.4.3.jar.请帮助

I am using itextpdf-5.1.2.jar and xmlworker-5.4.3.jar.Please help

推荐答案

与 xmlworker-5.4.1.jar 和 itextpdf-5.4.2.jar 完美配合.

It works perfectly with xmlworker-5.4.1.jar and itextpdf-5.4.2.jar.

这篇关于使用 itext 的 XML 工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:maven 抛出 ArrayOutOfBoundsException 下一篇:Java.lang.NumberFormatException 错误:空字符串 - 汇率应

相关文章

最新文章