Creating a signed PDF using adbe.pkcs7.detached - pdf

I am generating this really basic PDF and trying to sign it. But I am missing something and can figure out what, maybe you guys have an idea.
Acrobat does recognize the signature, but displays:
The validity of the document certification is UNKNOWN.
An error occurred while attempting to validate this signature.
The signature should be fine.
ByteRange offsets are fine as well.
So I can only think of a field or object I am missing (?)
%PDF-1.7
%âãÏÓ
1 0 obj
<</Type/Catalog/Version/1.7/Pages 2 0 R/Perms<</DocMDP 3 0 R>>/AcroForm<</Fields[4 0 R]/SigFlags 1>>>>
endobj
2 0 obj
<</Type/Pages/Kids[5 0 R]/Count 1>>
endobj
3 0 obj
<</Type/Sig/Filter/Adobe.PPKLite/ByteRange[0 295 3295 846] /Contents<308204db06092a864886f70d010702a08204cc308204c8020101310f300d06096086480165030402010500300b06092a864886f70d010701a082027f3082027b308201e4020900b4b98c33de20e306300d06092a864886f70d01010b0500308180310b30090603550406130244453112301006035504080c09617364662061736466310d300b06035504070c0461736466310d300b060355040a0c0461736466310d300b060355040b0c0461736466310c300a06035504030c036173663122302006092a864886f70d01090116136173646661736466406173646661732e636f6d3020170d3230313032303131303833315a180f33303230303232313131303833315a308180310b30090603550406130244453112301006035504080c09617364662061736466310d300b06035504070c0461736466310d300b060355040a0c0461736466310d300b060355040b0c0461736466310c300a06035504030c036173663122302006092a864886f70d01090116136173646661736466406173646661732e636f6d30819f300d06092a864886f70d010101050003818d0030818902818100dec31622a34a21373280798b444024738d7420500f1865466f760f8e3ba8effc320bbe4c4e15553af31518952a745852a1a4dffaff8f9ca95bf9b0b89595d6a0ffd698a42392361a7cbb5490c17a56ebcb7843aaff77a50424015c5a1c7acd2a29728b5893f9469cc1bef21f3d94278219c3084847c01d8720438da4819264b50203010001300d06092a864886f70d01010b05000381810023ae25bab322bff456acba1d1fa187a65b2bac659c0c7fd21dfc6cc56fd3eb5a3c2d681b4742667a36823633a70a6c6d4af602c4438b5d545950fcb2bf12a40f36bd82cc375c7c6ce03b488e55e014dbd176a74182465fab2dbcc5029f68ce36bb9135c8d4c2a1a692fa89d1c13f3b074f66ac97b1b011e53bdb84cfe1d0a690318202203082021c02010130818e308180310b30090603550406130244453112301006035504080c09617364662061736466310d300b06035504070c0461736466310d300b060355040a0c0461736466310d300b060355040b0c0461736466310c300a06035504030c036173663122302006092a864886f70d01090116136173646661736466406173646661732e636f6d020900b4b98c33de20e306300d06096086480165030402010500a081e4301806092a864886f70d010903310b06092a864886f70d010701301c06092a864886f70d010905310f170d3230313032313231323131305a302f06092a864886f70d01090431220420da807ca67317ae0b36afaa745f2dd2c3331bcb550e271e2757cf19c6974cdcb2307906092a864886f70d01090f316c306a300b060960864801650304012a300b0609608648016503040116300b0609608648016503040102300a06082a864886f70d0307300e06082a864886f70d030202020080300d06082a864886f70d0302020140300706052b0e030207300d06082a864886f70d0302020128300d06092a864886f70d01010105000481800df9fd0348768d03bf3328db4635b8a7845eb614d2b01f6c82b9c8c459bdbd0324c95d3b1c32de79883dc0468fae51ca5a8fe2a7a58351cb3dd09c02a65cbce54c0fbf7aeda516bbe6c2cc30b067b1f7ca4f4d94e37d47b21ef098f418e81104249e5dfc24e9aad76f1fbc9f30a2a2164a1beb2be2cbf318d7bbff77e32d6f7600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000>/ContactInfo(contact#example.com)/M(D:20201021212110+0000)/Location(Germany)/Name(Foo Bar)/Reason(Testing)/SubFilter/adbe.pkcs7.detached/Reference[<</TransformMethod/DocMDP/TransformParams<</Type/TransformParams/P 2/V/1.2>>/Type/SigRef>>]>>
endobj
4 0 obj
<</FT/Sig/V 3 0 R/Subtype/Widget/Type/Annot/Rect[0 0 0 0]/P 5 0 R/Ff 0/T(Signature)/F 4>>
endobj
5 0 obj
<</Type/Page/LastModified(D:20201021212110+0000)/Resources<<>>/Contents 6 0 R/MediaBox[0 0 100 100]/Parent 2 0 R/Annots[4 0 R]>>
endobj
6 0 obj
<</Length 26>>
stream
1 0 0 rg
25 25 50 50 re
f
endstream
endobj
xref
0 7
0000000000 65535 f
0000000019 00000 n
0000000137 00000 n
0000000188 00000 n
0000003544 00000 n
0000003649 00000 n
0000003793 00000 n
trailer
<</Size 7/Root 1 0 R/ID[<710C628807B8A8C0FE59D85C01B973A4><710C628807B8A8C0FE59D85C01B973A4>]>>
startxref
3868
%%EOF
Any hints are welcome!

There are at least two errors in your file.
Incorrect ByteRange
The gap in the ByteRange contains only the hex digits of the signature value
308204db...00000000
but it should contain the whole hexadecimal string including its delimiters, the angled brackets
<308204db...00000000>
as required by the specification for interoperable signatures:
For byte range signatures, Contents shall be a hexadecimal string with “<” and “>” delimiters. It shall fit precisely in the space between the ranges specified by ByteRange.
(ISO 32000-1 section 12.8.3.3.2)
Thus, your ByteRange array should have been [0 294 3296 845] instead of [0 295 3295 846].
Incorrect Signature Date
Your signature dictionary M value is
(D:20201022075138+0000)
This is incorrect in two ways:
First of all, if you in your date object have both hours and minutes of the timezone offset, they must be separated by an apostrophe. Thus,
(D:20201022075138+00'00)
Furthermore, the '+' sign is reserved for positive timezone offsets; for a zero offset the 'Z' must be used. Thus,
(D:20201022075138Z00'00)
This is required by the specification for date objects:
A date shall be a text string of the form
(D:YYYYMMDDHHmmSSOHH'mm)
...
A PLUS SIGN as the value of the O field signifies that local time is later than UT, a HYPHEN-MINUS signifies that local time is earlier than UT, and the LATIN CAPITAL LETTER Z signifies that local time is equal to UT.
Furthermore, a SigFlags value of 3 instead of 1 in your AcroForm dictionary would improve the user experience in PDF viewers.

Related

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.

PDF Adding Electronic Signature

We have an API for adding electronic signature to PDF documents. PDF documents signed by our API generally have the following structure:
/Type/Annot/
/Type/Sig/
/Type/Font/BaseFont/Helvetica
/Type/Font/BaseFont/ZapfDingbats
/Type/XObject/Subtype/Form
/Type/Page/
/Type/Catalog/
xref
0 1
0000000000 65535 f
57 1
0000088682 00000 n
221 7
0000088804 00000 n
0000088450 00000 n
0000054970 00000 n
0000088289 00000 n
0000054837 00000 n
0000088111 00000 n
0000088211 00000 n
trailer
<</Size 228/Root 221 0 R/Info 222 0 R/ID [<e8f997fdc4d9ee619c59add6882586d8><d13406b4c1e0cb3de6c1e8dd21d6be13>]/Prev 50291>>
startxref
89038
%%EOF
Yet, when we analyzed other API outputs, we also saw structure as follows:
/Type/Sig
/Type/XObject/Subtype/Form
/Type/Metadata/Subtype/XML
/Type/Catalog
/Type/ObjStm/N 4
34 0 obj
<</Length 52/Filter/FlateDecode/Size 35/Root 8 0 R/Info 6 0 R/ID [<4dc91a1875a6d707aec203bb021c93a0><b6fc5ae423a75860537207ff441de268>]/W[1 2 2]/Type/XRef/Index[0 2 6 1 8 2 28 7]/Prev 116>>stream
xœc``øÿŸq‘-ãB9 ±Şš‰A™QÈ]Pá%AXŒ ‚‰¤ =› ñ
endstream
endobj
startxref
45383
%%EOF
We could not find any reference to this difference in the PDF Standarts document nor Adding e-Signature to PDF documents guidelines. Can you please give information on when to use which format.
Thanks in advance
Your question is totally unrelated to iText, and even unrelated to digital signatures.
Let's start with PDF's that end like this:
xref
0 1
0000000000 65535 f
57 1
0000088682 00000 n
221 7
0000088804 00000 n
0000088450 00000 n
0000054970 00000 n
0000088289 00000 n
0000054837 00000 n
0000088111 00000 n
0000088211 00000 n
trailer
<</Size 228/Root 221 0 R/Info 222 0 R/ID [<e8f997fdc4d9ee619c59add6882586d8><d13406b4c1e0cb3de6c1e8dd21d6be13>]/Prev 50291>>
startxref
89038
%%EOF
You see the end-of-file marker (%%EOF) preceded with the starting positing of the cross-reference table (startxref, in this case the xref table starts at position 89038).
The cross-reference table defines the byte offsets of every object inside the PDF. In PDF versions prior to PDF 1.5, the Cross-reference table is added in plain text, and each byte offset is defined using ten digits. Hence the size of a file will be limited to 10 to the tenth bytes (approximately 10 gigabytes).
The maximum size of a PDF with a version older than PDF 1.5 is about 10 gigabytes.
Starting with PDF 1.5, you can also have this:
34 0 obj
<</Length 52/Filter/FlateDecode/Size 35/Root 8 0 R/Info 6 0 R/ID [<4dc91a1875a6d707aec203bb021c93a0><b6fc5ae423a75860537207ff441de268>]/W[1 2 2]/Type/XRef/Index[0 2 6 1 8 2 28 7]/Prev 116>>stream
xœc``øÿŸq‘-ãB9 ±Şš‰A™QÈ]Pá%AXŒ ‚‰¤ =› ñ
endstream
endobj
startxref
45383
%%EOF
We still have the end-of-file marker, and we still point to the starting point of the cross-reference table, but the entries of the cross-reference table are no longer visible in clear text. They are compressed: xœc``øÿŸq‘-ãB9 ±Şš‰A™QÈ]Pá%AXŒ ‚‰¤ =› ñ
This allows you to create files with a file size higher than 10 gigabytes. All of this is explained in ISO 32000-1 and ISO 32000-2.
This is a screen shot of the PDF specification:
If you have software that digitally signs documents, but only supports the format that predates PDF 1.5, I would seriously question whether that software supports digital signatures that meet today's standards. I recently saw a project where a developer still used SHA-1 as the hashing algorithm. Fortunately, I was able to explain that this was wrong by referring him to a blog about SHA-1, otherwise he'd probably have been fired if his employer found out (or sued if his customer discovered that he was paying for signatures that are no longer considered being safe).

pdf 1.2: How to display a graphical image?

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.

(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.