-1

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.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
afnanLamia
  • 7
  • 1
  • 2

3 Answers3

1

According the API of Gnotice PDFOne: you must use :

pdfDocument.write("someText", aFont)

where aFont can be created with this factory method:

PdfFont.create(name, size, encoding)

where encoding is an int representing one of the supported PdfEncodings

Unfortunately, UTF-8 is not supported by this library. I guess you will have to use another encoding (like UTF-16)... or another library.

ben75
  • 29,217
  • 10
  • 88
  • 134
0

You are using a commercial software library, Gnostice PDFOne, tt. o create a PDF. Apparently, it has a problem, limitation, complexity, with Arabic text. You would probably do better asking their tech support for advice, presumably paying them for a licenses entitles you to some of that. UTF-8 as nothing to do with it; their API apparently accepts full Java String values, so it is up to them to encode them correctly in the PDF file.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
-1

you can use from this link here

you have to change your default font, because the default font can't write utf8. I hope my text is usefull.

mehnet ali
  • 73
  • 3
  • 12