Search code examples
pdfsvgpdf-generation

Filling image in the text in PDF using PDF internal structure


In one of my application they are creating PDF from SVG. To construct PDF they are constructing buffer as PDF internal structure tree( header, body,xref table,trailer etc). In SVG I have tspan element which has fill attribute with URl of image pattern. I am using below PDF internal structure for filling text with image but I am getting blank PDF page. The image is jpeg image. please help me to resolve the issue.

15 0 obj 
<< /Type /Pattern 
/PatternType 1 
/PaintType 1 
/TilingType 1 
/BBox [0 0 60 60] 
/XStep 60 
/YStep 60 
/Resources 16 0 R 
/Length 12 
>> 
stream
q
/img1 Do
endstream
Q

BT 
/F1 1 Tf 
1 0 0 -1 0 0 Tm 
0 0 0 1 K
15.0 W
0 J
0 0 0 1 k
BX /ca0 gs EX
(HELLO) Tj 
/Pattern cs 
/P1 scn 
0 0 TD 
(HELLO) Tj 
ET

Solution

  • First of all and already discussed in comments, you draw a bitmap image in your pattern content stream

    /img1 Do
    

    without setting the current transformation matrix to scale it before. Thus, your image will be drawn in a 1×1 user space unit square (by default 1/72 in × 1/72 in). This surely is not what you want.

    You already corrected this in the comments

    I used BBox as 0,0 610 458 and xstep as 610 and ystep as 458 and cm as 610 0 0 458 0 0 but still image is not visisble.

    Looking for more issues, therefore, one finds in the file you shared:

    1. In you text object you set graphics state parameters using
      BX /ca0 gs EX
      
      Looking up the ca0 resource, one sees that this sets the alpha constant for non-stroking operations to 0. Thus, any following filling operations will be invisible! As you want to see the image you fill your text with, this is completely wrong. Use 1 instead of 0. If you actually want to reduce opaqueness, start with 1 and then reduce the value until the effect pleases you.
    2. When adding the cm operation to your Pattern definition, you appear to have accidentally dropped the image drawing operation. Thus, add the image drawing operation again.
    3. You use the same Resources dictionary for page and pattern. Apparently Adobe Acrobat does not like that. Thus, create a dedicated Resources dictionary for the pattern which then refers to the XObject dictionary from the page resources dictionary.

    After these changes your PDF shows

    screenshot

    (BTW, the integrated PDF viewer in Chrome already was happy with only the first two changes...)