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."); } 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> 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> 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 >> 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 > 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 >
Sample.pdf
Description: Adobe PDF document
------------------------------------------------------------------------------
_______________________________________________ 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