A question re: pdftron.PDF.Font.create(doc.getSdfObj(), "Arial Unicode", "Hello World! 官话 北方话")

421 views
Skip to first unread message

Support

unread,
Nov 21, 2012, 7:18:24 PM11/21/12
to pdfne...@googlegroups.com
Q:
 
I am using PDFNet on Android for PDF viewing and would alos like to add PDF generation capability (create extra PDF pages dynamically) and/or append content to existing pages. For stamping, I used pdftron.PDF.Stamper and it works great, however I need some more control and am planning to use ElementBuilder/ElementWriter. The code is as follows:
 
 

package com.example.pdfwritev2;

 

import pdftron.PDF.Element;

import pdftron.PDF.ElementBuilder;

import pdftron.PDF.ElementWriter;

import pdftron.PDF.Font;

import pdftron.PDF.PDFDoc;

import pdftron.PDF.PDFNet;

import pdftron.PDF.Page;

import pdftron.PDF.Rect;

import pdftron.SDF.Doc;

import pdftron.SDF.SDFDoc;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.view.Menu;

 

public class MainActivity extends Activity {

 

       @Override

       public void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              PDFNet.initialize(this);

              setContentView(R.layout.activity_main);

              String output_path = Environment.getExternalStorageDirectory().getPath() + "/";

 

              try {

                     PDFDoc doc = new PDFDoc();

 

                     ElementBuilder eb = new ElementBuilder();

                     ElementWriter writer = new ElementWriter();

 

                     // Start a new page ------------------------------------

                     Page page = doc.pageCreate(new Rect(0, 0, 612, 794));

 

                     writer.begin(page); // begin writing to this page

 

                     Font fnt = Font.create((Doc)doc.getSDFDoc(), "Arial Unicode", "Hello world, I am chess 汉语/漢語");

 

                     Element element = eb.createTextBegin(fnt, 1);

                     element.setTextMatrix(10, 0, 0, 10, 50, 600);

                     element.getGState().setLeading(2); // Set the spacing between lines

                     writer.writeElement(element);

 

                     String source = "Hello world, I am chess";

                     // Hello World!

                     writer.writeElement(eb.createUnicodeTextRun("Hello world. I am\n\n ches..."));

                     writer.writeElement(eb.createTextNewLine());

                     writer.writeElement(eb.createUnicodeTextRun(source));

                     writer.writeElement(eb.createTextNewLine());

                     // Finish the block of text

                     writer.writeElement(eb.createTextEnd());

 

                     writer.end(); // save changes to the current page

                     doc.pagePushBack(page);

 

                     doc.save((output_path + "unicodewrite.pdf"), SDFDoc.e_linearized, null);

                     doc.close();

                     System.out.println("Done. Result saved in unicodewrite.pdf...");

              } catch (Exception e) {

                     e.printStackTrace();

              }

 

              PDFNet.terminate();

 

       }

 

       @Override

       public boolean onCreateOptionsMenu(Menu menu) {

              getMenuInflater().inflate(R.menu.activity_main, menu);

              return true;

       }

}

 

However, I don't quite get the meaning of the third paramater to pdftron.PDF.Font.create():

 

  Font fnt = Font.create(doc.getSdfObj(), "Arial Unicode", "Hello World! 官话 北方话");

Based on the documentation, it says the third parameter is the charset of the characters, which, in my understanding, should be "UTF-8","GBK" and so on. Why it is "Hello World! 官话 北方话")?

----------------------- 

A:

 

pdftron.PDF.Font.create() was recently added (as part of v.5.9) and the documentation is a little confusing (we will improve that for the next version). The third parameter is not supposed to be ISO char-set (eg, UTF-8, ISO-8859,...). Instead it is a string that includes all characters that you will need with this font. This way you can make sure that the matched font will cover all the characters you need (which may not be the case with large character collections). PDFNet will try to find a system font that includes glyphs for all the required characters and that will ideally (but not necessarily) match the family name.

So, using Font fnt = Font.create((Doc)doc.getSDFDoc(), "Arial Unicode", "UTF-8") tells PDFNet to find a font that inclues glyphs for
"U", "T", "F", "-", and "8". If you need Chinese support there is possibility that the chosen font will not include support for Chinese characters.

 

Reply all
Reply to author
Forward
0 new messages