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

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

How to insert an image into PDF source code

问题

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

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

  1. ...
  2. 3 0 obj
  3. <<
  4. /Type/Page
  5. /Parent 2 0 R
  6. /MediaBox [0 0 612 792]
  7. /Contents 5 0 R
  8. /Resources <<
  9. /Font <</F1 4 0 R>>
  10. /XObject <</I1 6 0 R>>
  11. >>
  12. >>
  13. endobj
  14. ...
  15. 5 0 obj
  16. <<
  17. /Length 1
  18. >>
  19. stream
  20. q
  21. 1 0 0 1 0 0 cm
  22. /I1 Do
  23. Q
  24. BT/F1 10 Tf
  25. 100 632 Td(Hello world!)Tj
  26. ET
  27. endstream
  28. endobj
  29. 6 0 obj
  30. <<
  31. /Type /XObject
  32. /Subtype /Image
  33. /Filter /DCTDecode
  34. /Width 50
  35. /Height 50
  36. /ColorSpace /DeviceRGB
  37. /BitsPerComponent 8
  38. /Length 2291
  39. >>
  40. stream
  41. [JPEG binary data here]
  42. endstream
  43. endobj
  44. ...
英文:

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.

  1. ...
  2. 3 0 obj
  3. <<
  4. /Type/Page
  5. /Parent 2 0 R
  6. /MediaBox [0 0 612 792]
  7. /Contents 5 0 R
  8. /Resources <<
  9. /Font <</F1 4 0 R>>
  10. /XObject <</I1 6 0 R>>
  11. >>
  12. >>
  13. endobj
  14. ...
  15. 5 0 obj
  16. <<
  17. /Length 1
  18. >>
  19. stream
  20. q
  21. 1 0 0 1 0 0 cm
  22. /I1 Do
  23. Q
  24. BT/F1 10 Tf
  25. 100 632 Td(Hello world!)Tj
  26. ET
  27. endstream
  28. endobj
  29. 6 0 obj
  30. <<
  31. /Type /XObject
  32. /Subtype /Image
  33. /Filter /DCTDecode
  34. /Width 50
  35. /Height 50
  36. /ColorSpace /DeviceRGB
  37. /BitsPerComponent 8
  38. /Length 2291
  39. >>
  40. stream
  41. [JPEG binary data here]
  42. endstream
  43. endobj
  44. ...

答案1

得分: 1

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

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

  1. 100 0 0 100 100 650 cm
  2. /I1 Do

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

  1. %PDF-1.7
  2. %ÄѧÎ
  3. 1 0 obj
  4. <</Type/Catalog/Pages 2 0 R>>
  5. endobj
  6. 2 0 obj
  7. <</Type/Pages/Count 1/Kids[3 0 R]>>
  8. endobj
  9. 3 0 obj
  10. <</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>>
  11. endobj
  12. 4 0 obj
  13. <</Type/Font/BaseFont/Helvetica/Subtype/Type1>>
  14. endobj
  15. 5 0 obj
  16. <</Type/XObject/Subtype/Image/ColorSpace/DeviceRGB/BitsPerComponent 8/Width 50/Height 50/Length 2020/Filter/DCTDecode>>
  17. stream
  18. [图像数据 ÿØÿà JFIF ....]
  19. endstream
  20. endobj
  21. 6 0 obj
  22. <</Length 94>>
  23. stream
  24. q
  25. 100 0 0 100 100 650 cm
  26. /I1 Do
  27. Q
  28. q
  29. BT
  30. /F1 10 Tf
  31. 1 0 0 1 100 632 Tm
  32. (Hello world!) Tj
  33. ET
  34. Q
  35. q
  36. Q
  37. endstream
  38. endobj
  39. xref
  40. 0 7
  41. 0000000000 65536 f
  42. 0000000016 00000 n
  43. 0000000062 00000 n
  44. 0000000114 00000 n
  45. 0000000278 00000 n
  46. 0000000342 00000 n
  47. 0000002### 00000 n
  48. trailer
  49. <</Size 7/Root 1 0 R>>
  50. startxref
  51. 2xxx
  52. %%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.

  1. 100 0 0 100 100 650 cm
  2. /I1 Do

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

  1. %PDF-1.7
  2. %ÄѧÎ
  3. 1 0 obj
  4. <</Type/Catalog/Pages 2 0 R>>
  5. endobj
  6. 2 0 obj
  7. <</Type/Pages/Count 1/Kids[3 0 R]>>
  8. endobj
  9. 3 0 obj
  10. <</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>>
  11. endobj
  12. 4 0 obj
  13. <</Type/Font/BaseFont/Helvetica/Subtype/Type1>>
  14. endobj
  15. 5 0 obj
  16. <</Type/XObject/Subtype/Image/ColorSpace/DeviceRGB/BitsPerComponent 8/Width 50/Height 50/Length 2020/Filter/DCTDecode>>
  17. stream
  18. [image data ÿØÿà JFIF ....]
  19. endstream
  20. endobj
  21. 6 0 obj
  22. <</Length 94>>
  23. stream
  24. q
  25. 100 0 0 100 100 650 cm
  26. /I1 Do
  27. Q
  28. q
  29. BT
  30. /F1 10 Tf
  31. 1 0 0 1 100 632 Tm
  32. (Hello world!) Tj
  33. ET
  34. Q
  35. q
  36. Q
  37. endstream
  38. endobj
  39. xref
  40. 0 7
  41. 0000000000 65536 f
  42. 0000000016 00000 n
  43. 0000000062 00000 n
  44. 0000000114 00000 n
  45. 0000000278 00000 n
  46. 0000000342 00000 n
  47. 0000002### 00000 n
  48. trailer
  49. <</Size 7/Root 1 0 R>>
  50. startxref
  51. 2xxx
  52. %%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:

确定