Search code examples
pdfpdf-generation

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:

Desired result from my code

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.

Solution

  • 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

    single byte line endings

    and this in the second case

    two byte line endings

    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

    no line endings inbetween

    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.