Android PDF Writer (APW) Enconding

I use Android PDF Write (APW) to create a PDF file, but it does not work with some special characters (Portuguese).

mypdf.addText(170, 50, 40,"Coração"); 

Standard application:

 mypdf.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING); outputToFile("helloworld.pdf",pdfcontent,"ISO-8859-1"); 

I tried

 outputToFile("helloworld.pdf",pdfcontent,"UTF-8"); outputToFile("helloworld.pdf",pdfcontent,"UTF-16"); outputToFile("helloworld.pdf",pdfcontent,"Cp1252"); 

and failed. Any ideas what should I do?

EDIT

The outputToFile method is defined as:

  private void outputToFile(String fileName, String pdfContent, String encoding) { File newFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName); try { newFile.createNewFile(); try { FileOutputStream pdfFile = new FileOutputStream(newFile); pdfFile.write(pdfContent.getBytes(encoding)); pdfFile.close(); } catch(FileNotFoundException e) { // } } catch(IOException e) { // } } 

The addText method is defined as:

  public void addText(int leftPosition, int topPositionFromBottom, int fontSize, String text, String transformation) { addContent( "BT\n" + transformation + " " + Integer.toString(leftPosition) + " " + Integer.toString(topPositionFromBottom) + " Tm\n" + "/F" + Integer.toString(mPageFonts.size()) + " " + Integer.toString(fontSize) + " Tf\n" + "(" + text + ") Tj\n" + "ET\n" ); } 

In addition, I change the font color to white by adding the following rawcontent:

 mypdf.addRawContent("1 1 1 rg\n"); 

Then I go back to the black color of the font:

  mypdf.addRawContent("0 0 0 rg\n"); 
0
android encoding pdf
source share
1 answer

I took all the information provided, wrote the following simple unit test method, and ran it.

 public void test19192108() { PDFWriter mPDFWriter = new PDFWriter(PaperSize.FOLIO_WIDTH, PaperSize.FOLIO_HEIGHT); mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING); mPDFWriter.addText(170, 50, 40,"Coração"); String pdfcontent = mPDFWriter.asString(); outputToFile("helloworld19192108.pdf",pdfcontent,"ISO-8859-1"); } 

( outputToFile is a helper method from the APW class PDFWriterDemo )

The result is as follows:

Screenshot of helloworld19192108.pdf in Adobe Reader

It is like meeting expectations.

Thus, depending on how it does not work with some special characters (Portuguese) for the OP, important information is missing to reproduce the problem.

PS: Depending on the development environment setting, there may be a problem with non-ASCII characters in the source code. So it would be nice to replace

  mPDFWriter.addText(170, 50, 40,"Coração"); 

from

  mPDFWriter.addText(170, 50, 40,"Cora\u00e7\u00e3o"); 

PPS: Adobe Reader after viewing the created file wants to restore it. The reason is that the cross lookup table is broken. Entries for generating code for this:

 public void addObjectXRefInfo(int ByteOffset, int Generation, boolean InUse) { StringBuilder sb = new StringBuilder(); sb.append(String.format("%010d", ByteOffset)); sb.append(" "); sb.append(String.format("%05d", Generation)); if (InUse) { sb.append(" n "); } else { sb.append(" f "); } sb.append("\r\n"); mList.add(sb.toString()); } 

(from CrossReferenceTable.java )

Counting the characters in this record, we get 10 + 1 + 5 + 3 + 2 = 21.

According to specification:

Each record must be exactly 20 bytes, including the end of line marker

(from section 7.5.4 Cross-reference table of ISO 32000-1 )

When using (current version) Android PDF Writer, you must also fix this code.

+3
source share

All Articles