BASKET
Search
PDFlib

text_output/process_utf8

Download Java Code      Show Output PDF

/* $Id: process_utf8.java,v 1.8 2013/02/20 10:35:49 stm Exp $

 * Process UTF-8 data:

 * Read text in the UTF-8 format and output it.

 *

 * Required software: PDFlib/PDFlib+PDI/PPS 9

 * Required data: UTF-8 encoded text file

 */

package com.pdflib.cookbook.pdflib.text_output;


import java.io.*;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class process_utf8 {

    public static void main(String argv[]) {

        pdflib p = null;

        String searchpath = "../input";

        String infile = "../input/kraxi_paper_planes.utf8";

        String outfile = "process_utf8.pdf";

        String title = "Process UTF-8";


        try {

            p = new pdflib();


            p.set_option("searchpath={" + searchpath + "}");


            /* This means we must check return values of load_font() etc. */

            p.set_option("errorpolicy=return");


            if (p.begin_document(outfile, "") == -1)

                throw new Exception("Error: " + p.get_errmsg());


            p.set_info("Creator", "PDFlib Cookbook");

            p.set_info("Title", title + " $Revision: 1.8 $");


            int font = p.load_font("Helvetica-Bold", "unicode", "");

            if (font == -1)

                throw new Exception("Error: " + p.get_errmsg());


            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");


            /* Read UTF-8 text data from a file line by line */

            FileInputStream fis = new FileInputStream(infile);

            InputStreamReader isr = new InputStreamReader(fis, "UTF8");

            BufferedReader br = new BufferedReader(isr);


            int fontsize = 12;

            p.set_text_option("font=" + font + " fontsize=" + fontsize);

            String line = null;


            double xpos = 50;

            double ypos = 700;

           

            while ((line = br.readLine()) != null) {

                p.fit_textline(line, xpos, ypos, "");

                ypos -= fontsize;

            }


            br.close();

            isr.close();

            fis.close();


            /*

             * Convert a UCS-2 string to UTF-8 (this is only to simulate a UTF-8

             * data source. You will usually read the UTF-8 data from a file or

             * database.)

             */

            String text = "Kraxi Paper Planes, Inc.\u00AE";

            byte[] utf8 = text.getBytes("UTF-8");


            /* Convert the string back from UTF-8 to UCS-2 */

            text = new String(utf8, "UTF-8");


            /*

             * In non Unicode capable languages use the "textformat" parameter

             * to notify the PDFlib text output functions to process strings as

             * UTF-8 strings.

             *

             * p.set_option("textformat=utf8");

             */


            /* Output the text */

            p.fit_textline(text, 50, 750, "font= " + font + " fontsize=14");


            p.end_page_ext("");


            p.end_document("");


        }

        catch (PDFlibException e) {

            System.err.print("PDFlib exception occurred:\n");

            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname()

                + ": " + e.get_errmsg() + "\n");

        }

        catch (Exception e) {

            System.err.println(e.getMessage());

        }

        finally {

            if (p != null) {

                p.delete();

            }

        }

    }

}