如何将图像插入到PDF源代码中

huangapple go评论71阅读模式
英文:

How to insert an image into PDF source code

问题

我正在尝试将JPEG图像插入PDF源代码中,但似乎无法弄清楚可能出错的原因是什么?文本正常显示,但图像根本不显示(任何PDF查看器中都没有显示错误消息)。

我假设这与我在内容流中引用图像的方式有关,或者可能与二进制数据如何复制有关,但我无法完全弄清楚出了什么问题。我已经提供了我认为是必要的最小代码,以显示我想要实现的内容。

...
3 0 obj
<<
/Type/Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 5 0 R
/Resources <<
    /Font <</F1 4 0 R>>
    /XObject <</I1 6 0 R>>
>>
>>
endobj
...
5 0 obj
<<
/Length 1
>>
stream
q
1 0 0 1 0 0 cm
/I1 Do
Q
BT/F1 10 Tf
100 632 Td(Hello world!)Tj
ET
endstream
endobj
6 0 obj
<<
/Type /XObject
/Subtype /Image
/Filter /DCTDecode
/Width 50
/Height 50
/ColorSpace /DeviceRGB
/BitsPerComponent 8
/Length 2291
>>
stream
[JPEG binary data here]
endstream
endobj
...
英文:

I am trying to insert a JPEG image into PDF source code and I can't quite seem to figure out what might be going wrong? Text displays fine but images don't appear at all (and no error message displays in any PDF viewers).

I am assuming it has something to do with how I reference the image in my content stream, or maybe how the binary data is copied over, but I can't quite figure out exactly what is going wrong. I have provided what I believe to be the necessary minimum code below to show what I am trying to achieve.

...
3 0 obj
<<
/Type/Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 5 0 R
/Resources <<
    /Font <</F1 4 0 R>>
    /XObject <</I1 6 0 R>>
>>
>>
endobj
...
5 0 obj
<<
/Length 1
>>
stream
q
1 0 0 1 0 0 cm
/I1 Do
Q
BT/F1 10 Tf
100 632 Td(Hello world!)Tj
ET
endstream
endobj
6 0 obj
<<
/Type /XObject
/Subtype /Image
/Filter /DCTDecode
/Width 50
/Height 50
/ColorSpace /DeviceRGB
/BitsPerComponent 8
/Length 2291
>>
stream
[JPEG binary data here]
endstream
endobj
...

答案1

得分: 1

通常情况下,您的文本编码的PDF是可用的,但正如@mkl在评论中指出的那样,图片很难找到,而且如果某些其他文本不如预期,Acrobat Reader通常会触发重建。(对于内容的长度为5 0 obj << /Length 1 >>的情况可能是这样的情况,这里已设置为<</Length 95>>

在此设置图像的联合大小和放置位置,将其设置为基线下方650个点的100 x 100个点。

100 0 0 100 100 650 cm
/I1 Do

这应该被视为一个最小的工作示例,没有触发器。
如何将图像插入到PDF源代码中

%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/Parent 2 0 R/MediaBox[0 0 612 792]/CropBox[0 0 612 792]/Rotate 0/Resources<</XObject<</I1 5 0 R>>/Font<</F1 4 0 R>>>>/Contents 6 0 R>>
endobj

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

5 0 obj
<</Type/XObject/Subtype/Image/ColorSpace/DeviceRGB/BitsPerComponent 8/Width 50/Height 50/Length 2020/Filter/DCTDecode>>
stream
[图像数据 ÿØÿà JFIF ....]
endstream
endobj

6 0 obj
<</Length 94>>
stream
q
100 0 0 100 100 650 cm
/I1 Do
Q
q
BT
/F1 10 Tf
1 0 0 1 100 632 Tm
(Hello world!) Tj
ET
Q
q
Q

endstream
endobj

xref
0 7
0000000000 65536 f 
0000000016 00000 n 
0000000062 00000 n 
0000000114 00000 n 
0000000278 00000 n 
0000000342 00000 n 
0000002### 00000 n 

trailer
<</Size 7/Root 1 0 R>>
startxref
2xxx
%%EOF

其中2xxx将根据图像大小和/或位置而变化。

英文:

Generally your text encoded PDF is workable but as pointed out by @mkl in comments the image will be exceptionally hard to find and often Acrobat Reader may trigger a rebuild if some other text is not as expected. (your length of 5 0 obj
<< /Length 1 >>
for the contents could be such a case, here it has been set as <</Length 95>>)

Set a joint size and placement for the image here this will be 100 x 100 points at 650 from page baseline.

100 0 0 100 100 650 cm
/I1 Do

This should be considered a minimal working example, without that trigger.
如何将图像插入到PDF源代码中

%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/Parent 2 0 R/MediaBox[0 0 612 792]/CropBox[0 0 612 792]/Rotate 0/Resources<</XObject<</I1 5 0 R>>/Font<</F1 4 0 R>>>>/Contents 6 0 R>>
endobj

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

5 0 obj
<</Type/XObject/Subtype/Image/ColorSpace/DeviceRGB/BitsPerComponent 8/Width 50/Height 50/Length 2020/Filter/DCTDecode>>
stream
[image data ÿØÿà JFIF ....]
endstream
endobj

6 0 obj
<</Length 94>>
stream
q
100 0 0 100 100 650 cm
/I1 Do
Q
q
BT
/F1 10 Tf
1 0 0 1 100 632 Tm
(Hello world!) Tj
ET
Q
q
Q

endstream
endobj

xref
0 7
0000000000 65536 f 
0000000016 00000 n 
0000000062 00000 n 
0000000114 00000 n 
0000000278 00000 n 
0000000342 00000 n 
0000002### 00000 n 

trailer
<</Size 7/Root 1 0 R>>
startxref
2xxx
%%EOF

where 2xxx will vary by image size and or location

huangapple
  • 本文由 发表于 2023年7月17日 22:56:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定