Java iText7 UTF8 Characters [duplicate] - java

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.

Related

PdfBox write hindi characters in pdf file

I tried many things to write hindi characters using Apache PdfBox but seems its existing issue in the library.
I tried many font files available, Can someone really help me out in this.
I tried following :
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDFont font = PDTrueTypeFont.loadTTF( doc, new FileInputStream(new File("D:\\Data\\fonts\\dn.ttf")));
font.setFontEncoding(new WinAnsiEncoding());
PDPageContentStream content = new PDPageContentStream( doc, page, true, false );
content.setFont(font, 10);
content.beginText();
content.moveTextPositionByAmount( 200, 100 );
content.drawString( "हिंदी" ); // Writing word "Hindi" in hindi language.
content.endText();
content.close();
doc.save( new FileOutputStream(new File("D:\\testOutput1.pdf")));
doc.close();
It's working for me in PDFBox.
The trick here is to use non-unicode string instead of unicode string.
Use Kruti Dev Font given in below link.
Then convert your unicode string to non-unicode string.
And finally use that converted string in your code.
That means replace this like
content.drawString( "हिंदी" ); // Writing word "Hindi" in hindi language.
With this line
content.drawString( "fganh" ); // Writing word "Hindi" in hindi language.
Convert Unicode (Mangal) To Kruti Dev Font
I think this cannot be done using PdfBox as there are lot of issues with it.
I tried many fonts and the encoding types of PdfBox but failed to write in Hindi.
At the end I tried it in Node Js express pdfmaker() which converts Html to PDF, However I had issues on my Linux server and I installed appropriate ttf font and it worked !

java - generate unicode pdf with Apache PDFBox

I have to generate pdf in my spring mvc application. recently I tested iTextPdf library, but i could not generate unicode pdf document. in fact I didn't see non-latin characters in the generated document. I decided to use Apache PDFBox for my purpose, but I don't know has it support unicode characters? If has, is there any good tutorial for learning pdfBox? And If not, which library should I use?
Thanks in advance.
The 1.8.* versions don't support PDF generation with Unicode, but the 2.0.* versions do. This is the example EmbeddedFonts.java:
public class EmbeddedFonts
{
public static void main(String[] args) throws IOException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
String dir = "../pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/";
PDType0Font font = PDType0Font.load(document, new File(dir + "LiberationSans-Regular.ttf"));
PDPageContentStream stream = new PDPageContentStream(document, page);
stream.beginText();
stream.setFont(font, 12);
stream.setLeading(12 * 1.2);
stream.newLineAtOffset(50, 600);
stream.showText("PDFBox Unicode with Embedded TrueType Font");
stream.newLine();
stream.showText("Supports full Unicode text ?");
stream.newLine();
stream.showText("English русский язык Tiếng Việt");
stream.endText();
stream.close();
document.save("example.pdf");
document.close();
}
}
Note that unlike iText, PDFBox support for PDF creation is very low level, i.e. we don't support paragraphs or tables out of the box. There is no tutorial, but a lot of examples. The API orients itself on the PDF specification.
The current version of Apache PDFBox can't deal with Unicode, see:
https://pdfbox.apache.org/ideas.html
iTextPdf v. 5.x generates pdf files with Unicode. There is an exemple here:
iText in Action: Chapter 11: Choosing the right font
part3.chapter11.UnicodeExample
http://itextpdf.com/examples/iia.php?id=199
To run it, you just need to adapt the value of EncodingExample.FONT and to add some code to create the output file.

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...

Adding support to the new Rupee symbol to iTextPDF in 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.

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