-1

I'm trying to write a Java code to manually create a PDF file, I don't need an API, the PDF structure is simple enough to create it myself, but this was until I confronted the complexity of introducing Arabic Unicode characters.

I'm using a text editor to tamper with a sample PDF file I took from Make your own PDF file – Hello World.

Now I want to use the same Helvetica font provided with Adobe and I want to (using only a text editor) write in Arabic letters a simple Hello World PDF file that I can use as a sample to build upon.

Here is the sample I'm working on:

%PDF-1.4
1 0 obj <<
   /Type /Catalog
   /Pages 1000000 0 R
   /Outlines 900000 0 R
   /PageMode /UseOutlines
>>
endobj

2 0 obj <<
   /Type /Font
   /Subtype /Type1
   /BaseFont /Helvetica
>>
endobj

3 0 obj <<
   /Type /Font
   /Subtype /Type1
   /BaseFont /Helvetica-Bold
>>
endobj

4 0 obj <<
   /Font <<
      /F1 2 0 R
      /F2 3 0 R
   >>
>>
endobj

5 0 obj <<
   /Title (Cover)
   /Dest [6 0 R /XYZ null 841.890 null]
   /Parent 900000 0 R
   /Next 10 0 R
>>
endobj

6 0 obj <<
   /Type /Page
   /Parent 1000000 0 R
   /Resources 4 0 R
   /Contents 7 0 R
>>
endobj

7 0 obj <<
   /Length 44
>>
stream
BT /F2 24 Tf 175 720 Td (Cover)Tj ET
BT /F1 24 Tf 175 696 Td (Hello World!)Tj ET
BT /F1 24 Tf 175 672 Td (Hello World!)Tj ET
endstream
endobj

10 0 obj <<
   /Title (Content)
   /Dest [11 0 R /XYZ null 841.890 null]
   /Parent 900000 0 R
   /Prev 5 0 R
   /Next 15 0 R
>>
endobj

11 0 obj <<
   /Type /Page
   /Parent 1000000 0 R
   /Resources 4 0 R
   /Contents 12 0 R
>>
endobj

12 0 obj <<
   /Length 44
>>
stream
BT /F2 24 Tf 175 720 Td (Content)Tj ET
BT /F1 24 Tf 175 696 Td (Hello World!)Tj ET
BT /F1 24 Tf 175 672 Td (Hello World!)Tj ET
endstream
endobj

900000 0 obj <<
   /First 5 0 R
   /Last 10 0 R
   /Count 2
>>
endobj

1000000 0 obj <<
   /Type
   /Pages
   /MediaBox [0 0 595.276 841.890]
   /Count 2
   /Kids [
      6 0 R
      11 0 R
   ]
>>
endobj

xref
0 7
0000000000 65535 f
0000000009 00000 n
0000000056 00000 n
0000000094 00000 n
0000000161 00000 n
0000000216 00000 n
0000000317 00000 n
trailer <<
   /Size 7
   /Root 1 0 R
>>
startxref
100
%%EOF
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
asaeles
  • 66
  • 6
  • 2
    There are 3 standard fonts in PDF: Courier, Times New Roman and Helvetica and they only support what's called the Latin Character set which is a pretty close match on ISO-8859-1. On your computer these fonts probably have many more supported characters but the spec only defines these. If you want to use your computer's font you're going to have to perform much more complex things with PDF and you're going to learn that fonts themselves are generally "left-to-right" and you'll probably have to draw your text backwards. PDF is very simple for very simple things but grows very complex very fast. – Chris Haas Jan 19 '16 at 14:41
  • In addition to what Chris wrote, be aware that for non-Latin text, you need to use CID encoding to call out specific glyphs from a font, rather than just named characters (A, B, C, etc.) as in English/Latin text. And you need to embed the font. Also, for Arabic, there's another fun twist where you need to use special positional forms for letters at the beginning and end of words. This is all very hard to do even with a robust toolkit such as the Adobe PDF Library, and effectively impossible to do manually. I would look into an existing PDF generation toolkit. – Dan Korn Jan 19 '16 at 17:33
  • http://stackoverflow.com/questions/7355025/create-pdf-with-java – Dan Korn Jan 19 '16 at 17:39
  • Thanks guys, your comments are more than enough, but as I'm new to Stackoverflow, how am I supposed to close this question? – asaeles Jan 27 '16 at 06:50

1 Answers1

0

There is no easy way to do this, thanks to Chris Haas & Dan Korn for their helpful comments.

Community
  • 1
  • 1
asaeles
  • 66
  • 6