Bruno,
If you can see my code I will be very thanks, this Visitor visits the tags nodes from a html and convert it to a Document. How you can see there isn't Strings addeds with PdfContentByte and my problem persists.
public class ParserVisitor extends NodeVisitor {
private boolean negrito;
private boolean italico;
private boolean sublinhado;
private float tamFonte;
private int tipoFonte;
private LinkedList<com.lowagie.text.List> listaDeListas;
private ListItem li;
private LinkedList<LinkedList<Cell>> tabela;
private LinkedList<Cell> linhaDaTabela;
private Cell celula;
private boolean td = false;
private Paragraph paragraph;
private Document doc;
public ParserVisitor(Document doc) {
this.doc = doc;
tamFonte = 12;
tipoFonte = Font.NORMAL;
negrito = false;
italico = false;
sublinhado = false;
listaDeListas = new LinkedList<com.lowagie.text.List>();
li = null;
}
public void visitTag(Tag tag) {
if(tag.getTagName().compareToIgnoreCase("p") == 0) {
paragraph = new Paragraph("");
paragraph.setSpacingAfter(30);
} else if(tag.getTagName().compareToIgnoreCase("b") == 0) {
negrito = true;
} else if(tag.getTagName().compareToIgnoreCase("i") == 0) {
italico = true;
} else if(tag.getTagName().compareToIgnoreCase("u") == 0) {
sublinhado = true;
} else if(tag.getTagName().compareToIgnoreCase("ol") == 0) {
List list = new List(true,tamFonte);
list.setListSymbol(new Chunk("1", FontFactory.getFont("arial", tamFonte, Font.NORMAL)));
listaDeListas.add(list);
} else if(tag.getTagName().compareToIgnoreCase("ul") == 0) {
List list = new List(false,tamFonte);
list.setListSymbol(new Chunk("\u2022", FontFactory.getFont("arial", tamFonte, Font.BOLD)));
listaDeListas.add(list);
} else if(tag.getTagName().compareToIgnoreCase("li") == 0) {
li = new ListItem();
} else if(tag.getTagName().compareToIgnoreCase("table") == 0) {
tabela = new LinkedList<LinkedList<Cell>>();
} else if(tag.getTagName().compareToIgnoreCase("tr") == 0) {
linhaDaTabela = new LinkedList<Cell>();
} else if(tag.getTagName().compareToIgnoreCase("td") == 0) {
celula = new Cell();
td = true;
}
}
public void visitStringNode(Text string) {
if(string.getText().compareTo("") != 0) {
String text = formatText( string.toString());
Chunk chunk = new Chunk(text);
updateFontType();
chunk.setFont(FontFactory.getFont("arial",tamFonte,tipoFonte));
if(sublinhado)
chunk.setUnderline(1,-2);
if(li != null) {
li.add(chunk);
} else if(paragraph != null) {
paragraph.add(chunk);
} else if(td) {
try {
celula.addElement(chunk);
} catch (BadElementException ex) {
ex.printStackTrace();
}
}
}
}
public void visitEndTag(Tag tag) {
if(tag.getTagName().compareToIgnoreCase("p") == 0) {
try {
doc.add(paragraph);
} catch (DocumentException ex) {
ex.printStackTrace();
}
paragraph = null;
} else if(tag.getTagName().compareToIgnoreCase("b") == 0) {
negrito = false;
} else if( tag.getTagName().compareToIgnoreCase("i") == 0) {
italico = false;
} else if(tag.getTagName().compareToIgnoreCase("u") == 0) {
sublinhado = false;
} else if( tag.getTagName().compareToIgnoreCase("ol") == 0) {
if(listaDeListas.size() > 1) {
List lAux = listaDeListas.removeLast();
listaDeListas.getLast().add(lAux);
} else if(listaDeListas.size() == 1) {
try {
if(paragraph == null) {
doc.add(listaDeListas.removeFirst());
Paragraph p = new Paragraph("");
p.setSpacingAfter(10);
doc.add(p);
} else {
paragraph.add(listaDeListas.removeFirst());
}
} catch (DocumentException ex) {
ex.printStackTrace();
}
}
} else if(tag.getTagName().compareToIgnoreCase("ul") == 0) {
if( listaDeListas.size() > 1) {
List lAux = listaDeListas.removeLast();
listaDeListas.getLast().add(lAux);
} else if(listaDeListas.size() == 1) {
try {
if(paragraph == null) {
doc.add(listaDeListas.removeFirst());
Paragraph p = new Paragraph("");
p.setSpacingAfter (10);
doc.add(p);
} else {
paragraph.add(listaDeListas.removeFirst());
}
} catch (DocumentException ex) {
ex.printStackTrace();
}
}
} else if(tag.getTagName().compareToIgnoreCase("li") == 0) {
listaDeListas.getLast().add(li);
li = null;
} else if(tag.getTagName().compareToIgnoreCase("table") == 0) {
int tam = tabela.getFirst().size();
try {
Table table = new Table(tam);
for(LinkedList<Cell> linha : tabela) {
for(Cell celula : linha) {
int l = tabela.indexOf(linha);
int c = linha.indexOf(celula);
if(c == 0) {
if(l == 0) {
celula.setBorder(Rectangle.TOP | Rectangle.BOTTOM | Rectangle.RIGHT);
} else if(l == tabela.size()-1) {
celula.setBorder (Rectangle.BOTTOM | Rectangle.RIGHT);
} else {
celula.setBorder(Rectangle.RIGHT);
}
} else if(c == linha.size()-1) {
if(l == 0) {
celula.setBorder(Rectangle.TOP | Rectangle.BOTTOM | Rectangle.LEFT);
} else if(l == tabela.size ()-1) {
celula.setBorder(Rectangle.BOTTOM | Rectangle.LEFT);
} else {
celula.setBorder(Rectangle.LEFT);
}
} else {
if(tabela.indexOf(linha) == 0) {
celula.setBorder(Rectangle.TOP | Rectangle.BOTTOM | Rectangle.RIGHT | Rectangle.LEFT);
} else if(tabela.indexOf(linha) == tabela.size()-1) {
celula.setBorder(Rectangle.BOTTOM | Rectangle.RIGHT | Rectangle.LEFT);
} else {
celula.setBorder(Rectangle.RIGHT | Rectangle.LEFT);
}
}
celula.setHorizontalAlignment(Element.ALIGN_CENTER );
celula.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(celula, l, c);
}
}
table.setBorder(Rectangle.TOP | Rectangle.BOTTOM);
table.setAlignment(table.ALIGN_CENTER);
doc.add(table);
Paragraph p = new Paragraph("");
p.setSpacingAfter(30);
doc.add(p);
tabela = null;
} catch (BadElementException ex) {
ex.printStackTrace();
} catch (DocumentException ex) {
ex.printStackTrace ();
}
} else if(tag.getTagName().compareToIgnoreCase("tr") == 0) {
tabela.add(linhaDaTabela);
linhaDaTabela = null;
} else if(tag.getTagName().compareToIgnoreCase("td") == 0) {
celula.add(new Chunk("\n "));
linhaDaTabela.add(celula);
td = false;
}
}
private void updateFontType() {
if(!negrito && !italico) {
tipoFonte = Font.NORMAL;
} else if(negrito && !italico) {
tipoFonte = Font.BOLD;
} else if(!negrito && italico) {
tipoFonte = Font.ITALIC ;
} else if(negrito && italico) {
tipoFonte = Font.BOLDITALIC;
}
}
private boolean negrito;
private boolean italico;
private boolean sublinhado;
private float tamFonte;
private int tipoFonte;
private LinkedList<com.lowagie.text.List> listaDeListas;
private ListItem li;
private LinkedList<LinkedList<Cell>> tabela;
private LinkedList<Cell> linhaDaTabela;
private Cell celula;
private boolean td = false;
private Paragraph paragraph;
private Document doc;
public ParserVisitor(Document doc) {
this.doc = doc;
tamFonte = 12;
tipoFonte = Font.NORMAL;
negrito = false;
italico = false;
sublinhado = false;
listaDeListas = new LinkedList<com.lowagie.text.List>();
li = null;
}
public void visitTag(Tag tag) {
if(tag.getTagName().compareToIgnoreCase("p") == 0) {
paragraph = new Paragraph("");
paragraph.setSpacingAfter(30);
} else if(tag.getTagName().compareToIgnoreCase("b") == 0) {
negrito = true;
} else if(tag.getTagName().compareToIgnoreCase("i") == 0) {
italico = true;
} else if(tag.getTagName().compareToIgnoreCase("u") == 0) {
sublinhado = true;
} else if(tag.getTagName().compareToIgnoreCase("ol") == 0) {
List list = new List(true,tamFonte);
list.setListSymbol(new Chunk("1", FontFactory.getFont("arial", tamFonte, Font.NORMAL)));
listaDeListas.add(list);
} else if(tag.getTagName().compareToIgnoreCase("ul") == 0) {
List list = new List(false,tamFonte);
list.setListSymbol(new Chunk("\u2022", FontFactory.getFont("arial", tamFonte, Font.BOLD)));
listaDeListas.add(list);
} else if(tag.getTagName().compareToIgnoreCase("li") == 0) {
li = new ListItem();
} else if(tag.getTagName().compareToIgnoreCase("table") == 0) {
tabela = new LinkedList<LinkedList<Cell>>();
} else if(tag.getTagName().compareToIgnoreCase("tr") == 0) {
linhaDaTabela = new LinkedList<Cell>();
} else if(tag.getTagName().compareToIgnoreCase("td") == 0) {
celula = new Cell();
td = true;
}
}
public void visitStringNode(Text string) {
if(string.getText().compareTo("") != 0) {
String text = formatText( string.toString());
Chunk chunk = new Chunk(text);
updateFontType();
chunk.setFont(FontFactory.getFont("arial",tamFonte,tipoFonte));
if(sublinhado)
chunk.setUnderline(1,-2);
if(li != null) {
li.add(chunk);
} else if(paragraph != null) {
paragraph.add(chunk);
} else if(td) {
try {
celula.addElement(chunk);
} catch (BadElementException ex) {
ex.printStackTrace();
}
}
}
}
public void visitEndTag(Tag tag) {
if(tag.getTagName().compareToIgnoreCase("p") == 0) {
try {
doc.add(paragraph);
} catch (DocumentException ex) {
ex.printStackTrace();
}
paragraph = null;
} else if(tag.getTagName().compareToIgnoreCase("b") == 0) {
negrito = false;
} else if( tag.getTagName().compareToIgnoreCase("i") == 0) {
italico = false;
} else if(tag.getTagName().compareToIgnoreCase("u") == 0) {
sublinhado = false;
} else if( tag.getTagName().compareToIgnoreCase("ol") == 0) {
if(listaDeListas.size() > 1) {
List lAux = listaDeListas.removeLast();
listaDeListas.getLast().add(lAux);
} else if(listaDeListas.size() == 1) {
try {
if(paragraph == null) {
doc.add(listaDeListas.removeFirst());
Paragraph p = new Paragraph("");
p.setSpacingAfter(10);
doc.add(p);
} else {
paragraph.add(listaDeListas.removeFirst());
}
} catch (DocumentException ex) {
ex.printStackTrace();
}
}
} else if(tag.getTagName().compareToIgnoreCase("ul") == 0) {
if( listaDeListas.size() > 1) {
List lAux = listaDeListas.removeLast();
listaDeListas.getLast().add(lAux);
} else if(listaDeListas.size() == 1) {
try {
if(paragraph == null) {
doc.add(listaDeListas.removeFirst());
Paragraph p = new Paragraph("");
p.setSpacingAfter (10);
doc.add(p);
} else {
paragraph.add(listaDeListas.removeFirst());
}
} catch (DocumentException ex) {
ex.printStackTrace();
}
}
} else if(tag.getTagName().compareToIgnoreCase("li") == 0) {
listaDeListas.getLast().add(li);
li = null;
} else if(tag.getTagName().compareToIgnoreCase("table") == 0) {
int tam = tabela.getFirst().size();
try {
Table table = new Table(tam);
for(LinkedList<Cell> linha : tabela) {
for(Cell celula : linha) {
int l = tabela.indexOf(linha);
int c = linha.indexOf(celula);
if(c == 0) {
if(l == 0) {
celula.setBorder(Rectangle.TOP | Rectangle.BOTTOM | Rectangle.RIGHT);
} else if(l == tabela.size()-1) {
celula.setBorder (Rectangle.BOTTOM | Rectangle.RIGHT);
} else {
celula.setBorder(Rectangle.RIGHT);
}
} else if(c == linha.size()-1) {
if(l == 0) {
celula.setBorder(Rectangle.TOP | Rectangle.BOTTOM | Rectangle.LEFT);
} else if(l == tabela.size ()-1) {
celula.setBorder(Rectangle.BOTTOM | Rectangle.LEFT);
} else {
celula.setBorder(Rectangle.LEFT);
}
} else {
if(tabela.indexOf(linha) == 0) {
celula.setBorder(Rectangle.TOP | Rectangle.BOTTOM | Rectangle.RIGHT | Rectangle.LEFT);
} else if(tabela.indexOf(linha) == tabela.size()-1) {
celula.setBorder(Rectangle.BOTTOM | Rectangle.RIGHT | Rectangle.LEFT);
} else {
celula.setBorder(Rectangle.RIGHT | Rectangle.LEFT);
}
}
celula.setHorizontalAlignment(Element.ALIGN_CENTER );
celula.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(celula, l, c);
}
}
table.setBorder(Rectangle.TOP | Rectangle.BOTTOM);
table.setAlignment(table.ALIGN_CENTER);
doc.add(table);
Paragraph p = new Paragraph("");
p.setSpacingAfter(30);
doc.add(p);
tabela = null;
} catch (BadElementException ex) {
ex.printStackTrace();
} catch (DocumentException ex) {
ex.printStackTrace ();
}
} else if(tag.getTagName().compareToIgnoreCase("tr") == 0) {
tabela.add(linhaDaTabela);
linhaDaTabela = null;
} else if(tag.getTagName().compareToIgnoreCase("td") == 0) {
celula.add(new Chunk("\n "));
linhaDaTabela.add(celula);
td = false;
}
}
private void updateFontType() {
if(!negrito && !italico) {
tipoFonte = Font.NORMAL;
} else if(negrito && !italico) {
tipoFonte = Font.BOLD;
} else if(!negrito && italico) {
tipoFonte = Font.ITALIC ;
} else if(negrito && italico) {
tipoFonte = Font.BOLDITALIC;
}
}
}
2006/5/17, Bruno Lowagie <[EMAIL PROTECTED]>:
Davi Garcia Pereira wrote:
> When the size of a string exceds some limit the text of the String it
> isn't complete inserted, only a part and ...
>
> If you can help...
1) Mail to [email protected] (instead of to the list
admin).
2) This is not true for Strings added with Chunk, Phrase, Paragraph,...
This is normal for Strings added with showTextAligned (PdfContentByte).
Both ways of adding content has its own purpose.
It's explained in the online tutorial.
br,
Bruno
