Hi,
Since no one else seemed interested and I needed to get HTML table
column widths into a PDF file, here's a patch for a simple
implementation (attached).
Only percentage values are supported and they are relative to each other
(so two columns set to "1%" and "4%" are actually 25% and 75% of table
width).
Table cell (TD) widths are retained from only one row (TR), i.e. the
last one that has a width set, and those widths are only used if they
are set for all the columns. Nested tables work as expected.
As said, it's limited but at least now there's an option.
-Samuli
> Hi,
>
> I believe you have a lot of other work.
>
> Can we aspect to support controlling cell width in future?
>
> Žiga
>
> -----Original Message-----
> From: itext-questions-bounces <at> lists.sourceforge.net
> [mailto:itext-questions-bounces <at> lists.sourceforge.net] On Behalf Of
> Paulo Soares
> Sent: Friday, August 29, 2008 2:48 PM
> To: Post all your questions about iText here
> Subject: Re: [iText-questions] HTMLWorker - table cell width
>
> There's no workaround, it's just not there the feature. It was not needed
> initially and later I had other things to do.
>
> Paulo
>
> ----- Original Message -----
> From: "Ziga Janez" <ziga <at> marg.si>
> To: "Post all your questions about iText here"
> <itext-questions <at> lists.sourceforge.net>
> Sent: Friday, August 29, 2008 12:16 PM
> Subject: Re: [iText-questions] HTMLWorker - table cell width
>
> Hi,
>
> any other tricks or hacks to control cell width?
> I have tried with colspan, but in more complex HTML page I can not use this.
>
> Žiga
>
> -----Original Message-----
> From: itext-questions-bounces <at> lists.sourceforge.net
> [mailto:itext-questions-bounces <at> lists.sourceforge.net] On Behalf Of
> Paulo
> Soares
> Sent: Friday, August 29, 2008 12:25 PM
> To: Post all your questions about iText here
> Subject: Re: [iText-questions] HTMLWorker - table cell width
>
> Cell width is not supported in HTMLWorker.
>
> Paulo
>
> ----- Original Message -----
> From: "Ziga Janez" <ziga <at> marg.si>
> To: "Post all your questions about iText here"
> <itext-questions <at> lists.sourceforge.net>
> Sent: Friday, August 29, 2008 8:25 AM
> Subject: [iText-questions] HTMLWorker - table cell width
>
> Hi,
>
> How to cotrol table cell width when converting html to pdf with HTMLWorker.
> ParseToList().
> I have tried with:
>
> - st.LoadStyle("title", "width", "100px");
>
> - <td width="100">test</td>
>
> - <td style="width:100px"></td>
> searching for solution on googel and itext-questions archive, but not found
> working solution.
>
> Žiga
diff -Naur iText_2_1_7/src/core/com/lowagie/text/html/simpleparser/HTMLWorker.java iText/src/core/com/lowagie/text/html/simpleparser/HTMLWorker.java
--- iText_2_1_7/src/core/com/lowagie/text/html/simpleparser/HTMLWorker.java 2009-07-07 14:41:13.000000000 +0300
+++ iText/src/core/com/lowagie/text/html/simpleparser/HTMLWorker.java 2010-03-04 18:43:29.000000000 +0200
@@ -54,7 +54,10 @@
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
import java.util.Stack;
import java.util.StringTokenizer;
@@ -590,11 +593,15 @@
pendingTR = false;
cprops.removeChain("tr");
ArrayList cells = new ArrayList();
+ ArrayList cellWidths = new ArrayList();
IncTable table = null;
while (true) {
Object obj = stack.pop();
if (obj instanceof IncCell) {
- cells.add(((IncCell) obj).getCell());
+ IncCell cell = (IncCell) obj;
+ if(cell.getWidth() != null)
+ cellWidths.add(cell.getWidth());
+ cells.add(cell.getCell());
}
if (obj instanceof IncTable) {
table = (IncTable) obj;
@@ -602,6 +609,14 @@
}
}
table.addCols(cells);
+ if(cellWidths.size() > 0) {
+ // cells come off the stack in reverse, naturally
+ Collections.reverse(cellWidths);
+ int[] widths = new int[cellWidths.size()];
+ for(int i=0; i<widths.length; i++)
+ widths[i] = ((Integer)cellWidths.get(i)).intValue();
+ table.setColWidths(widths);
+ }
table.endRow();
stack.push(table);
skipText = true;
diff -Naur iText_2_1_7/src/core/com/lowagie/text/html/simpleparser/IncCell.java iText/src/core/com/lowagie/text/html/simpleparser/IncCell.java
--- iText_2_1_7/src/core/com/lowagie/text/html/simpleparser/IncCell.java 2009-07-07 14:41:13.000000000 +0300
+++ iText/src/core/com/lowagie/text/html/simpleparser/IncCell.java 2010-03-04 18:30:55.000000000 +0200
@@ -63,6 +63,7 @@
private ArrayList chunks = new ArrayList();
private PdfPCell cell;
+ private Integer width;
/** Creates a new instance of IncCell */
public IncCell(String tag, ChainedProperties props) {
@@ -102,6 +103,10 @@
cell.setUseDescender(true);
value = props.getProperty("bgcolor");
cell.setBackgroundColor(Markup.decodeColor(value));
+ value = props.getProperty("width");
+ if(value != null && value.endsWith("%")) {
+ width = new Integer(value.replace("%", ""));
+ }
}
public boolean add(Object o) {
@@ -141,5 +146,13 @@
*/
public boolean isNestable() {
return true;
- }
+ }
+
+ public Integer getWidth() {
+ return width;
+ }
+
+ public void setWidth(Integer width) {
+ this.width = width;
+ }
}
\ No newline at end of file
diff -Naur iText_2_1_7/src/core/com/lowagie/text/html/simpleparser/IncTable.java iText/src/core/com/lowagie/text/html/simpleparser/IncTable.java
--- iText_2_1_7/src/core/com/lowagie/text/html/simpleparser/IncTable.java 2009-07-07 14:41:13.000000000 +0300
+++ iText/src/core/com/lowagie/text/html/simpleparser/IncTable.java 2010-03-04 18:59:14.000000000 +0200
@@ -50,7 +50,9 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
+import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
@@ -62,6 +64,8 @@
private HashMap props = new HashMap();
private ArrayList rows = new ArrayList();
private ArrayList cols;
+ private int[] colWidths;
+
/** Creates a new instance of IncTable */
public IncTable(HashMap props) {
this.props.putAll(props);
@@ -92,7 +96,15 @@
return rows;
}
- public PdfPTable buildTable() {
+ public int[] getColWidths() {
+ return colWidths;
+ }
+
+ public void setColWidths(int[] colWidths) {
+ this.colWidths = colWidths;
+ }
+
+ public PdfPTable buildTable() {
if (rows.isEmpty())
return new PdfPTable(1);
int ncol = 0;
@@ -118,6 +130,13 @@
table.addCell((PdfPCell)col.get(k));
}
}
+
+ try {
+ if(colWidths != null)
+ table.setWidths(colWidths);
+ } catch (DocumentException e) {
+ // fail silently
+ }
return table;
}
}
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/