Trying to write heart symbol in pdf through java code .
This is my input to pdf : ❤️❤️❤️
But pdf generated is empty without anything written.
Using itext to write to pdf.
The Font used is tradegothic_lt_boldcondtwenty.ttf
OutputStream file = new FileOutputStream(fileName);
Document document = new Document(PageSize.A6);
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
PdfLayer nested = new PdfLayer("Layer 1", writer);
PdfContentByte cb = writer.getDirectContent();
cb.beginLayer(nested);
ColumnText ct = new ColumnText(cb);
Font font = getFont();
Phrase para1 = new Phrase("❤️❤️❤️",font);
ct.setSimpleColumn(para1,38,0,260,138,15, Element.ALIGN_LEFT);
ct.go();
cb.endLayer();
document.close();
file.close();
private Font getFont() {
final String methodName = "generatePDF";
LOGGER.entering(CLASSNAME, methodName);
Font font = null;
try {
String filename = tradegothic_lt_boldcondtwenty.ttf;
FontFactory.register(filename, filename);
font = FontFactory.getFont(filename, BaseFont.CP1252, BaseFont.EMBEDDED,11.8f);
} catch(Exception exception) {
LOGGER.logp(Level.SEVERE, CLASSNAME, methodName, "Exception Occurred while fetching the Trade Gothic font." + exception);
font = FontFactory.getFont(FontFactory.HELVETICA_BOLD,11.8f);
}
return font;
}
Phrase para1 has the heart correctly. But not able to see in pdf
I want to create report in Arabic using PDFBox I have saw different solution on stackoverflow but cant be get solution for my problem yet.
the arabic word came as an character in reverse order how to fix it if you have any example please help me here is my code.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String relativeWebPath = "/font/arial.ttf";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
File file = new File(absoluteDiskPath);
System.out.print(file);
ByteArrayOutputStream output=new ByteArrayOutputStream();
PDDocument document=new PDDocument();
PDFont font = PDType0Font.load(document, new File(absoluteDiskPath));
PDPage test=new PDPage();
document.addPage(test);
PDPageContentStream content=new PDPageContentStream(document, test);
final String EXAMPLE = "النص العربي";
System.out.print(EXAMPLE);
content.beginText();
content.newLineAtOffset(50, 680);
content.setFont(font, 12);
content.showText(EXAMPLE);
System.out.print(EXAMPLE);
content.endText();
content.close();
PDFTextStripper textStripper = new PDFTextStripper();
String Text = textStripper.getText(document);
System.out.print(Text);
document.save(output);
document.close();
response.setContentType("application/pdf;base64,BASE_64_ENCODED_PDF");
response.addHeader("Content-Disposition", "inline; filename=\"TestReport.pdf\"");
response.getCharacterEncoding();
response.getOutputStream().write(output.toByteArray());
}
For support for bidirectional languages like Arabic. PDFBox uses the ICU4J library from the International Components for Unicode (ICU) project to support such languages in PDF documents. You need add the ICU4J jar to your project.
I'm trying to export Vietnamese characters to PDF using iText. I tried to use
BaseFont bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
It displays some unicode characters correctly, for example Russian, but not the ones with accents in Vietnamese language (ạ,ã,ố etc.).
Here's the class I wrote:
public class PDFMaker {
private final static String FILE = "FilePdf.pdf";
public static File fontFile = new File("fonts/arialuni.ttf");
public static void makePDF() throws IOException{
try{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
BaseFont bf = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf,15);
document.open();
document.add(new Paragraph("Đại học bách khoa Hà Nội", font));
document.close();
} catch (FileNotFoundException | DocumentException e) {
e.printStackTrace(System.out);
}}
It displays: Đi hc bách khoa Hà Ni. Please help.
The characters aren't shown, because MS Arial Unicode doesn't know those characters. You need to use another font. For instance: I downloaded a package of Vietnames fonts from SourceForge and I replaced arialuni.ttf in your code sample with vuArial.ttf (found in the downloaded package). When using that font, all the characters were visible.
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();
}
}