Using iText 5.5.11 from the maven repo https://mvnrepository.com/artifact/com.itextpdf/itextpdf/5.5.11
public class test {
public static void main(String[] args) throws DocumentException, IOException {
final String text = "BMP: \u6d4b \u8bd5 Surrogate: \uD841\uDF0E \uD841\uDF31 \uD859\uDC02";
BaseFont baseFont = BaseFont.createFont("C:\\Windows\\Fonts\\arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(baseFont, 6.8f);
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("out.pdf"));
doc.open();
Paragraph p = new Paragraph();
p.add(new Phrase(text, font));
doc.add(p);
doc.close();
}
}
The non-surrogate characters in the basic multilingual plane are rendered on the resulting pdf, but the surrogate characters are not.
Edit: Also tried with font "STSong-Light" with encoding "UniGB-UCS2-H" (as in examples in book). Same result - surrogate characters missing.
Edit2: Got it to work with "SimSun-ExtB" font
This is usually a sign that the font being used (in this case Arial) does not have the glyphs for your characters.
I am using iText PDF to create PDF files in android. I need to write the '₹' symbol in the PDF.
I have the following code:-
public static final String FONT1 = "/main/assets//PlayfairDisplay-Regular.ttf";
public static final String FONT2 = "/main/assets/PT_Sans-Web-Regular.ttf";
public static final String FONT3 = "/main/assets/FreeSans.ttf";
public static final String RUPEE = "The Rupee character \u20B9 and the Rupee symbol \u20A8";
These are declared as class variables.
I have a function createPDF() which writes to the PDF document.
I have the following lines of code in the function:-
File pdfFile = new File(filePath);
OutputStream output = new FileOutputStream(pdfFile);
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, output);
document.open();
Font f1 = FontFactory.getFont(FONT1, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f2 = FontFactory.getFont(FONT2, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f3 = FontFactory.getFont(FONT3, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f4 = FontFactory.getFont(FONT3, BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
document.add(new Paragraph(RUPEE, f1));
document.add(new Paragraph(RUPEE, f2));
document.add(new Paragraph(RUPEE, f3));
document.add(new Paragraph(RUPEE, f4));
document.close();
However neither the font gets reflected in the PDF nor the Rupee Symbol.
My PDF looks as below:-
I have followed steps using the below links:-
iText Developers Tutorial
and StackOverflow Question on where to place Assets folder
I am aware of another similar question on SO:-
Rupee symbol is not showing in android
however this also has not helped me.
Am I doing anything wrong here ? Are my fonts placed in the wrong location?
Try,
Remove all /main/ just keep like assets/PlayfairDisplay-Regular.ttf so on... and in the first font you have // remove that one too and try
I need help with iText I look at some Google result and some here but don't find anything that work for me. I need to use polish character in my pdf but i got nothing for no. Here is a code that I think is important if need something else write in comment:
private static Font bigFont = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
another
Paragraph par = new Paragraph(Łabadzak, bigFont);
Can any1 tell me what to do to make that Ł visible in pdf and other polish character
UPDATE
I fund this but dunno how to use it for my project
Polish character in itext PDF
You need a unicode font. Here is an example:
BaseFont bf = BaseFont.createFont("arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Paragraph p = new Paragraph("Şinasi ıssız ile ağaç", new Font(bf, 22));
document.add(p);
http://abdullahakay.blogspot.com/2011/11/java-itext-unicode.html
EDIT:
Here, the font file name arialuni.tff is a static resource directly under /src/main/resources/ and can be any Unicode Font File of your choice. Here's a list of free Unicode Font Files available online.
It depends on used font and encoding. i found something like this:
http://itext-general.2136553.n4.nabble.com/Polish-National-Characters-are-not-getting-displayed-in-the-PDF-created-by-iTExt-td2163833.html
There is example like this:
BaseFont bf = BaseFont.createFont("c:/windows/fonts/arial.ttf",
BaseFont.CP1250, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
String polish = "\u0104\u0105\u0106\u0107\u0118\u0119";
document.add(new Paragraph(polish, font));
Remember that some fonts does not contain polish national characters.
In case you are using com.itextpdf.kernel package you can use any encoding which is not present in PdfEncodings class
PdfWriter writer ;
writer = new PdfWriter( dest ) ;
PdfDocument pdf = new PdfDocument( writer ) ;
Document document = new Document( pdf ) ;
FontProgram fontProgram = FontProgramFactory.createFont( ) ;
PdfFont font = PdfFontFactory.createFont( fontProgram, "Cp1254" ) ;
document.setFont( font );
for turkish characters I used "Cp1254"
I'm using PDFBox and iText to create a simple (just paragraphs) pdf document from various languages. Something like :
pdfBox:
private static void createPdfBoxDocument(File from, File to) {
PDDocument document = null;
try {
document = new TextToPDF().createPDFFromText(new FileReader(from));
document.save(new FileOutputStream(to));
} finally {
if (document != null)
document.close();
}
}
private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDType1Font font = PDType1Font.TIMES_ROMAN;
contentStream.setFont(font, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 400);
contentStream.drawString("š");
contentStream.endText();
contentStream.close();
document.save("test.pdf");
document.close();
}
itext:
private static Font blackFont = new Font(Font.FontFamily.COURIER, 12, Font.NORMAL, BaseColor.BLACK);
private static void createITextDocument(File from, File to) {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(to));
document.open();
addContent(document, getParagraphs(from));
document.close();
}
private static void addContent(Document document, List<String> paragraphs) {
for (int i = 0; i < paragraphs.size(); i++) {
document.add(new Paragraph(paragraphs.get(i), blackFont));
}
}
The input files are encoded in UTF-8 and some languages of Unicode char set, like Russian alphabet etc., are not rendered properly in pdf. The Fonts in both libraries don't support Unicode charset I suppose and I can't find any documentation on how to add and use third party fonts. Could please anybody help me out with an example ?
If you are using iText, it has quite good support.
In iText in Action (chapter 2.2.2) you can read more.
You have to download some unicode Fonts like arialuni.ttf and do it like this :
public static File fontFile = new File("fonts/arialuni.ttf");
public static void createITextDocument(File from, File to) throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(to));
document.open();
writer.getAcroForm().setNeedAppearances(true);
BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
FontSelector fs = new FontSelector();
fs.addFont(new Font(unicode));
addContent(document, getParagraphs(from), fs);
document.close();
}
private static void addContent(Document document, List<String> paragraphs, FontSelector fs) throws DocumentException {
for (int i = 0; i < paragraphs.size(); i++) {
Phrase phrase = fs.process(paragraphs.get(i));
document.add(new Paragraph(phrase));
}
}
arialuni.ttf fonts work for me, so far I checked it support for
BG, ES, CS, DA, DE, ET, EL, EN, FR, IT, LV, LT, HU, MT, NL, PL, PT, RO, SK, SL, FI, SV
and only PDF in Romanian language wasn't created properly...
With PDFBox it's almost the same:
private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDFont font = PDTrueTypeFont.loadTTF(document, "fonts/arialuni.ttf");
contentStream.setFont(font, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 400);
contentStream.drawString("š");
contentStream.endText();
contentStream.close();
document.save("test.pdf");
document.close();
}
However as Gagravarr says, it doesn't work because of this issue PDFBOX-903 . Even with 1.6.0-SNAPSHOT version. Maybe trunk will work. I suggest you to use iText. It works there perfectly.
You may find this answer helpful - it confirms that you can't do what you need with one of the standard type 1 fonts, as they're Latin1 only
In theory, you just need to embed a suitable font into the document, which handles all your codepoints, and use that. However, there's at least one open bug with writing unicode strings, so there's a chance it might not work just yet... Try the latest pdfbox from svn trunk too though to see if it helps!
In my project, I just copied the font that supported UTF8 (or whatever language you want) to a directory (or you can used Windows fonts path) and add some code, it looked like this
BaseFont baseFont = BaseFont.createFont("c:\\a.ttf", BaseFont.IDENTITY_H,true);
Font font = new Font(baseFont);
document.add(new Paragraph("Not English Text",font));
Now, you can use this font to show your text in various languages.
//use this code.Sometimes setfont() willnot work with Paragraph
try
{
FileOutputStream out=new FileOutputStream(name);
Document doc=new Document();
PdfWriter.getInstance(doc, out);
doc.open();
Font f=new Font(FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
Paragraph p=new Paragraph("New PdF",f);
p.setAlignment(Paragraph.ALIGN_CENTER);
doc.add(p);
doc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
Does anyone know an easy way to include the greater than or equal symbol in an iText PDF document without resorting to custom fonts?
You need to add the unicode for this character. Also make sure that the char is included in the font you use.
document.add( new Paragraph("\u2265"));
Use the below code for the same. It's work for me.
//symbol for greater or equal then
public void process() throws DocumentException, IOException {
String dest = "/home/ashok/ashok/tmp/hello.pdf";
BaseFont bfont = BaseFont.createFont(
"/home/ashok/ashok/fonts/Cardo-Regular.ttf",
BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
Font font = new Font(bfont, 12);
try {
Document document=new Document();
PdfWriter.getInstance(document,new
FileOutputStream("/home/ashok/ashok/tmp/hello.pdf"));
document.open();
Chunk chunk = new Chunk("\u2265");
Paragraph p = new Paragraph("Mathematical Operators are ",font);
p.add(chunk);
document.add(p);
p = new Paragraph(" ",font);
chunk = new Chunk("\u2264");
p.add(chunk);
document.add(p);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}