4

I am trying to add some text to a pdf file manually.I was able to add new text with a specific font. But i am not able to set the font color. So how can i do it manually? (I just want to change these manually as i already have the code where i write these byte to make the pdf file) Also how can i use graphic states specified in the pdf standard to manipulate the text so that feature changes does not affect the color changes etc.How exactly i can use the graphic state?

Source pdf file click here Modified pdf file clcik here

Behrang
  • 46,888
  • 25
  • 118
  • 160
IT researcher
  • 3,274
  • 17
  • 79
  • 143
  • What do you mean by adding text manually? Is it related to programming at all or do you mean in Adobe Acrobat? – ellak Mar 06 '13 at 07:33
  • Not by using adobe acrobat.I want to do it using programming.For that i want to change objects of the pdf. – IT researcher Mar 06 '13 at 08:18
  • Can you show the code that you have so far? What programming language are you using? – ellak Mar 06 '13 at 08:20
  • Your tag *adobe* is misleading as you neither want to use Adobe libraries to create the PDFs nor have any problems specific to Adobe PDF viewers. – mkl Mar 06 '13 at 08:36

1 Answers1

15

The PDF color operators are listed in Table 74 of the PDF specification ISO 32000-1:2008.

In your case your added content stream is

104 0 obj
<</Length 105 0 R>>stream
  /Helv 8 Tf
  BT
  1 0 0 1 15.67 150 Tm
  (l)Tj
  ET
  /Helv 8 Tf
  BT
  1 0 0 1 17.88 190 Tm
  (abcdefghijklmnopqr)Tj
  ET
endstream
endobj 

If e.g. you want the writing to be filled with red in a RGB color space, you add an 1 0 0 rg:

104 0 obj
<</Length 105 0 R>>stream
  BT
  1 0 0 1 15.67 150 Tm
  /Helv 8 Tf
  1 0 0 rg
  [...]

EDIT

If you are afraid that that change may affect later text, remember to use the Graphics State Stack operators q and Q (cf. section 8.4.2 of the PDF specification). E.g.

q
0 1 -1 0 595.22 0 cm
q
BT
1 0 0 1 36 540 Tm
/Xi0 12 Tf
0.75 g
(Hello people!)Tj
0 g
ET
Q
Q

(Copied from How to add text object to existing pdf)

Community
  • 1
  • 1
mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thank you. If i use the color by specifying such as 1 0 0 rg then does it effect all fonts and other objects? Because i don't want to effect this color changes to all the text in my pdf file. – IT researcher Mar 06 '13 at 09:15
  • I was able to add text with color which i wanted.But the resulting pdf does not open only in Windows 8 reader(the application in windows 8 for pdf view) . in adobe reader it opens properly.How can i solve it? – IT researcher Mar 06 '13 at 09:27
  • Please supply it. I assume unmatched operator pairs. – mkl Mar 06 '13 at 09:34
  • Or with "manual" you mean that you've opened the PDF with text file reading/writing code to insert the additional instructions. If so, you've also screwed up the cross-reference table at the end of the file which would explain why it still opens in Adobe Reader (as it is very good at repairing incorrect XRef tables)... – David van Driessche Mar 06 '13 at 09:49
  • Concerning *does it effect all fonts and other objects* --- the fill color change affects all subsequent objects which respect fill colors. Thus, **remember to use the Graphics State Stack operators** for which I added a reference to their description to the answer above. – mkl Mar 06 '13 at 10:33
  • @DavidvanDriessche As far as I followed winman's questions so far, he has to implement PDF manipulating code in VB6 without being able (willing? allowed?) to make use of existing, time-proven PDF libraries. He hardened his cross reference table manipulation skills in the context of the question [Text object gets deleted in Adobe Reader 11](http://stackoverflow.com/questions/15083341/text-object-gets-deleted-in-adobe-reader-11/15085307). – mkl Mar 06 '13 at 10:40
  • @mkl can u please give an examples of where should i use graphics state q and Q? or how to use it? – IT researcher Mar 06 '13 at 11:40
  • @winman I added a sample which I copied from my answer to your former question [How to add text object to existing pdf](http://stackoverflow.com/questions/15151097/how-to-add-text-object-to-existing-pdf/15198301#15198301). – mkl Mar 06 '13 at 13:45
  • 1
    In case it helps anyone else, also note the difference between `rg` (fill colour) and `RG` (stroke colour). Was modifying some pdf files today and couldn't figure out why the text colour wasn't changing as I expected. Turns out I had incorrectly modified `RG` when I needed to modify `rg`. – Stéphane Apr 12 '22 at 21:30