iText Java - 无法在html标题

时间:2016-08-08 11:51:51

标签: java image itext

我正在关注此示例HtmlHeaderFooter.java 用于创建在每个页面上具有相同页眉和页脚的pdf文件但由于某种原因我无法将图像显示在pdf上。它只需要显示图像就是空白。

我尝试在每个文件夹上移动图像以防路径问题,但也没有运气。

以下是我的代码示例。

package amt.view.PDF.section0;

 import amt.Methods;
 import com.itextpdf.text.*;
 import com.itextpdf.text.pdf.*;
 import com.itextpdf.tool.xml.*;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;


 public class section0 {



     static String Customer;
     static String CustomerTitle;
     static String RefNo;
     static String Revision;
     static String RefNoTitle;
     public static String DEST;
     public static final String UserDesktop = System.getProperty("user.home") + "/Desktop";

     static String selectedID = "1";

     public static void getVariables() throws SQLException {
         Connection c = Methods.chooseConnection();
         String SQL = "SELECT * FROM preset_values WHERE ID LIKE '" + selectedID + "'";
         ResultSet rs = c.createStatement().executeQuery(SQL);
         while (rs.next()) {
             Customer = rs.getString("Customer");
             RefNo = rs.getString("Reference_No");
             CustomerTitle = rs.getString("Customer").replace(" ", "_");
             RefNoTitle = rs.getString("Reference_No").replace(" ", "_");
             Revision = rs.getString("Revision");
         }
         DEST = UserDesktop + "/" + CustomerTitle + "-" + RefNoTitle + "_Rev-" + Revision + ".pdf";

     }


     public static final String HEADER = "<table width=\"100%\" border=\"0\"><tr><td><img src=\"logo.png\" /></td><td align=\"right\">Some title</td></tr></table>";
     public static final String FOOTER = "<table width=\"100%\" border=\"0\"><tr><td>Footer</td><td align=\"right\">Some title</td></tr></table>";


     public class HeaderFooter extends PdfPageEventHelper {

         protected ElementList header;
         protected ElementList footer;

         public HeaderFooter() throws IOException {
             header = XMLWorkerHelper.parseToElementList(HEADER, "../headerfooter.css");
             footer = XMLWorkerHelper.parseToElementList(FOOTER, null);
         }

         @
         Override
         public void onEndPage(PdfWriter writer, Document document) {
             try {
                 ColumnText ct = new ColumnText(writer.getDirectContent());
                 ct.setSimpleColumn(new Rectangle(36, 832, 559, 810));
                 header.stream().forEach((e) - > {
                     ct.addElement(e);
                 });
                 ct.go();
                 ct.setSimpleColumn(new Rectangle(36, 10, 559, 32));
                 footer.stream().forEach((e) - > {
                     ct.addElement(e);
                 });
                 ct.go();
             } catch (DocumentException de) {
                 throw new ExceptionConverter(de);
             }
         }
     }

     public static void main(String[] args) throws IOException, DocumentException, SQLException {
         getVariables();
         File file = new File(DEST);
         file.getParentFile().mkdirs();
         new section0().createPdf(DEST);
     }

     public void createPdf(String filename) throws IOException, DocumentException {
         // step 1
         Document document = new Document(PageSize.A4, 36, 36, 36, 72);
         // step 2
         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
         writer.setPageEvent(new HeaderFooter());
         // step 3
         document.open();
         // step 4
         for (int i = 0; i < 50; i++)
             document.add(new Paragraph(Customer + "\n" + RefNo));
         // step 5
         document.close();
     }
 }

0 个答案:

没有答案