Genjuro Genjuro - 15 days ago 7
Java Question

printing arabic characters with pdfView api

i'm having an issue with printing a document in arabic using the

com.sun.pdfview.PDFPrintPage
api.
the arabic characters are either blank or "#" .
here the code :

final PrinterJob pjob = PrinterJob.getPrinterJob();
pjob.setJobName(docName);
final Book book = new Book();
final PDFPrintPage pages = new PDFPrintPage(curFile);
book.append(pages, pformat, curFile.getNumPages());

pjob.setPageable(book);
if (pjob.printDialog()) {
new PrintThread(pages, pjob).start();
}


is there any way i can print arabic characters by tweaking the current api , or should i move to another Api?

thanks

Answer

I don't know this API, but I had difficulties once with Apache PdfBox while attempting to print Japanese kanji characters.

I solved the issue simply by selecting a font that would support those characters.

You might want to try selecting an appropriate font in package com.sun.pdfview.font or com.sun.pdfview.font.ttf.


Edit: example with Apache PdfBox

  • Download the Apache PdfBox binaries and reference them in you project
  • Add a ttf Unicode font to your project's resources
  • Change the constants for font and output file to match your settings
  • You might need to raise the heap space allowed to your IDE
  • Check out the Apache PdfBox notes about Arabic writing

package test;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;

public class Main {
    // TODO adapt 1st two constants
    private static final String FILE_OUTPUT_PATH = "yourPath/yourFile.pdf"; 
    private static final String FONT_PATH = "test/yourUnicodeFont.ttf";
    private static final String EXAMPLE = "\u060F";

    public static void main(String[] args) {

        // if the following line returns garbage characters,
        // make sure you set your project (and console output) to UTF-8
        System.out.println(EXAMPLE);
        PDDocument document = null;
        try {
            // generates custom document
            document = new PDDocument();
            // gets the Arial.ttf font you placed in your src/test folder
            InputStream fontStream = Main.class.getClassLoader()
                    .getResourceAsStream(FONT_PATH);
            // loads it in the doc
            PDFont font = PDTrueTypeFont.loadTTF(document, fontStream);

            // closes the stream
            fontStream.close();
            // initializing a new PDF page
            PDPage page = new PDPage();
            // initializing a page content stream
            PDPageContentStream stream = new PDPageContentStream(document,
                    page, true, false);
            // assigining font to page content stream
            stream.setFont(font, 24);
            // setting color
            stream.setNonStrokingColor(Color.BLACK);
            // starts drawing text
            stream.beginText();
            // draws something in Arabic    
            stream.drawString(EXAMPLE);
            // stops drawing text
            stream.endText();
            // closes stream
            stream.close();
            // imports the page into the doc
            document.importPage(page);
            // creating file
            File file = new File(FILE_OUTPUT_PATH);
            file.createNewFile();
            // creating output stream
            OutputStream outputStream = new FileOutputStream(file);
            // saving doc content to stream
            document.save(outputStream);
            // flushing stream
            outputStream.flush();
            // closing stream
            outputStream.close();
        }
        // oops! something wrong happened, see stack trace
        catch (Throwable t) {
            t.printStackTrace();
        }
        finally {
            // closing doc
            if (document != null) {
                try {
                    document.close();
                }
                // oops!
                catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        }
    }
}

Finally, this blew up for me while parsing the ttf (I was using a 32MB Arial Unicode version and my machine just couldn't handle it).

I think you'll need some additional research on the right font to use.

Best of luck!

Comments