Adding support to the new Rupee symbol to iTextPDF in Java - java

My project is related to a billing application and I am using the iTextPdf library for PDF file generation. However my requirement is to display the new Rupee symbol in the PDF generated by iTextPdf, instead of "Rs.".
I know that following Unicode \u20B9 is for the new Rupee symbol. I am using the following code for formatting:
String formater(String a) {
DecimalFormat formatter = new DecimalFormat("\u20B9 000");
return formatter.format(Double.parseDouble(a));
}
But the generated PDF file does not show any Rupee symbol. So how can I use that with the iTextPdf library? Is there any additional font required to be merged with the library itself?

changing in IDE is not the issue.
Your iTextPDF will be writing content into PDF in a particular character set. that controls if the data is shown properly or not.
you can try these 2 links
SO question for that contain how to check character set
iTextPdf site for how to correct character set

Thanks mkl, Naveen for the help.
Hope this can help someone, I did the following for this:
Step 1: Downloaded Font that has Rupee Symbol Unicode (for this I updated my windows (Windows Update and got the arial font with rupee symbol)
Step 2: Using iText I created the embedded base font with IDENTITY_H encoding:
BaseFont baseFont = BaseFont.createFont(this.getClass().getResource("arial.ttf").toString(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(baseFont);
Now the pdf generated has the new rupee symbol.

Related

Java iText7 UTF8 Characters [duplicate]

I am trying to create a pdf with greek characters using iText 7 for Java.
Only latin characters and numbers are visible in the PDF.
I am loading fonts using this code:
PdfFont normalFont = PdfFontFactory.createFont(FontConstants.HELVETICA, "CP1253");
What should I do?
This is the solution:
PdfFont normalFont = PdfFontFactory.createFont("C:\\Windows\\Fonts\\arial.ttf", "Identity-H", true);
You can use any font that supports your language. Also Identity-H seems to be important as the encoding of the PDF file.

Copy formatted text between PDF documents, empty unicode mapping for font

I'm using PDFBox 2.0.8 for pdf content extraction, converting it to JSON and then building a new document from created JSON (to clean possible vulnerabilities). I've extended PDFTextStripper class for getting font info:
PDFont font = textPosition.getFont() // it is embedded font
Now I'm trying to write just the same extracted character with its font to new pdf document:
contentStream.setFont(font, 16);
contentStream.showText(text);
and I'm getting java.lang.IllegalArgumentException: No glyph for U+004A in font HLOXAY+Birka-SemiBoldItalic exception on the second line.
The text I want to write is "John Whitington" from the third page of a "PDF Explained" book.
I've already read that it is because of current font doesn't have a Unicode mapping. But as I understand if this text is displayed in all readers there should be a way for copying it to another pdf.
I just want to full copy text and fonts info between documents.
Sorry if this duplicates any question here, but after a few days of searching, I still can't find an acceptable solution. Thanks in advance for any help.

IText Unicode Indic characters are not diplayed Properly

I am using Itext for PDF Generation.
I have Text In Marathi.
but Itext is generation that text without Glyph Substitution.
For Example :
Text Should Be : ल्ल
But iText Showing in PDF : लल
.
Same as
Text should be : क्ष्म
But iText Showing in PDF : कष
I have using windows Sytem file for font .e. arial unicode font.
And Code is
BaseFont base=BaseFont.createFont("c:/windows/fonts/ARIALUNI.ttf",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
i am using itext Version itext-5.4.3.jar.
Please give me solution.In what way i should procceed.
Thank you
You need to add unicode with your string when you're passing as input.The unicode for bangla or Hindi is
\u0986\u09ae\u09bf \u0995\u09cb\u09a8 \u09aa\u09a5\u09c7
\u0995\u09cd\u09b7\u09c0\u09b0\u09c7\u09b0 \u09b7\u09a8\u09cd\u09a1
\u09aa\u09c1\u09a4\u09c1\u09b2 \u09b0\u09c1\u09aa\u09cb
\u0997\u0999\u09cd\u0997\u09be \u098b\u09b7\u09bf
Here is the Example
I got Solution...
The solution is That IText currently not support (as per my searching) Indic languages such as Marathi.
It's language parser is yet not completely mature for indic Language.
As it is open source,i was using it.
Now,I have moved to Apache FOP 1.1.
It support indic Languages.But important con is that it is resource intensive.In other word, it is slower than IText.
Thank You...

Character encoding errors with DynamicReport

I'm having problems with character encodings with DynamicReports in Jasper Reports. I don't know where you should indicate the encoding. There are problems with accented characters. I have tried:
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); //CP1252
exporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, "UTF-8");
The screen capture linked to below shows that the characters are shown correctly in my code but not in the report. How can I set the encoding in the report correctly?
Today I had same problem, I will describe my solution.
My problem is not about encoding, it is about font.
DynamicReports create the pdf document with Helvetica font.
When I changed fontName to "DejaVu Serif", then problem was solved.
StyleBuilder myStyle= stl.style().setPadding(2);
myStyle.setFontName("DejaVu Serif");
TextColumnBuilder<Double> weightCol = col.column("Ağırlığı", "weight", type.doubleType());
weightCol.setStyle(myStyle);

OpenType font kerning with itext

I am using itext and ColdFusion (java) to write text strings to a PDF document. I have both trueType and openType fonts that I need to use. Truetype fonts seem to be working correctly, but the kerning is not being used for any font file ending in .otf. The code below writes "Line 1 of Text" in Airstream (OpenType) but the kerning between "T" and "e" is missing. When the same font is used in other programs, it has kerning. I downloaded a newer version of itext also, but the kerning still did not work. Does anyone know how to get kerning to work with otf fonts in itext?
<cfscript>
pdfContentByte = createObject("java","com.lowagie.text.pdf.PdfContentByte");
BaseFont= createObject("java","com.lowagie.text.pdf.BaseFont");
bf = BaseFont.createFont("c:\windows\fonts\AirstreamITCStd.otf", "" , BaseFont.EMBEDDED);
document = createobject("java","com.lowagie.text.Document").init();
fileOutput = createObject("java","java.io.FileOutputStream").init("c:\inetpub\test.pdf");
writer = createobject("java","com.lowagie.text.pdf.PdfWriter").getInstance(document,fileOutput);
document.open();
cb = writer.getDirectContent();
cb.beginText();
cb.setFontAndSize(bf, 72);
cb.showTextAlignedKerned(PdfContentByte.ALIGN_LEFT,"Line 1 of Text",0,72,0);
cb.endText();
document.close();
bf.hasKernPairs(); //returns NO
bf.getClass().getName(); //returns "com.lowagie.text.pdf.TrueTypeFont"
</cfscript>
according the socalled spec: http://www.microsoft.com/typography/otspec/kern.htm
OpenType™ fonts containing CFF outlines are not supported by the 'kern' table and must use the 'GPOS' OpenType Layout table.
I checked out the source, IText implementation only check the kern for truetype font, not read GPOS table at all, so the internal kernings must be empty, and the hasKernPairs must return false.
So, there have 2 way to solove:
get rid of the otf you used:)
patch the truetypefont by reading the GPosition table
wait for me, I'm processing the cff content, but PDF is optional of ever of my:) but not exclude the possibility:)
Have a look at this thread about How to use Open Type Fonts in Java.
Here is stated that otf is not supported by java (not even with iText). Otf support depends on sdk version and OS.
Alternatively you could use FontForge which converts otf to ttf.

Categories