pdf 1.2: How to display a graphical image? - pdf

I am trying to learn structure of a pdf document from guide. I could add the text and shapes with lines, but I am having problem displaying the image.
The code I am writing to display an image is (on page 54):
%PDF-1.2
% based on e08.pdf
1 0 obj
<<
/Type /Page
/Parent 5 0 R
/Resources 3 0 R
/Contents 2 0 R
>>
endobj
2 0 obj
<< /Length 51 >>
stream
BT
/F1 24 Tf
1 0 0 1 260 254 Tm
/CS1 cs
63 127 127 sc
(Hello World)Tj
ET
100 0 127 sc
/CS2 CS
0 0 1 SC
315 226 m
299 182 l
339 208 l
291 208 l
331 182 l
b
100 0 0 100 65 326 cm
BI /W 36 /H 32 /BPC 8
/CS /DeviceGray
ID
ççççççççççççÕˇˇˇˇˇˇˇˇˇÕççççççççççç
çççççççççççç͡ˇˇˇˇˇˇˇˇÍçççççççççççç
ççççççççççç¢ˇˇˇˇˇˇˇˇˇˇˇ¢ççççççççççç
çççççççççç瑡ˇˇˇˇˇˇˇˇˇˇ‘ççççççççççç
ççççççççççˇˇˇˇˇˇˇˇˇˇˇîçççççççççç
ççççççççççøˇˇˇˇˇˇˇˇˇˇˇˇˇøçççççççççç
ççççççççççÒˇˇˇˇˇˇˇˇˇˇˇˇˇÒçççççççççç
çççççççç籡ˇˇˇˇˇˇˇˇˇˇˇˇˇˇ±ççççççççç
çççççççç瀡ˇˇˇˇˇˇˇˇˇˇˇˇˇˇ€ççççççççç
ççççççççõˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇõçççççççç
ççççççççDˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇDçççççççç
çççççççç¯ˇˇˇˇˇˇˇˇ¯ˇˇˇˇˇˇˇˇ¯çççççççç
çççççççÕˇˇˇˇˇˇˇˇˇÕˇˇˇˇˇˇˇˇˇÕçççççç
çççççç炡ˇˇˇˇˇˇˇÍç͡ˇˇˇˇˇˇˇ‚ççççççç
çççççç¢ˇˇˇˇˇˇˇˇˇÕçÕˇˇˇˇˇˇˇˇˇ¢ççççç
ççççç瑡ˇˇˇˇˇˇˇ¯ççç¯ˇˇˇˇˇˇˇˇ‘çççççç
çççççî¯ˇˇˇˇˇˇˇˇDçççÕˇˇˇˇˇˇˇˇ¯îççççç
çççççøˇˇˇˇˇˇˇˇˇõçççõˇˇˇˇˇˇˇˇˇøççççç
çççççÒˇˇˇˇˇˇˇˇ‘çççç瀡ˇˇˇˇˇˇˇÒççççç
ççç癡ˇˇˇˇˇˇˇˇ™çççç癡ˇˇˇˇˇˇˇˇ™ççç
ççç瑡ˇˇˇˇˇˇˇÒçççççççÒˇˇˇˇˇˇˇˇ‘çççç
çççõˇˇˇˇˇˇˇˇˇÕçççççççøˇˇˇˇˇˇˇˇˇõççç
çççDˇˇˇˇˇˇˇˇ¯îçççççççî¯ˇˇˇˇˇˇˇˇDççç
çççÒˇˇˇˇˇˇˇˇÕçççççççç瑡ˇˇˇˇˇˇˇÒççç
ççÕˇˇˇˇˇˇˇˇˇõçççççççççõˇˇˇˇˇˇˇˇˇÕçç
ç炡ˇˇˇˇˇˇˇÒDDDDDDçççç炡ˇˇˇˇˇˇˇ‚çç
çõˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ™ççççÕˇˇˇˇˇˇˇˇˇõç
瑡ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ‘çççççÒˇˇˇˇˇˇˇˇ‘ç
î¯ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇîççççDˇˇˇˇˇˇˇˇ¯î
ÕˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇøççççˇˇˇˇˇˇˇÕ
͡ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇÒçççç瑡ˇˇˇˇˇˇˇÒ
ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ™ççç癡ˇˇˇˇˇˇˇˇ
EI
endstream
endobj
3 0 obj
<<
/ProcSet[/PDF/Text]
/Font <</F1 4 0 R>>
/ColorSpace
<<
/CS1
[
/Lab
<<
/Range [-128 127 -128 127]
/WhitePoint [ 0.951 1 1.089]
>>
]
/CS2
[
/CalRGB
<<
/Gamma [2.222 2.222 2.222]
/Matrix
[
0.412 0.213 0.019
0.358 0.715 0.119
0.181 0.072 0.951
]
/WhitePoint [0.951 1 1.089]
>>
]
>>
>>
endobj
4 0 obj
<<
/Type /Font
/Subtype /Type1
/Name /F1
/BaseFont/Helvetica
>>
endobj
5 0 obj
<<
/Type /Pages
/Kids [ 1 0 R ]
/Count 1
/MediaBox [ 0 0 612 446 ]
>>
endobj
6 0 obj
<<
/Type /Catalog
/Pages 5 0 R
>>
endobj
trailer
<< /Root 6 0 R >>
What I expect from it is:
But when I open the file in Acrobat Reader DC 2015, I see the text and the star, but not the image logo.
Note:
I have formatted the code myself, so please let me know if it is not proper.
I assume that there are problems with the characters that are used to show the Adobe logo. I guess the characters should be binary data, and when the PDF is generated, they are converted to those symbols.
Here the author is using pdf 1.2, that is pretty old, but as far as I know it should not make a problem, since pdf is backward compatible.
My question:
Why I cannot see the desired result as shown in the image using this code?
How to get the codes needed to use in PDF to display an image. Let us say this textual representation of the binary code (or even the binary itself) that I have used in my code?
Update:
As mentioned in the comment, cross reference table does not exist in my code, but when I generated that with pdftk tool, the result was the same.

The major problem with your inline image is that you try to create a binary data block using text.
The data between ID and EI is interpreted as a stream of a single (!) white space character followed by height x width x bits-per-component/8 x number-of-components data bytes, i.e. in your case (according to /W 36 /H 32 /BPC 8 /CS /DeviceGray) 32 * 36 * 8/8 * 1 bytes.
This is not the case in your sample. In your question you have that data block indented which adds numerous bytes to the stream. Furthermore you have lines containing different numbers of bytes (even though they may look equally long in an editor).
Your binary download differs substantially from your question text, e.g. instead of the ˇ characters filling the A you have . characters there. If suffers from unequal line lengths, too
I assume you use a text editor to write that PDF which is a bad choice because you do not correctly see the real number of bytes used. Especially problematic are control characters and byte values not associated with a character in your encoding.
Let's therefore try something more simple and only use characters in the ASCII range and a smaller, simpler form:
Depending on your end-of-line sequence (their bytes are part of the data bytes!!) use either of the following two samples
in case of single byte end-of-line sequences (only CR or only LF, typical for Mac or Unix):
BI /W 5 /H 4 /BPC 8
/CS /DeviceGray
ID
zzzz
z..z
z..z
zzzz
EI
in case of two byte end-of-line sequences (CR LF, typical for DOS / MS Windows):
BI /W 6 /H 4 /BPC 8
/CS /DeviceGray
ID
zzzz
z..z
z..z
zzzz
EI
Do take care not to add any leading or trailing spaces! They would also be interpreted as data bytes!
The result looks like this in the first case
and this in the second case
The dark bar(s) on the right / on both right and left is/are due to the line ending character(s).
If you don't want such bars, you have to get rid of the line endings, e.g.
BI /W 4 /H 4 /BPC 8
/CS /DeviceGray
ID zzzzz..zz..zzzzz EI
resulting in
That all been said, please do yourself a favor and
don't create PDFs as text, e.g. in a text editor! While they can be understood to a certain degree in a text viewer, creating them in a text editor very soon becomes hell;
don't use inline images but instead image resources! Inline images have proved to be troublesome and in PDF 2.0 will be deprecated or at least restricted to very small sizes; and finally
don't use the PDF 1.2 reference but instead the current PDF standard ISO 32000-1! Adobe personal called the old PDF references not normative in nature, so you can not count on what they say.

Related

Trying to embed simple UTF16 character into manually created PDF but failing

I'm trying to manually create a PDF document (using the PDFGen C code on github). This is on a small footprint device with limited storage.
All works fine until I want to embed (say) the Unicode Ohms character (U+2126).
Below is the test file I'm using, which should show "Hello" with an Ohms symbol after the 'H'.
However, it actually shows "H!&ello".
%PDF-1.4
<hex chars removed>
1 0 obj
<< /Pages 2 0 R /Type /Catalog >>
endobj
2 0 obj
<< /Count 1 /Kids [ 3 0 R ] /Type /Pages >>
endobj
3 0 obj
<< /Contents 4 0 R /MediaBox [ 0 0 500 800 ] /Parent 2 0 R /Resources 5 0 R /Type /Page >>
endobj
4 0 obj
<< /Length 57 >>
stream
BT /F1 24 Tf 175 720 Td <FEFF004821260065006C006C006F> Tj ET
endstream
endobj
5 0 obj
<< /Font << /F1 6 0 R >> >>
endobj
6 0 obj
<< /BaseFont /Courier /Subtype /Type1 /Type /Font >>
endobj
xref
0 7
0000000000 65535 f
0000000015 00000 n
0000000064 00000 n
0000000123 00000 n
0000000229 00000 n
0000000335 00000 n
0000000378 00000 n
trailer << /Root 1 0 R /Size 7 /ID [<89311a609a751f1666063e6962e79bd5><89311a609a751f1666063e6962e79bd5>] >>
startxref
448
%%EOF
I can only assume my Unicode hex string <FEFF004821260065006C006C006F> is badly formatted.
Or is the Font definition incorrect ?
Or is my understanding of how to embed Unicode wrong ?
I'm ultimately not wanting to embed any fonts as I don't have the storage space or processing power. I just want to add Unicode characters and rely on the PDF renderer to work out how to display them using the default Courier font.
Is that even possible ?
Thanks in advance for any help/advice/comments.
UPDATE
After some useful advice below, I've now managed to achieve what I needed.
I modded my code to switch fonts on a per-character basis between Courier and Symbol and now support (nearly) all the standard characters.
I also added some character scaling to keep the Symbol characters aligned with the Courier font but the end result works for me :)
Here's an image of my test PDF ...
Oddly the original PC IBM 437 code set included Ω wiki note i [03A9] (234) but did not make it to Courier ??
You could try coding those few characters you need as an embedded sub-setted symbol font and quite possibly do that using ascii(7bit) or ansi(8bit) but the overheads would be tremendous for your few characters.
Simpler try switching fonts (as required per target characters) to Symbol font and it could look like this
P.S. the codes dont need to be "word" doubles there are only 256 chars.
<< /BaseFont /Symbol /Subtype /Type1 /Type /Font >>
BT /F2 24 Tf 175 720 Td <4857657C7C6F20766FC27C64> Tj ET
By alternating courier and symbol you will get your desired
In your code it could look something like (with included transforms)
BT
/F0 24 Tf 1 0 0 1 0 .0675 Tm (H) Tj
ET
BT
/F1 24 Tf 1 0 0 1 14.4 .0675 Tm <003a> Tj
ET
BT
/F0 24 Tf 1 0 0 1 32.832 .0675 Tm (ello) Tj
ET
Note my editor used F0 for Courier and F1 for Symbol (base 0 is more normal)
Also it used a slightly different code method of defining Omega as <003a>
Here I am tweaking the text in Windows Notepad to watch how compiling (Ctrl+S) moves the Omega character spacing whilst watching it slide sideways live in the Previewer. Also note that Upper case Omega is W in the raw symbol font !!
So my replacement fix for your code looks like this (You can easily make it look closer to yours, and leaner, by removing white space and line feeds)
%PDF-1.4
%µ¶
1 0 obj
<<
/Pages 2 0 R
/Type /Catalog
>>
endobj
2 0 obj
<<
/Count 1
/Kids [ 3 0 R ]
/Type /Pages
>>
endobj
3 0 obj
<<
/Contents 4 0 R
/MediaBox [ 0 0 500 800 ]
/Parent 2 0 R
/Resources <<
/Font <<
/F1 5 0 R
/F2 6 0 R
>>
>>
/Type /Page
>>
endobj
4 0 obj
<<
/Length 133
>>
stream
q
BT
/F1 24 Tf
1 0 0 1 175 720 Tm
(H) Tj
ET
BT
/F2 24 Tf
1 0 0 1 189 720 Tm
(W) Tj
ET
BT
/F1 24 Tf
1 0 0 1 206 720 Tm
(ello) Tj
ET
Q
endstream
endobj
5 0 obj
<<
/BaseFont /Courier
/Subtype /Type1
/Type /Font
>>
endobj
6 0 obj
<<
/BaseFont /Symbol
/Subtype /Type1
/Type /Font
>>
endobj
xref
0 7
0000000000 65536 f
0000000016 00000 n
0000000070 00000 n
0000000136 00000 n
0000000307 00000 n
0000000494 00000 n
0000000569 00000 n
trailer
<<
/Size 7
/Root 1 0 R
/ID [ <89311A609A751F1666063E6962E79BD5> <EE408A115072E92E3A34C8BB8BDC6AE6> ]
>>
startxref
643
%%EOF
You cannot do it.
Note: you want to insert a Unicode character (not a UTF-16, which it is just one of many representation/encoding of Unicode).
No fonts includes all glyphs, and as far I know, only few Latin-1 fonts are safe (and required) for PDF. Note: such fonts requires a Latin-1 encoding (contrary of all other fonts, this is just a portability issue, for "pre Unicode epoch"). An additional problem. Type1 uses glyph indices, which may not be the same as Unicode Codepoints (in fact, I think they are always different). IIRC Adobe has some documentation about this. And type1 is nearly out of support, maybe it is better not to use it for 2021 programs.
You may assume people will have Microsoft Windows, and so you can use Symbol font (and using Omega, instead of Ohm, which may be represented with the same glyph). But in this case you are creating a "Non-Portable" Portable Document format (PDF).

PDF generation — How to merge multiple stream objects?

I'm currently into generating PDF documents without the use of an external library and it has been going well so far. I've written the document exposed below with a text editor (vim) and it renders the expected results using at least two PDF distinct viewers (evince & gsview, running Linux).
This document produces three squares at top of the page, coming in different sizes, widths and colors.
My question is : is there a way to merge two stream objects into a new single one or, in other words, is there a way to compose sophisticated objects starting from simple ones, so we can easily refer to these composite objects, multiple times if needed ?
In the given example, object 5 0 obj is drawing a square, and following ones are just applying colors and coordinates transformations (through a matrix).
The PDF reference manual states that multiple stream contents passed as an array to page object's /Contents parameter are concatenated and processed as a single continuous stream, which totally does the trick… as long as the document remains small and simple!
In this same example, the /Contents array is indirectly passed through object 4 0 obj, which refers three times to 5 0 R, to draw the squares.
The ideal here would be to define three differents objects, each refering to 5 0 R by themselves, then invoke only these objects, a single time each, from the Contents array.
I tried adding subarrays inside it, which could in turn be embedded into dedicated objects and referenced indirectly, but it unfortunately doesn't work. :-(
A lot of thanks to any people that could/try-to help !
PS: I'm doing it because I'm interested in the format itself and would like to produce some autogenerated documents from small scripts. Also, I'll probably embed them into a weakly powered appliance and I cannot afford relying on dozens of megabytes in dependencies.
But before this, I still tried to do that too, using PHP with TCPDF. If there's already some facilities dedicated to this that I would have missed, this is relevant to my interests too. :-)
Small.pdf (hand made PDF file)
%PDF-1.7
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<<
/Type /Pages
/Count 1
/Kids [ 3 0 R ]
>>
endobj
3 0 obj
<<
/Type /Page
/MediaBox [ 0.000000 0.000000 1000.000000 1414.213562 ]
/Contents 4 0 R
>>
endobj
4 0 obj
% A simple array, just to avoid embedding it directly in /Page object (3 0 R here)
[
6 0 R 5 0 R % Red square
7 0 R 5 0 R % Green square
8 0 R 5 0 R % Blue square (tilted)
]
endobj
5 0 obj
% Draws a square, centered by default on lower left corner
<<
/Length 43
>>
stream
+20 +20 m
+20 -20 l
-20 -20 l
-20 +20 l s Q
endstream
endobj
6 0 obj
<<
/Length 63
>>
stream
/DeviceRGB CS
q
1.0 0.0 0.0 SC
2.0 w
1 0 0 -1 60 1354.213562 cm
endstream
endobj
7 0 obj
<<
/Length 49
>>
stream
q
0.0 1.0 0.0 SC
1.0 w
2 0 0 -2 190 1334.213562 cm
endstream
endobj
8 0 obj
<<
/Length 83
>>
stream
q
0.0 0.0 1.0 SC
5.0 w
0.707106781 0.707106781 -0.707106781 0.707106781 110 1250 cm
endstream
endobj
xref
0 9
0000000000 65535 f
0000000010 00000 n
0000000079 00000 n
0000000168 00000 n
0000000296 00000 n
0000000513 00000 n
0000000674 00000 n
0000000796 00000 n
0000000905 00000 n
trailer
<<
/Size 9
/Root 1 0 R
/ID [ <0000000000> <0000000001> ]
>>
startxref
01047
%%EOF
What you are looking for are form XObjects.
The pdf specification ISO 32000-1 characterizes them like this:
A form XObject is a PDF content stream that is a self-contained description of any sequence of graphics objects. A form XObject may be painted multiple times - either on several pages or at several locations on the same page - and produces the same results each time, subject only to the graphics state at the time it is invoked.
For details please read section 8.10 of the specification.

XREF table in a pdf file

I have written the following Hello code for a pdf file.
%PDF-1.4
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Kids [3 0 R] /Count 1 >>
endobj
3 0 obj
<< /Parent 2 0 R /Contents 4 0 R >>
endobj
4 0 obj
<< /Length 20 >>
stream
BT
/F1 40 Tf
100 600 Td
(Hello!) Tj
ET
endstream
endobj
trailer
<< /Root 1 0 R
/Size 3
>>
%%EOF
I want to know how the xref table is calculated?\
UPDATE AFTER THIRD COMMENT:
Can I write a table as below?
xref
0 3
0000000000 65536 f
0000000001 00000 n
0000000002 00000 n
0000000003 00000 n
What is wrong with that (if any)?
The example in this page, shows that there are differences (greater than 1) between objects in xref. However, it is not clear why first object has offset 15 and second object has 87 offset. How these numbers are calculated?
After the edit of the question the problem became clear, you don't know the units in which the offset is measured.
The n entry in a xref table is described as
The format of an in-use entry shall be:
nnnnnnnnnn ggggg n eol
where:
nnnnnnnnnn shall be a 10-digit byte offset in the decoded stream
ggggg shall be a 5-digit generation number
n shall be a keyword identifying this as an in-use entry
eol shall be a 2-character end-of-line sequence
"10-digit byte offset in the decoded stream" might be a bit unclear. Fortunately the text above immediately is followed by an explanation:
The byte offset in the decoded stream shall be a 10-digit number, padded with leading zeros if necessary, giving the number of bytes from the beginning of the file to the beginning of the object.
(ISO 32000-1, section 7.5.4 "Cross-Reference Table")
Thus, the offset here really simply is the byte position in the PDF at which the object starts which its object and generation number.
As an aside, one item you must also follow strictly is the length of such an entry:
the overall length of the entry shall always be exactly 20 bytes.

(Manually created Simple PDF using PDF Reference-1.7 )Adobe Reader XI asking to save when closing the PDF?

I have generated a PDF Using the following PDF code its working fine but when i am trying to close ,its asking me to save.I have analyzed my PDF code to detect the problem. I have identified there is a problem in startxref offset size and xref offset position.I have done enough changes but i couldn't solve this problem(Do you want to save changes 'xxx.pdf' before closing).
here is my PDF CODE
%PDF-1.4
%âãÏÓ
1 0 obj
<<
/Type/Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<<
/Type/Pages
/MediaBox[0 0 612.0 792.0]
/Count 1
/Kids [ 3 0 R ]
>>
endobj
3 0 obj
<<
/Type/Page
/Parent 2 0 R
/Resources 4 0 R
/Contents 5 0 R
>>
endobj
4 0 obj
<<
/ExtGState <</GS1 7 0 R>>
/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]
/Font<< /F1 8 0 R >>
>>
>>
endobj
5 0 obj
<</Length 44>>
stream
BT
/F1 18 Tf
0 g
1 0 0 1 100.0 400.0 Tm
(kersom) Tj
ET
endstream
endobj
6 0 obj<</Producer(Xxxxxxxx XXX Xxxxxxxx - 1.1)>>
endobj
7 0 obj
<</ca 0.35/CA 0.35>>
endobj
8 0 obj
<<
/Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
xref
0 9
0000000000 65535 f
0000000015 00000 n
0000000063 00000 n
0000000148 00000 n
0000000228 00000 n
0000000340 00000 n
0000000442 00000 n
0000000499 00000 n
0000000535 00000 n
trailer
<<
/Info 6 0 R
/Root 1 0 R
/Size 9
>>
startxref
606
%%EOF
Having received the sample PDF in its original form, the issue immediately becomes clear: The offsets in the cross reference table are correct but that table itself is incorrectly built.
Let's look at a hex dump:
Obviously each entry in the cross reference table is 19 bytes in size.
Now let's look at the PDF specification:
Each entry shall be exactly 20 bytes long, including the end-of-line marker. [...] The format of an in-use entry shall be:
nnnnnnnnnn ggggg n eol
where:
nnnnnnnnnn shall be a 10-digit byte offset in the decoded stream
ggggg shall be a 5-digit generation number
n shall be a keyword identifying this as an in-use entry
eol shall be a 2-character end-of-line sequence
[...] a 2-character end-of-line sequence consisting of one of the following: SP CR, SP LF, or CR LF. Thus, the overall length of the entry shall always be exactly 20 bytes
(section 7.5.4 Cross-Reference Table of ISO 32000-1)
Thus, the issue in the OP's PDF is that each cross reference table entry has an end-of-line sequence of only one byte, a LF, while it must have a 2-byte end-of-line sequence, either SP CR, SP LF, or CR LF.
This makes each entry one byte too short which in turn results in look-ups from that table returning utterly broken byte sequences.
Save the form with Adobe Reader and compare it at a binary level. You will discover a slight difference. For instance: the cross-reference table was rebuilt because you didn't take into account 'carriage return' characters, there was white space where you didn't expect it, etc...
Adobe Reader also fixes errors such as this one:
4 0 obj
<<
/ExtGState <</GS1 7 0 R>>
/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]
/Font<< /F1 8 0 R >>
>>
>>
You have a double dictionary ending here (remove >>) once. That's at least one error in the PDF you've copy/pasted.

How to add text object to existing pdf

I have a source pdf which I am modifying by adding text objects. I am using "Incremental Updates" which is mentioned in the PDF specification. But while adding text objects using this method I am making some mistakes due to which the pdf doesn't render properly in Adobe Reader 11. When the pdf is opened and I double-click on it, the added text objects get deleted. I figured out that this is due to text annotation.
Now I want to know how a new text object can be added using incremental update? How do the Contents and RC of a free text annotation have to be to maintained?
Also is it possible to disable or delete the annotation so that my problem can be avoided easily? Because I want a simple pdf, I don't want annotation options.
The source pdf I am using is here.
The modified pdf after adding text object is here.
I am not sure that source pdf is itself proper according to pdf specification.
First off let me show you how easy things are if you can use a decent PDF library. I use iTextSharp as an example but the same can also be done with others like PDFBox or PDFNet (already mentioned by #Ika in his answer):
PdfReader reader = new PdfReader(sourcePdf);
using (PdfStamper stamper = new PdfStamper(reader, targetPdfStream)) {
Font FONT = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, new GrayColor(0.75f));
PdfContentByte canvas = stamper.GetOverContent(1);
ColumnText.ShowTextAligned(
canvas,
Element.ALIGN_LEFT,
new Phrase("Hello people!", FONT),
36, 540, 0
);
}
(Derived from the Webified iTextSharp Example StampText.cs explained in chapter 6 of iText in Action — 2nd Edition.)
(Which PDF library you choose, depends on your general requirements and available license models.)
If, in spite of the ease of use of such PDF libraries, you insist on doing it manually, here some remarks:
First you have to find the Page dictionary of the page you want to add content to. Depending on the type of PDF this already might require decompression of object streams etc. but in your sample modified1.pdf that is not necessary:
7 0 obj
<</Rotate 90
/Type /Page
/TrimBox [ 9.54 6.12 585.68 835.88 ]
/Resources 8 0 R
/CropBox [ 0 0 595.22 842 ]
/ArtBox [ 9.54 18.36 585.68 842 ]
/Contents [ 9 0 R 10 0 R 11 0 R 12 0 R 13 0 R 14 0 R 15 0 R 16 0 R ]
/Parent 6 0 R
/MediaBox [ 0 0 595.22 842 ]
/Annots 17 0 R
/BleedBox [ 9.54 6.12 585.68 835.88 ]
>>
endobj
You see the array of references to content streams. This is where you have to add new page content to. You can manipulate an existing stream or create a new stream and add it to that array.
(Most PDFs have their content stream compressed. For the general case, therefore, you'd have to decompress a stream before you can work on it. Thus, in my eyes, the easier way would be to start a new stream.)
You chose to manipulate the last referenced stream 16 0 which in your PDF is uncompressed:
16 0 obj
<</Length 37 0 R>>
stream
S 1 0 0 1 13.183 0 cm 0 0 m
[...]
0 10 -10 -0 506.238 342.629 Tm
.13333 .11765 .12157 scn
-.0002 Tc
.0006 Tw
(the Bank and branch on which cheque is drawn\).)Tj
/F1 2 Tf
-15.1279 10.9462 Td
(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!##$%^&*aaaaaaaaaaaaa)Tj
/F2 1 Tf
015.1279 01.9462 Td
(ANAabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789)Tj
ET
endstream
endobj
Your additions, I gather, are the two 3-liners at the bottom which first select a font, then position the insertion point and finally print a selection of letters.
Now you say you added text abc..z and ABC...Z just for testing. But letters b j k q v etc not appearing in the pdf. The problem becomes even more visible for your second addition of letters; here only the capital 'A' and 'N' are displayed.
This is due to the fact that the fonts in question are embedded into the PDF --- fonts are embedded into PDFs to allow PDF viewers on systems which don't have the font in question, to display the PDF --- but they are not completely embedded, only the subset of characters required from that font.
Let's look for the font F2 for which only 'N' and 'A' appear:
According to the page object, the page resources can be found in object 8 0:
8 0 obj
<</Font <</F1 45 0 R /TT2 46 0 R /F2 47 0 R>>
/ExtGState <</GS2 48 0 R>>
/ProcSet [ /PDF /Text ]
/ColorSpace <</Cs6 49 0 R>>
>>
endobj
So F2 is defined in 47 0:
47 0 obj
<</Subtype /Type1
/Type /Font
/Widths [ 722 250 250 250 250 250 250 250 250 250 250 250 250 722 ]
/Encoding 52 0 R
/FirstChar 65
/FontDescriptor 53 0 R
/ToUnicode 54 0 R
/BaseFont /ILBPOB+TimesNewRomanPSMT-Bold
/LastChar 78
>>
endobj
In the referenced ToUnicode map 54 0 you see
54 0 obj
<</Length 55 0 R>>stream
/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo <<
/Registry (AAAAAA+F2+0) /Ordering (T1UV) /Supplement 0 >> def
/CMapName /AAAAAA+F2+0 def
/CMapType 2 def
1 begincodespacerange <41> <4e> endcodespacerange
2 beginbfchar
<41> <0041>
<4e> <004E>
endbfchar
endcmap CMapName currentdict /CMap defineresource pop end end
endstream
endobj
In this mapping you see that only character codes 0x41 'A' and 0x4e 'N' are mapped
In your document the font is used only to print "NA" in the amount table cells and for nothing else. Thus, only those two letters 'N' and 'A' are embedded, which results in your addition with that font only outputting these letters.
Thus, to successfully add text to the page you either have to check the font ressources associated with the page for the glyphs they provide (and restrict your additions to those glyphs) or you have to add your own font resource.
As the presence of characters in the encoding often is not as easy to see as it is here (ToUnicode is optional), I would propose, you add your own font ressources. The PDF specification ISO 32000-1 explains how to do that.
Furthermore you state the x and y axis position for the text is not properly displaying in pdf. While you don't say what exactly you mean, you should be aware that in the content stream you can apply affine transformations to the coordinate system of the page, i.e. stretch, skew, rotate, and move the axis.
If you want to use the original coordinate system and not depend on the coordinates to be proper at your additions, you should add an initial content stream to the page containing a q operator (to save the current graphics state on the graphics state stack) and start your additions in a new final content stream with a Q operator (to restore the graphics state by removing the most recently saved state from the stack and making it the current state).
EDIT As a sample I applied the Java equivalent of the C# code at the top to your modified1.pdf with append mode activated. The following objects were changed or added as a result:
The page object 7 0 has been updated:
7 0 obj
<</CropBox[0 0 595.22 842]
/Parent 6 0 R
/Contents[69 0 R 9 0 R 10 0 R 11 0 R 12 0 R 13 0 R 14 0 R 15 0 R 16 0 R 70 0 R]
/Type/Page
/Resources<<
/ExtGState<</GS2 48 0 R>>
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/ColorSpace<</Cs6 49 0 R>>
/Font<</F1 45 0 R/F2 47 0 R/TT2 46 0 R/Xi0 68 0 R>>
>>
/MediaBox[0 0 595.22 842]
/TrimBox[9.54 6.12 585.68 835.88]
/BleedBox[9.54 6.12 585.68 835.88]
/Annots 17 0 R
/ArtBox[9.54 18.36 585.68 842]
/Rotate 90
>>
endobj
If you compare with your former version, you see that
two new content streams have been added, 69 0 at the start and 70 0 at the end;
the resources are not an indirect object anymore but instead are directly included here;
the resources contain a new Font ressource Xi0 at 68 0.
Now let's look at the added objects.
This is the font ressource for Helvetica-Bold named Xi0 at 68 0:
68 0 obj
<</BaseFont/Helvetica-Bold
/Type/Font
/Encoding/WinAnsiEncoding
/Subtype/Type1
>>
endobj
Non-embedded, standard 14 font resources are not complicated at all...
Now there are the additional content streams. iText does compress them, but I'll show them in an uncompressed state here:
69 0 obj
<</Length 1>>stream
q
endstream
endobj
70 0 obj
<</Length 106>>stream
Q
q
0 1 -1 0 595.22 0 cm
q
BT
1 0 0 1 36 540 Tm
/Xi0 12 Tf
0.75 g
(Hello people!)Tj
0 g
ET
Q
Q
endstream
endobj
So the new content stream at the start stores the current graphic state, and the new one at the end retrieves that stored state, changes the coordinate system, positions for text insertion, selects font, font size, and the fill colour, and finally prints a string.