I am demoing an idea I have been playing around with, and while the Adobe specification says that including PS XObjects is not a good idea, some PDF readers should still support this functionality. Anyways, that is beside the fact. I have been using the Adobe PDF specification and have the following PDF object. This merely uses PostScript to generate a pseudo random value and then print it to the page. Ideally, each time this page is rendered a new value should display:
5 0 obj
<< /Type/XObject
/Subtype/PS
/Length 103
>>
stream
/Times findfont 10 scalefont setfont
/str 32 string def
10 20 moveto
rand str cvs show
endstream
endobj
Each time any PDF viewer I have tested this against reads this object I get errors such as:"Error (741): Missing 'endstream'" And similarly for every token in that stream. I am sure my offsets are correct. And while I know my PDF viewer does support some PS for forms and such, is there anything obviously incorrect. If anyone has a sample PDF I can go from, that would be nice. The form examples that I tested my reader against have not been too helpful. If I run just the PS code from GhostView it works fine. Thanks for any insight.
I've scoured my back collection of PDF files and come up with 2 which contain PS XObjects (this really is deprecated). I can't, unfortunately, share tehm as they are customer data files :-(
However, here is an extract from one of them:
74 0 obj
<<
/Type /XObject
/Subtype /PS
/Filter /FlateDecode
/Length 77 0 R
/Name /Ps1
>>
stream
....endstream
Note 1, there is no EOL between the end of data and the 'endstream' token.
77 0 obj
4480
endobj
The offset of the 0x0A following the 'stream' token is 0xdab15, the offset of the 'e' in endstream is 0xdbc96. That is 4481 bytes. SO it looks to me like the /Length should contain all the bytes after the EOL for the 'stream' token' right up to the last byte before the 'e' in the endstream token.
I think it would be OK to insert a 0x0A after the stream data and before the endstream. That would come down to a whitespace after the stream data before the token, and PDF is supposed to be tolerant of whitespace.
This is consistent with the description of the /Length entry for stream dictionaries in Table 3.4 (p62 of the 1.7 PDF reference):
The number of bytes from the beginning of the line fol-lowing the keyword stream to the last byte just before the keyword endstream. (There may be an additional EOL marker, preceding endstream, that is not included in the count and is not logically part of the stream data.) See “Stream Extent,” above, for further discussion.
I think (if I've counted correctly) that the /Length in your example should be 87, assuming one byte line terminators in the PostScript fragment.
Related
I'm using this library https://github.com/vbuch/node-signpdf to sign a pdf document. After I have signed the document I can see the signature when I open the pdf with Foxit reader but not when I open it with Adobe reader DC. I also tried Adobe reader XI but there I can't see it either.
When I open the document in pdf xchange viewer I get this error: non critical errors detected in the xref table.
Any ideas what the problem could be?
That's the file I signed: https://drive.google.com/file/d/1AZvS4sP2Y3FwW4Deod87Dgxc9I0QZkoc/view?usp=sharing
In your example PDF the name of the signature field consists of 10 bytes, 9 bytes with value 0x00 and one byte with value 0x01. Apparently Adobe Reader does not like that field name.
After some experiments it looks like Adobe Reader does not like a field name starting with a 0x00 byte.
Maybe it contains some code that determines string lengths in a c'ish manner and interprets a 0x00 as end-of-string. A field name with a leading 0x00 byte, therefore, is interpreted as empty string, a field name not accepted by Adobe Reader either.
Thus, please use a signature field name made of (in particular starting with) some meaningful characters. As validators usually display the name of the signature field, this is a good idea anyways.
In terms of lowlevel PDF objects:
The signature field object looks like this:
18 0 obj
<<
/Type /Annot
/Subtype /Widget
/FT /Sig
/Rect [0 0 0 0]
/V 17 0 R
/T ( )
/F 4
/P 1 0 R
>>
endobj
but only like this, the string value of the T entry actually contains the above mentioned nine 0x00 bytes and one 0x01 byte. This is the value that must be changed to a non-empty string not starting with 0x00. I would propose not using bytes < 0x20 at all. Furthermore, the dot, 0x2e, must not be used in the name, it is reserved for separating partial names.
I'm tring to understand PDF struckture right now but I have a little Problem with calculating the byte offset of an String. The offsets of the objects are couted fom the begining of the file to the index of the object (6 0 obj).
I have a working hello world PDF file but when I count the offsets I get a diffrent offset than in the xref table.
If anybody understands how this is counted please let me know!
Example:
0 6 obj xref:9 me:17
0 1 obj xref:60 me:72
0 4 obj xref:145 me 187
(I count with "\r\n" (2) as line break)
Adobe Standart:http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf
%PDF-1.4
%%EOF
6 0 obj
<<
/Type /Catalog
/Pages 5 0 R
>>
endobj
1 0 obj
<<
/Type /Page
/Parent 5 0 R
/MediaBox [ 0 0 612 792 ]
/Resources 3 0 R
/Contents 2 0 R
>>
endobj
4 0 obj
<<
/Type /Font
/Subtype /Type1
/Name /F1
/BaseFont/Helvetica
>>
endobj
2 0 obj
<<
/Length 53
>>
stream
BT
/F1 24 Tf
1 0 0 1 260 600 Tm
(Hello World)Tj
ET
endstream
endobj
5 0 obj
<<
/Type /Pages
/Kids [ 1 0 R ]
/Count 1
>>
endobj
3 0 obj
<<
/ProcSet[/PDF/Text]
/Font <</F1 4 0 R >>
>>
endobj
xref
0 7
0000000000 65535 f
0000000060 00000 n
0000000228 00000 n
0000000424 00000 n
0000000145 00000 n
0000000333 00000 n
0000000009 00000 n
trailer
<<
/Size 7
/Root 6 0 R
>>
startxref
488
%%EOF
This is a very interesting file and reading the PDF specification initially just confused me more :-). In such cases (I'll madden some people with this) I would simply save the example PDF file and do as #KenS suggests in his previous answer; open it in Acrobat and if Acrobat reports it as damaged or asks you to save when you close the file - it doesn't like it and you can assume you've gotten it wrong.
The reason this file is interesting is the second line, the:
%%EOF
I don't agree with KenS that having this line automatically invalidates the file - I can find no text in ISO 32000 that states this. The text says that the %%EOF line at the end of a file has syntactical meaning (and explains why it is there) and it states that any line beginning with a percentage character (%) is a comment and what that means. But nowhere does it state that %%EOF is not allowed as comment somewhere else in the file (though I consider it a dumb thing to do but that is something different).
If that %%EOF line isn't there, the XREF table is correct. If it is there, its wrong. Some more explanation of what I read in the documentation:
1) As far as I understand the offset is starting from the first byte of the file (it's a byte offset, not a character offset) which is "0" and then counts up. The idea behind this is that you can open a file, set the file read position to a given offset and start reading. So if you open up the file in a binary editor that shows real bytes, the offset should match what you're seeing there. If your %%EOF line isn't there, that means the first object (6 0 obj) effectively begins at offset 9 (if you line ending character here is a single byte line ending). At this point it matches what is given as an example in the PDF specification itself, so I'm confident that offset of 9 is correct provided that second line (%%EOF) would not be in the PDF file.
2) That second line starts with a percentage sign which makes it a comment. The PDF specification states that a comment (everything from the % sign up to but not including the line end character) shall be interpreted as a single whitespace character. That's interesting and could lead to all kinds of speculation on what that means for the offset of the object following it but frankly all of that speculation is out of order and irrelevant because of what I stated before.
The idea behind this is that you can open a file, set the file read position to a given offset and start reading.
That's exactly what the cross-reference table is for and it should be taken literally. In other words, assuming single-byte line ending characters, object 6 in your example file starts at offset 15 and that's the number that should be in the XREF table for that object.
Again, take #KenS' comment into account, you cannot just assume the line ending is two bytes, you have to know what they are (and they could be mixed so you can't even assume all lines have the same). If this file would have two byte line endings for all lines, your count of 17 would be the correct one.
You cannot assume a line end is a \r\n pair, it could be \r, \n or \r\n, you need to use a binary editor to be certain. We also cannot tell you which value is correct without access to the original file, a cut/paste as above simply isn't good enough, sorry. Though 9 cannot be correct unless that %%EOF is spurious......
Your quoted PDF file isn't correct anyway, you should not have %%EOF as the second line, that 'ought' to be a binary sequence of bytes with the high bit set to make sure PDF files are transferred as binary files.
How do you know the xref in your PDF file is correct ? If you open it with Acrobat does it offer to save changes on exit ? That's a sure sign that Acrobat rebuilt the xref for you, because it was incorrect.....
[edited for clarity and because too long for comments]
I should be clearer in my explanation. I contracted 2 statements into 1 in my initial answer, and intended to clarify it in the comments. The file is incorrect, the xref offsets appear to me to be wrong, and on initial inspection appear to be wrong by the %%EOF (but see later), which looks like it is bogus in some way (inserted by cut/paste, or by an editor, or something).
Technically you can have any text which begins % anywhere in a PDF file (but outside string and stream), provided you account for it properly and don't break PDF syntax. But I still would not put two %%EOF comments in a PDF file, its too likely to confuse simple minded PDF consumers.
I don't think that having a comment before the 'x y obj' statement is necessarily wrong (I wouldn't do it, but that's not the same thing). It also doesn't totally invalidate David's point about:
The idea behind this is that you can open a file, set the file read position to a given offset and start reading.
Provided that the PDF consumer is prepared to read a comment and doesn't expect whitespace or a 'x y obj' statement (and I have seen PDF files which have preceding whitespace here). That is debatable, and while I would read the spec to say that the xref offset ought to point precisely to the first byte of the 'x y obj' line, it doesn't actually say that in so many words in the spec. And a PDF consumer does need to be able to deal with comments during the course of the object definition itself. For example I think that this:
1 0 obj
%% Here's a comment
<<
/Type /Page
/Parent 5 0 R
/MediaBox [ 0 0 612 792 ]
/Resources 3 0 R
/Contents 2 0 R
>>
Would be legal. The line begins with a '%', its not in a string or stream context, and doesn't break PDF syntax, the consumer should simply skip right over it.
This isn't very much different from:
%% Here's a comment
1 0 obj
<<
/Type /Page
/Parent 5 0 R
/MediaBox [ 0 0 612 792 ]
/Resources 3 0 R
/Contents 2 0 R
>>
Again, I wouldn't do that myself (or if I did I would point the xref to the start of the 1 0 obj, but I think its arguable that way.
But in the original example my binary editor says that (with 2 byte line endings) object 4 starts at offset 187, if I use 1 byte endings that comes down to 170. In order to get object one's xref to be correct I assume that the line endings are 1 byte, and I remove the "%%EOF\n". But subtracting 6 bytes from 170 still comes out at 164, so nowhere near the 145 the xref contains. I cannot see any way to get object 4 to be at location 145 without removing real PDF operators/structure.
In adobe acrobat x i was inserting text objects and when it is opened in adobe reader 10 it was opening properly.but in adobe reader 11 when i click on that pdf file text objects gets deleted.why this happens? How to solve it?
The source pdf file click here
The pdf file which has problem when double clicking on it in adobe reader 11.
click here
In a nutshell:
You try to change the contents of a free text annotation by changing its normal appearance stream.
This is insufficient: A compliant PDF viewer may ignore this entry and provide their own appearances. So it's mere luck that older Adobe Reader versions chose to not ignore your change.
Thus, you also need to change the information a PDF viewer is expected to create their own appearance from, i.e. foremost the rich text value of RC (in the free text annotation dictionary) that shall be used to generate the appearance of the annotation, and also the Contents value which is the Text that shall be displayed for the annotation.
Furthermore there are defects in your PDFs:
the cross reference table in your first attempt result.pdf was broken;
the intent (IT value) of the free text annotation in your source files is spelled incorrectly.
In detail:
Your result.pdf is broken. Different PDF viewers may display broken PDFs differently.
Some details:
It has been created based on your Src.pdf in append mode but additionally the following change in the original revision has been made to its /Pages object:
In the source:
6 0 obj
<</Count 6
/Type /Pages
/Kids [ 7 0 R 8 0 R 9 0 R 10 0 R 11 0 R 12 0 R ]
>>
endobj
In the result:
6 0 obj
<</Count 3
/Type /Pages
/Kids [ 7 0 R 8 0 R 9 0 R 12 0 R 11 0 R 10 0 R ]
>>
endobj
So the order of the last three pages was changed (which is ok) and the /Count was reduced from 6 to 3. This is inconsistent as there still are 6 child objects but according to the PDF specification ISO 32000-1, Count is
The number of leaf nodes (page objects) that are descendants of this node within the page tree.
Furthermore the cross reference stream of the appended revision is broken.
xref
0 1
0000000000 65535 f
24 1
0001465240 00000 n
57 1
0001466075 00000 n
66 1
0001466909 00000 n
73 1
0001467744 00000 n
93 1
0001473484 00000 n
131 1
0001478703 00000 n
The entries are 19 bytes long including their respectively ending single byte newline character According to the spec, though,
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 [...] eol shall be a 2-character end-of-line sequence
There may be more errors in the PDF but you may want to start fixing these.
EDIT
Now with the new PDF Pay-in.pdf with a proper cross reference at hand, let's look at it more in-depth.
Adobe Preflight complains about many occurances of:
[...]
An unexpected value is associated with the key
Key: IT
Value: /FreeTextTypewriter
Type: CosName
Formal Representation: Annot.AnnotFreeText
Cos ID: 86
Traversal Path: ->Pages->Kids->[0]->Annots->[13]
[...]
Ok, let's look at that object 86:
86 0 obj
<< /P 8 0 R
/Type /Annot
/CreationDate (D:20130219194939+05'30')
/T (winman)
/NM (0f202782-2274-44b8-9081-af4010be86d4)
/Subj (Typewritten Text)
/M (D:20130219195100+05'30')
/F 4
/Rect [ 53.2308 33.488 552.088 826.019 ]
/DS (font: Helv 12.0pt;font-stretch:Normal; text-align:left; color:#000000 )
/AP <</N 107 0 R >>
/Contents (wwww)
/IT /FreeTextTypewriter
/BS 108 0 R
/Subtype /FreeText
/Rotate 90
/DA (16.25 TL /Cour 12 Tf)
/RC (<?xml version="1.0"?>
<body xmlns="http://www.w3.org/1999/xhtml"
xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"
xfa:APIVersion="Acrobat:10.0.0"
xfa:spec="2.0.2"
style="font-size:12.0pt;text-align:left;color:#000000;font-weight:normal;
font-style:normal;font-family:Helv;font-stretch:normal">
<p dir="ltr">
<span style="line-height:16.3pt;font-family:Helvetica">wwww</span>
</p>
</body>)
>>
endobj
Preflight stated that it is unhappy about the line /IT /FreeTextTypewriter. Looking at the PDF specification again uncovers for annotations with /Subtype /FreeText, i.e. Free Text Annotations specified in section 12.5.6.6:
IT name
(Optional; PDF 1.6) A name describing the intent of the free text annotation (see also the IT entry in Table 170). The following values shall be valid:
FreeText The annotation is intended to function as a plain free-text annotation. A plain free-text annotation is also known as a text box comment.
FreeTextCallout The annotation is intended to function as a callout. The callout is associated with an area on the page through the callout line specified in CL.
FreeTextTypeWriter The annotation is intended to function as a click-to-type or typewriter object and no callout line is drawn.
Default value: FreeText
Thus, your value FreeTextTypewriter is invalid (remember, PDF names are case sensitive!). Therefore, the annotation is (slightly) broken which may already result in all kinds of problems.
But there are other important entries here, too, to understand your issue: All you do in your appended changes is to replace the appearance stream in object 107 (as per /AP <</N 107 0 R >>) of this annotation by a different one. But this annotation contains an RC value, too, which according to the specification is
A rich text string (see 12.7.3.4, “Rich Text Strings”) that shall be used to generate the appearance of the annotation.
Thus, any PDF viewer may regenerate the appearance from that rich text description, especially as the specification in section 12.5.2 says about the content of the AP dictionary
Individual annotation handlers may ignore this entry and provide their own appearances.
Thus, simply replacing the normal appearance stream does not suffice to permanently change the appearance of that annotation, you have to change the appearance dictionary and at least remove any alternative source for the appearance.
Furthermore the entry /Contents (wwww) is not replaced by your appended changes either. So a PDF viewer trying to decide whether to use the appearance stream or not will feel tempted to somehow create a new appearance as your appearance stream in no way represents that value.
Especially when starting to manipulate the free text (e.g. when clicking into the PDF in your case), the PDF viewer knows it eventually will have to create a new appearance anyways, and unless the current appearance is as it would have created it anyway, the viewer may prefer to begin anew starting with an appearance derived from the rich text or even the contents value.
I want to create a PDF file which does not contain angle brackets in it's source.
This apparently implies not using the dictionary data type, as this involves << and >>.
Is it possible to completely avoid angle brackets and still create a PDF file with formatted content?
Can it be done hiding in a stream, using a character encoding technique or with an alternative dictionary notation?
The solution is needed for an obfuscation technique; the bracket-problem cannot be circumvented.
I think this is not possible. Every element in a PDF file is contained in some dictionary: the document catalog (the root dictionary), the page objects, the page content streams, all of them are dictionaries that require the char sequence << >>.
Sample catalog dictionary:
1 0 obj
<<
/Pages 2 0 R
/Type /Catalog
>>
endobj
If you want to use a "instructions sequence only" presentation format, you may try using PostScript instead.
Edit after comments:
Using a stream object with some filter encoding will not solve your problem, since you still need to specify the filter type in the stream dictionary.
Example:
5 0 obj
<</Length 6 0 R /Filter /FlateDecode>>
stream
***illegible characters***
endobj
I'm looking for a grammar of PDF 1.7 (BNF or variant)
absolutely not googleable
I am not aware of any formal specification of the PDF file format in the form of a grammar, BNF or not.
But I happen to know for sure that the ISO technical committee 171/SC2 which currently works on the specification of PDF-2.0 has an agenda topic of "Updates from ad hoc committees: [...] iv. File format syntax for validating PDF files (L. Rosenthol)" for its next face to face meeting taking place in Berlin, Sept 11-12 2012. -- Which agenda item I take as "some more people seem to be interested in a more formal description of the PDF syntax"... :-)
Leonard Rosenthol is an Adobe PDF higher-up, and he frequently answers questions in the Adobe user forums. Maybe it is a good idea to ask a question there? Chances are, there you'll get a better answer than here.
PDF is a binary format that is not context-free. In PDF for example you need to read and interpret the size of a binary stream before parsing the stream.
Example:
10 0 obj
<</Type /XObject
/Subtype /Image
/Width 260
/Height 52
/ColorSpace /DeviceRGB
/SMask 10 0 R
/BitsPerComponent 8
/Filter /FlateDecode
/Length 4570>> stream
--- insert binary data here ---
endstream
endobj
There is no way to tell if your binary data will contain the tokens endstream or endobj inside, so you have no other choice than reading the length of the stream before parsing it.
BNF can only be used for context-free grammars, so it is not possible to construct a BNF grammar for PDF.
Take a look at the specification here:
PDF Reference Document