Reputation: 4540
I have a pdf (of a simple line graph) which was generated using R, and is just stored with plain text streams. The data used to generate the graph has been lost, and I would like to modify the axis labels by editing the pdf.
I found this line:
/F2 1 Tf 12.00 0.00 -0.00 12.00 238.73 18.72 Tm (**Error Rate**) Tj
Which appears to control the axis label I want (which currently says "Error Rate"). Changing it to say:
/F2 1 Tf 12.00 0.00 -0.00 12.00 238.73 18.72 Tm (**Different Label**) Tj
Does indeed result in the axis label changing to "Different Label".
Now, I want the new label to be "Mu". As in, the greek letter Mu. I know this is possible, since I can generate pdf's in R with greek letters in their axis labels.
My first thought was to manually enter the UTF-8 charactor for Mu using the vim trigraph ctrl+k m*, and also through the character map and the like, to give:
/F2 1 Tf 12.00 0.00 -0.00 12.00 238.73 18.72 Tm ( μ ) Tj
If I attempt to write the file after doing this, I get an error message "CONVERSION ERROR in line xyz", where xyz is the modified line. Opening the saved pdf reveals a '?' for the axis label.
How does the pdf encode mu? How can I modify the label accordingly?
Upvotes: 3
Views: 1593
Reputation: 10418
PDF files are binary files, modifying them as text files will corrupt them in most cases. In order to keep the file valid you need to update the xref table (see the PDF specification for more details). If the byte count of the whole file changes, you will need to update each entry of the xref table for the file to remain valid. Another option could be to remove the xref table all together and pass the resulting file through another tool that can "guess" it for you. I have done this with ghostscript in the past with good results.
About the issue with fonts, which font /F2 corresponds to? Is it partially embedded in the PDF file? If it is, you probably do not have the required information in the file to add the character μ.
Upvotes: 4
Reputation: 4591
As @yms pointed out, PDFs are typically not editable in a text editor, as they most likely contain binary data and have an xref table that needs to be updated if chracters are inserted or deleted in the PDF. If you must edit a pdf, use qpdf to edit the PDF.
Upvotes: 2
Reputation: 226871
I tried this:
pdf("testmu.pdf",compress=FALSE)
plot(1:10,1:10,xlab="abc",ylab=expression("LABEL "*mu))
dev.off()
and found the following chunk in the resulting file:
BT
/F2 1 Tf 0.00 12.00 -12.00 0.00 10.28 235.40 Tm (LABEL ) Tj
ET
BT
/F6 1 Tf 0.00 12.00 -12.00 0.00 10.28 276.09 Tm (m) Tj
ET
So I suspect if you use
/F6 1 Tf 12.00 0.00 -0.00 12.00 238.73 18.72 Tm ( m ) Tj
in your example above it should work. I don't know whether R always defines F6
(Symbol font), so you might also need to hack in something along these lines:
13 0 obj
<< /Type /Font /Subtype /Type1 /Name /F6 /BaseFont /Symbol
>>
edit: as pointed out in the other answer, and the comment below it, it seems you also need to manually update the xref count by (I think) searching for xref
, finding a chunk like
xref
0 13
and incrementing the second value ...
Upvotes: 3