Hi all,
I've found some questions about the problem, but no working solution (in
the web or the itext-qustions-archive):
I have a problem by painting a polish text into the graphics-object
(created with pdfTemplate.createGraphics(...)):
I'm getting the texts from the database; texts should be encoded in
ISO-8859-2. I change that by using a CharsetDecoder.
While itext prints this pretty using a a Paragraph, and Java in a simple
awt-App too, i cant't display some Characters with the "itext-Graphics".
Consider that I'm checking, whether the used Font "canDisplay" the
Character.
The Font says: yes, but the Charcers are not shown. I've appended the
sample - results.
I would be pleased to receive a solution. Thanks!
Here's the Code:
---
package test.itext;
import java.awt.Color;
...
public class Polish extends JFrame {
private static final long serialVersionUID = 7190430058032118108L;
public static void main(String[] args) throws Exception {
new Polish().create();
} private Collection<String> _values;
private void create() throws Exception {
Document document = new Document();
PdfWriter mainWriter= PdfWriter.getInstance(document,
new FileOutputStream("c:/tmp/polish.pdf"));
document.open(); Class.forName("com.mysql.jdbc.Driver");
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
Collection<String> values= new ArrayList<String>();
try {
con= DriverManager.getConnection(
"jdbc:mysql://localhost/" +
"branch_test", "root", "root");
stmt= con.createStatement();
rs= stmt.executeQuery("SELECT TITEL_PL FROM PROZESS " +
"WHERE TITEL_PL <> ''");
BaseFont baseFont=BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1250, BaseFont.NOT_EMBEDDED) ;
Font polishFont= new Font(baseFont, 8, Font.NORMAL);
Charset charset = Charset.forName("ISO-8859-2");
CharsetDecoder decoder = charset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
while (rs.next()) {
String val= rs.getString(1);
if(val==null) {
continue;
}
String value= decoder.decode(ByteBuffer.wrap(
val.getBytes())).toString();
Paragraph p= new Paragraph(value, polishFont);
document.add(p);
values.add(value);
}
}
finally {
rs.close();
stmt.close();
con.close();
}
PdfContentByte cb = mainWriter.getDirectContent();
PdfTemplate tp= cb.createTemplate(400, 300);
DefaultFontMapper mapper= new DefaultFontMapper();
Graphics2D g = tp.createGraphics(400, 300, mapper);
cb.saveState();
int start= 20;
g.setColor(Color.BLUE);
java.awt.Font font= new java.awt.Font("Helvetica",
java.awt.Font.BOLD, 12);
g.setFont(font);
for (String text : values) {
boolean can= true;
for (int i = 0; i < text.length(); i++) {
if(!font.canDisplay(text.charAt(i))) {
can= false;
}
}
g.drawString(text + " / " + can, 20, start);
start+= 15;
}
int templateY= (int) (655 - 300);
cb.addTemplate(tp, 20, templateY);
g.dispose();
cb.restoreState();
document.close();
crateView(values);
}
private void crateView(Collection<String> values) {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
pack();
setSize(new Dimension(400, 300));
_values= values;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.draw3DRect(5, 5, 380, 280, true);
g.setColor(Color.WHITE);
g.fill3DRect(5, 5, 380, 280, true);
int start= 50;
g.setColor(Color.BLACK);
for (String text : _values) {
g.drawString(text, 20, start);
start+= 15;
}
}
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
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/