2

Im using an example i found on stackoverflow to explain my problem.

Java code :

/** * Example written by Bruno Lowagie in answer to the following question: * RowSpan does not work in iTextSharp? */

public class ParseHtml {
public static final String DEST = "results/xmlworker/html_table_4.pdf";
public static final String HTML = "D:\\Users\\Documenten\\NetBeansProjects\\ItextTest\\recources\\xml\\table2_css.html";

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

/**
 * Creates a PDF with the words "Hello World"
 * @param file
 * @throws IOException
 * @throws DocumentException
 */
public void createPdf(String file) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    // step 3
    document.open();
    // step 4
   // XMLWorkerHelper.getInstance().parseXHtml(writer, document, null, null);
    XMLWorkerHelper.getInstance().parseXHtml(writer, document,
            new FileInputStream(HTML));
    // step 5
    document.close();
}

}

Html code:

<table border="1" style="width: 100%">
<tr>
  <td colspan="5" align="center" style="background-color: lightblue">INVOICE</td>
</tr>
<tr>
  <td colspan="2" rowspan="4" style="background-color: white"><b>AIRNET NETWORKS</b><br />asdadadadaada asd asd a ads adsadsadsadasd</td>
  <td>INVOICE</td>
  <td>DATE</td>
  <td>aDATEsd</td>
</tr>
<tr>
    <td>Order</td>
    <td>XXXX</td>
    <td>Ref XXXXXX</td>
</tr>
<tr>
  <td>Delivery</td>
  <td>XXXX</td>
  <td>Ref XXXXXX</td>
</tr>
<tr>
  <td>Due Date</td>
  <td>XXXX</td>
  <td>Ref XXXXXX</td>
</tr>
<tr>
  <td colspan="2" rowspan="4" style="background-color: white"><p><b >CUSTOMER NAME</b></p> asd asd adadaadadadada  adadaadsasdad ada asd adad</td>
  <td>Customer Care No:</td>
  <td colspan="2">544646454,88877978975</td>
</tr>
<tr>
  <td>Email Id</td>
  <td colspan="2">airnet@gmail.com</td>
</tr>
<tr>
  <td>Account Details</td>
  <td colspan="2">5522245125545455 IFSC 323hasd<br /> SBI India</td>
</tr>
</table>
<div>&nbsp;</div>
<table border="1" style="width: 100%">
<tr>
  <td  style="background-color: lightblue" height="15" >Srno</td>
  <td  style="background-color: lightblue">Particulars</td>
  <td  style="background-color: lightblue">Quantity</td>
  <td  style="background-color: lightblue">Rate/Month</td>
  <td  style="background-color: lightblue">Total Rupees</td>
</tr>
<tr>
  <td valign="top">1</td>
  <td valign="top">1 MBPS Plan</td>
  <td valign="top">1</td>
  <td valign="top">600</td>
  <td valign="top">692</td>
</tr>
<tr>
  <td height="300" valign="top" >1</td>
  <td valign="top">1 MBPS Plan</td>
  <td valign="top">1</td>
  <td valign="top">600</td>
  <td valign="top">692</td>
</tr>
<tr>
  <td colspan="3" rowspan="3" valign="top">asdasdasd</td>
  <td colspan="1">Total</td>
  <td colspan="1">692</td>
</tr>
<tr>
  <td>Service Tax</td>
  <td>692</td>
</tr>
<tr>
  <td>Grand Total</td>
  <td>692</td>
</tr>
</table>

The problems im facing: Is it possible to give the html access to a javaclass so i can use it like this:

<tr>
  <td>invoice.getdate</td>
  <td>invoice.getInvoiceNumber</td>
  <td>invoice.getSomethingelse</td>
</tr>

Can you create a dynamic table like this:

<table border="1" style="width: 100%">
<tr>
  <td  style="background-color: lightblue" height="15" >Srno</td>
  <td  style="background-color: lightblue">Particulars</td>
  <td  style="background-color: lightblue">Quantity</td>
  <td  style="background-color: lightblue">Rate/Month</td>
  <td  style="background-color: lightblue">Total Rupees</td>
</tr>
 for (ServiceDTO service : listServices) {
 <tr>
  <td valign="top">service.getsomevalue</td>
  <td valign="top">service.getsomevalue</td>
  <td valign="top">service.getsomevalue</td>
  <td valign="top">service.getsomevalue</td>
  <td valign="top">service.getsomevalue</td>
</tr>          
   } 

As work around im using a stringbuilder. Where i create a temporarily html page then load the html page inside itext. But there has to be a better way ? Can someone give me some direction where to start ?

Community
  • 1
  • 1
Greg
  • 1,690
  • 4
  • 26
  • 52

0 Answers0