Reply from itext support: It looks to me like you're taking a very elaborate path towards rendering bold and italic text. Unless there is a very specific use case that absolutely requires Graphics2D, iText has no need for the kind of low-level API calls you are making. For instance, java.awt.Font is not needed at all in this example. What you're trying to do in this sample can be done in about 25 lines of code. If you really need the Graphics2D, please tell us more about your use case so we can understand your question better. On any Chunk, you can set a skew factor for italics, and set the TextRenderMode to TEXT_RENDER_MODE_FILL_STROKE, which gives a bold font. You can see these calls in the sample below: http://sourceforge.net/p/itext/code/HEAD/tree/book/src/part1/chapter03/FoobarFilmFestival.java#l146 http://sourceforge.net/p/itext/code/HEAD/tree/book/src/part1/chapter03/FoobarFilmFestival.java#l154 Since you can add a Phrase directly to a Document, you presumably won't need the Direct Content for adding text. If you follow this sample, the only thing you need to do to embed your font is to make sure you create it as follows: int fontSize = 12; BaseFont base = BaseFont.createFont("/path/to/arial.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED); Font arial = new Font(base, fontSize); On a side note: is there a reason why you're not using the newest version of iText, which is 5.5.3 ? Version 5.4.0 is almost 2 years old, and the newer versions contain fixes and greater functionality.
From: Harsha Kaundinya [mailto:hsk.1...@gmail.com] Sent: Tuesday, October 28, 2014 10:21 PM To: Post all your questions about iText here Subject: Re: [iText-questions] Can an Attributed String be written onto the PDF using iText? Hi Alexis, Yes, i agree that it is outdated. But our application has been using it from many years and probably, shortly, we will upgrade to the latest version. Anyhow, i have written a sample code by using PdfGraphics2D and FontMapper using iText 5.5.3. Attributed String is not rendered correctly when PdfGraphics2D is used. Graphics2D renders the same Attributed String correctly. But we have to embed the font, so only Graphics2D object is of no help. Is this a bug in PdfGraphics2D? Please see the below code. The output PDF document is attached. package com.attrstr.itext; import com.itextpdf.awt.FontMapper; import com.itextpdf.awt.PdfGraphics2D; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import com.itextpdf.text.pdf.PdfWriter; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.font.TextAttribute; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.text.AttributedString; import java.util.StringTokenizer; public class DemoAttrStringToPdf { public DemoAttrStringToPdf() { super(); } public static void main(String[] args) { DemoAttrStringToPdf demoAttrStringToPdf = new DemoAttrStringToPdf(); File f = new File(""); demoAttrStringToPdf.createPdf(f.getAbsolutePath() + "//Sample.pdf"); System.out.println(f.getAbsolutePath() + "\\Sample.pdf is generated.<file:///\\Sample.pdf%20is%20generated.>"); } public void createPdf(String filename) { ByteArrayOutputStream baos = null; OutputStream oos = null; Document document = null; PdfContentByte contentByte = null; PdfWriter writer = null; Graphics2D g2 = null; try { Rectangle rect = new Rectangle(288.0f, 216.0f); document = new Document(rect); writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); contentByte = writer.getDirectContent(); g2 = contentByte.createGraphicsShapes(document.getPageSize().getWidth(), document.getPageSize().getHeight()); contentByte.setRGBColorFill(0, 0, 0); g2.setColor(Color.black); java.awt.Font graphicsFont = new java.awt.Font("Arial", Font.PLAIN, 10); g2.setFont(graphicsFont); AttributedString attStr = new AttributedString("Attributed String to PDF"); attStr.addAttribute(TextAttribute.FONT, g2.getFont()); String strTextStyle = "11~16~fStyle~B;21~23~fStyle~I;"; if (strTextStyle != null && !strTextStyle.trim().equals("")) { String toPrintIndx = "0~23"; attStr = setAttributeProps(attStr, toPrintIndx, g2.getFont(), strTextStyle); } /** ARIAL */ FontMapper fontMapper = new FontMapper() { public BaseFont awtToPdf(java.awt.Font font) { try { BaseFont bFont = null; bFont = BaseFont.createFont("C:\\Windows\\Fonts\\ARIAL.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); return bFont; } catch (Exception e) { e.printStackTrace(); } return null; } public java.awt.Font pdfToAwt(BaseFont font, int size) { return null; } }; /** Writing throught PdfGraphics2D object */ Graphics2D graphics2D = new PdfGraphics2D(contentByte, document.getPageSize().getWidth(), document.getPageSize().getHeight(), fontMapper, false, false, 100.0f); graphics2D.drawString(attStr.getIterator(), 20, 50); graphics2D.dispose(); /** Writing throught Graphics2D object */ g2.drawString(attStr.getIterator(), 20, 100); g2.dispose(); document.close(); /** Below code Writes into a physical file **/ baos = new ByteArrayOutputStream(); PdfReader reader = new PdfReader(filename); PdfStamper stamper = null; try { stamper = new PdfStamper(reader, baos); } catch (DocumentException e) { e.printStackTrace(); } finally { try { if (stamper != null) { stamper.close(); } if (reader != null) { reader.close(); } } catch (DocumentException e) { e.printStackTrace(); } } byte[] buf = new byte[8192]; InputStream is = new FileInputStream(filename); oos = new FileOutputStream(filename); int c = 0; while ((c = is.read(buf, 0, buf.length)) > 0) { oos.write(buf, 0, c); oos.flush(); } baos.writeTo(oos); oos.flush(); oos.close(); } catch (Exception ex) { ex.printStackTrace(); } } public AttributedString setAttributeProps(AttributedString attrString, String toPrintIndx, java.awt.Font fontObj, String strTextStyle) { int toPrintIndxStart = Integer.parseInt(toPrintIndx.substring(0, toPrintIndx.indexOf("~"))); int toPrintIndxEnd = Integer.parseInt(toPrintIndx.substring(toPrintIndx.indexOf("~") + 1)); int toPrintIndxStart1 = toPrintIndxStart; toPrintIndxEnd = toPrintIndxEnd - toPrintIndxStart; toPrintIndxStart = 0; for (StringTokenizer stk = new StringTokenizer(strTextStyle, ";"); stk.hasMoreTokens(); ) { String txtStyle = stk.nextToken(); String[] splitStyle = txtStyle.split("~"); int intStart = Integer.parseInt(splitStyle[0]); int intEnd = Integer.parseInt(splitStyle[1]); intStart = intStart - toPrintIndxStart1; intEnd = intEnd - toPrintIndxStart1; int intX = 0; int intY = toPrintIndxEnd; boolean bSet = false; for (int i = toPrintIndxStart; i <= toPrintIndxEnd; i++) { if (i >= intStart && i <= intEnd && !bSet) { intX = i; bSet = true; } if (i == intEnd) { intY = intEnd; break; } } intStart = intX; intEnd = intY; if (bSet) { String styleType = splitStyle[3]; if (styleType.equals("B")) { fontObj = fontObj.deriveFont(java.awt.Font.BOLD); attrString.addAttribute(TextAttribute.FONT, fontObj, intStart, intEnd + 1); } else if (styleType.equals("I")) { fontObj = fontObj.deriveFont(java.awt.Font.ITALIC); attrString.addAttribute(TextAttribute.FONT, fontObj, intStart, intEnd + 1); } else if (styleType.equals("BI")) { fontObj = fontObj.deriveFont(java.awt.Font.BOLD | java.awt.Font.ITALIC); attrString.addAttribute(TextAttribute.FONT, fontObj, intStart, intEnd + 1); } else if (styleType.equals("P")) { fontObj = fontObj.deriveFont(java.awt.Font.PLAIN); attrString.addAttribute(TextAttribute.FONT, fontObj, intStart, intEnd + 1); attrString.addAttribute(TextAttribute.UNDERLINE, new Integer(-1), intStart, intEnd + 1); } else if (styleType.equals("U")) { attrString.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL, intStart, intEnd + 1); } } } return attrString; } } On Tue, Oct 28, 2014 at 6:25 PM, Alexis Pigeon <pigeon.ale...@gmail.com<mailto:pigeon.ale...@gmail.com>> wrote: Hi Harsha, iText 2.1.2 is more that 6 years old. It's technically and legally outdated. Unfortunately, you should not expect free support for such an old version. Regards, alexis On 28 October 2014 05:39, Harsha Kaundinya <hsk.1...@gmail.com<mailto:hsk.1...@gmail.com>> wrote: Hi all, We are using iText2.1.2 library for our application. We have a requirement of adding an AttributedString into a PDF. For example, we want to add a String like "Attributed String into PDF" into our PDF. We have seen an example of adding it using Graphics2D.drawString(AttrStr.iterator(), x,y). But this does not support the languages which are of RTL type and also does not embed the font into the PDF. Is there a way to use Attributed String in iText which also embeds fonts and supports RTL? Thanks in advance, Harsha ------------------------------------------------------------------------------ _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net<mailto:iText-questions@lists.sourceforge.net> https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php ------------------------------------------------------------------------------ _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net<mailto:iText-questions@lists.sourceforge.net> https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php LEGAL NOTICE: Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this e-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this e-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please delete this e-mail and inform the sender immediately. Laurus Infosystems (India) Private Limited, with its principal place of business at Plot No. 100-101, Export Promotion Industrial Park, Whitefield, Bangalore 560066.
------------------------------------------------------------------------------
_______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php