How to write an UTF-8 character in pdf file using java?
I use netbeans, the following is the code. What should I add to my code that can display in the output pdf file?
import java.awt.Color;
import java.io.*;
import com.gnostice.pdfone.*;
import com.gnostice.pdfone.encodings.PdfEncodings;
import com.gnostice.pdfone.fonts.PdfFont;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import java.awt.Font;
public class createpdf {
public static void main(String[] args) throws PdfException, IOException, DocumentException {
create_sample_doc1();
get_pagecount_from_sample_doc1();
write_to_sample_doc1();
// NOTE: Requires Tahoma font file in the
// current directory
write_to_sample_doc1_using_fonts();
}
static void create_sample_doc1() throws IOException, PdfException {
// Create a PdfWriter instance
PdfWriter w = PdfWriter.fileWriter("C:\\Users\\Dell\\Desktop\\files\\sample_doc1.pdf");
// Create a PdfDocument instance with the PdfWriter
PdfDocument d = new PdfDocument(w);
// Write some text on page 1
d.writeText("السلام");
// Set output file to be displayed after it is
// written to
d.setOpenAfterSave(true);
// Write document to file
d.write();
// Close all I/O streams associated with the writer
w.dispose();
}
static void get_pagecount_from_sample_doc1() throws IOException,
PdfException {
// Create a PdfReader instance
PdfReader r = PdfReader.fileReader("C:\\Users\\Dell\\Desktop\\files\\sample_doc1.pdf");
// Create a PdfDocument instance with the reader
PdfDocument d = new PdfDocument(r);
// Get page count and display on console
System.out.println(
"Number of pages in sample_doc1.pdf is " +
d.getPageCount());
// Close all I/O streams associated with the reader
r.dispose();
}
static void write_to_sample_doc1() throws IOException, PdfException {
// Create a PdfReader instance
PdfReader r = PdfReader.fileReader(
"C:\\Users\\Dell\\Desktop\\files\\sample_doc1.pdf", // read from
"C:\\Users\\Dell\\Desktop\\files\\sample_doc2.pdf"); // write to
System.out.println("AFNAAAN");
// Create a PdfDocument instance with the reader
PdfDocument d = new PdfDocument(r);
// Write text at position (100, 100) on page 1
d.writeText("السلام",
100, // x-coordinate
50); // y-coordinate
// Set output file to be displayed after it is
// written to
d.setOpenAfterSave(true);
// Write to output file
d.write();
// Close all I/O streams associated with the reader
r.dispose();
}
static void write_to_sample_doc1_using_fonts() throws IOException, PdfException, DocumentException {
// Create a PdfReader instance
PdfReader r = PdfReader.fileReader(
"C:\\Users\\Dell\\Desktop\\files\\sample_doc1.pdf", // read from
"C:\\Users\\Dell\\Desktop\\files\\sample_doc3.pdf"); // write to
// Create a PdfDocument instance with the reader
PdfDocument d = new PdfDocument(r);
BaseFont bf = BaseFont.createFont("C:\\Windows\\Fonts\\Arial.ttf", BaseFont.IDENTITY_H, true);
bf.correctArabicAdvance();
// Create font objects
PdfFont fontArialItalic = PdfFont.create(
"C:\\Windows\\Fonts\\ArabicNaskhSSK.ttf", // name of installed font
PdfFont.ITALIC ,
18,
PdfEncodings.UTF_16BE);
PdfFont fontTahomaNormal = PdfFont.create(
"C:\\Windows\\Fonts\\ArabicNaskhSSK.ttf", // pathname of a font file
PdfFont.STROKE_AND_FILL,
48,
PdfEncodings.UTF_16BE);
// Write text on page 1 using the Arial font created above
d.writeText("السلام",
fontArialItalic, // font
100, 50);
// Set font properties
fontTahomaNormal.setStrokeWidth(2);
fontTahomaNormal.setStrokeColor(Color.RED);
fontTahomaNormal.setColor(Color.ORANGE);
// Write more text on page 1 using Tahoma
d.writeText("السلام",
fontTahomaNormal, // font
100, 100);
System.out.println("AFNAAAN5");
// Set output file to be displayed after it is
// written to
d.setOpenAfterSave(true);
// Write to output file
d.write();
// Close all I/O streams associated with the reader
r.dispose();
}
}
I think I have to use utf-8 somewhere but I don't now where! Please help.