Please tell us how you can calculate the values in the TJ array?
From the question
example:
1 0 0 1 0 188.28799438 cm
BT
/F2 11.04 Tf
1 0 0 -1 0 9.38000011 Tm
(Some)Tj ( )Tj
21.24200058 0 Td
(text)Tj ( )Tj
Which I want to turn into this:
1 0 0 1 0 188.28799438 cm
BT
/F2 11.04 Tf
1 0 0 -1 0 9.38000011 Tm
[(S)10(o)11(m)12(e)( )]TJ
21.24200058 0 Td
[(t)10(e)10(x)10(t)( )]TJ
How values are calculated(10) (11) (12) ?
so if I open any pdf and find THESE values there, how can I check them? how do they appear?
I gone through different solutions on CTM matrix calculations(someof them are this and this).
What I know about content stream is when "q " encounters we need to push identity matrix in a graphics_stack and keep multiply with next position operator(cm , Tm, Td, TD) CTM. When "Q" encounters we need to pop the last matrix.
For text positioning parsing when "BT" encounters push in identity matrix in position_stack and keep multiply with next position operator(cm , Tm, Td, TD) CTM. When "ET" encounters we need to pop the last matrix.
Here the some times we need to multiply with last CTM matrix and some times just multiply with identity matrix. When these cases are occurs?
Case 1:
From as shown in image 1 and 2 case merely replacement of new matrix from Td to Tm. And from 2 to 3 it's again last CTM multiplication. How I know?(By visually I can tell by looks)
Case 2:
In this case which how the matrix pushing and calculation will be there ?
Case 3:
BT
TT_1 20 Tf
35.56 150.24533 Td _______________ 1
(some sample text) Tj
50.526 250.36 Td ________________ 2
(second line new replace) Tj
0 -16.2 Td _____________________ 3
(Line end.) Tj
ET
This case 1 and 2 are merely replacement, 2 and 3 previous matrix multiplication. How do I know?
Case 4:
Please parse these positions at least till 10. The source file of this pdf
Case 5:
In above one need to calculate the l position. I highlighted with 1 to 4 numbers. I need to calculate the positions of each and every l How can do that? pdf
case6:
What is the change in calculation when the page(pdf) is rotated to 90 or 180 or 270 and 315 degrees?
These are some cases what I saw. What else might cases I can encounter and What is the generic approach to solve this ?
Operators of interest
First of all, I get the impression you mix up two different aspects. You have the current transformation matrix (CTM) and you have the text and text line matrices. The CTM is subject to cm, q, and Q. The text and text line matrices are subject to BT, Tm, Td, ... And to determine the exact position and direction of drawn text you need the product of text matrix and CTM at the time that text is drawn.
How those operators change the matrices, is described in the PDF specification, ISO 32000 part 1 or 2.
From ISO 32000-1 Table 57 – Graphics State Operators –
cm: Modify the current transformation matrix (CTM) by concatenating the specified matrix
q: Save the current graphics state including the CTM on the graphics state stack
Q: Restore the graphics state including the CTM by removing the most recently saved state from the stack and making it the current state
From ISO 32000-1 Table 107 – Text object operators –
BT: Begin a text object, initializing the text matrix, Tm, and the text line matrix, Tlm, to the identity matrix.
From ISO 32000-1 Table 108 – Text-positioning operators –
tx ty Td: Move to the start of the next line, offset from the start of the current line by (tx, ty). More precisely, this operator shall perform these assignments:
a b c d e f Tm: Set the text matrix, Tm, and the text line matrix, Tlm:
Furthermore, TD, T*, ', and " operate on Tm and Tlm in a way specified using the Td operator.
From ISO 32000-1 section 9.4.4 – Text Space Details –
Whenever a glyph is drawn, its entire transformation from text space may be represented by a text rendering matrix, Trm:
where Tfs is the current font size, Th is the current horizontal scaling factor, and Trise is the current text rise value.
After drawing that glyph, Tm is updated according to the glyph displacement
In horizontal mode tx is the displacement and ty is zero, in vertical mode tx is zero and ty is the displacement. The applicable value is calculated as
Example 1
In the following paragraphs I use rounded values to concentrate on the essentials.
The CTM starts as the Identity matrix, and as there is no cm operation here, it remains so all the time. Tm and Tlm on the other hand do change:
BT
Tm and Tlm are both set to the identity matrix
/GS0 gs
/T1_0 10 Tf
No change to Tm or Tlm.
317 65 Td
This multiplies a translation matrix as described above from the left to the former value of the Tlm and sets Tm and Tlm to the result:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm 317 65 1 0 0 1 317 65 1
As that former value was the identity, this may look like a replacement by the Td translation matrix but it actually is a multiplication.
(F)Tj
This draws a glyph transformed by the text rendering matrix
10 × 1 0 0 1 0 0 1 0 0 10 0 0
T = 0 10 0 * 0 1 0 * 0 1 0 = 0 10 0
rm 0 0 1 317 65 1 0 0 1 317 65 1
Thereafter Tm is updated as described above. Unfortunately we don't have the widths of the font T1_0, so we cannot calculate the updated value.
1 0 0 1 370 87 Tm
This sets the text matrix, Tm, and the text line matrix, Tlm, to the given matrix:
1 0 0
T = T = 0 1 0
m lm 370 87 1
So now we know the current Tm value again.
-47 -22 Td
This multiplies a translation matrix as described above from the left to the former value of the Tlm and sets Tm and Tlm to the result:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm -47 -22 1 370 87 1 323 65 1
[(igure)-251(2.3:)-621(P)16...] TJ
This draws the strings in the argument array with a starting text rendering matrix of
10 × 1 0 0 1 0 0 1 0 0 10 0 0
T = 0 10 0 * 0 1 0 * 0 1 0 = 0 10 0
rm 0 0 1 323 65 1 0 0 1 323 65 1
updating Tm again and again as described above.
Example 2
In the following paragraphs I use rounded values to concentrate on the essentials.
The CTM starts as the Identity matrix.
q
This saves the current graphics state, including the current CTM. As there is no Q operation in the example, though, we can ignore that for now.
.24 0 0 .24 91 740 cm
This updates the CTM by the given matrix:
0.24 0 0 1 0 0 0.24 0 0
CTM = 0 0.24 0 * 0 1 0 = 0 0.24 0
91 740 1 0 0 1 91 740 1
BT
Tm and Tlm are both set to the identity matrix
133 0 0 133 0 0 Tm
This sets the text matrix, Tm, and the text line matrix, Tlm, to the given matrix:
133 0 0
T = T = 0 133 0
m lm 0 0 1
/TT1.0 1 Tf
.002 Tc
No change to CTM, Tm, or Tlm.
[(The)1( )1(Long )1(Tai)1(l)]TJ
This draws the strings in the argument array with a starting text rendering matrix of
1 × 1 0 0 133 0 0 0.24 0 0 32 0 0
T = 0 1 0 * 0 133 0 * 0 0.24 0 = 0 32 0
rm 0 0 1 0 0 1 91 740 1 91 740 1
updating Tm again and again as described above.
Example 3
BT
TT_1 20 Tf
35.56 150.24533 Td _______________ 1
(some sample text) Tj
50.526 250.36 Td ________________ 2
(second line new replace) Tj
0 -16.2 Td _____________________ 3
(Line end.) Tj
ET
In the following paragraphs I use rounded values to concentrate on the essentials.
The CTM starts as the Identity matrix, and as there is no cm operation here, it remains so all the time. Tm and Tlm on the other hand do change:
BT
Tm and Tlm are both set to the identity matrix
TT_1 20 Tf
No change to Tm or Tlm.
36 150 Td
This multiplies a translation matrix as described above from the left to the former value of the Tlm and sets Tm and Tlm to the result:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm 36 150 1 0 0 1 36 150 1
As that former value was the identity, this may look like a replacement by the Td translation matrix but it actually is a multiplication.
(some sample text) Tj
This draws glyphs transformed by the text rendering matrix
20 × 1 0 0 1 0 0 1 0 0 20 0 0
T = 0 20 0 * 0 1 0 * 0 1 0 = 0 20 0
rm 0 0 1 36 150 1 0 0 1 36 150 1
Thereafter Tm is updated as described above. Unfortunately we don't have the widths of the font TT_1, so we cannot calculate the updated value.
51 250 Td
This multiplies a translation matrix as described above from the left to the former value of the Tlm and sets Tm and Tlm to the result:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm 51 250 1 36 150 1 87 400 1
So now we know the current Tm value again.
(second line new replace) Tj
This draws glyphs transformed by the text rendering matrix
20 × 1 0 0 1 0 0 1 0 0 20 0 0
T = 0 20 0 * 0 1 0 * 0 1 0 = 0 20 0
rm 0 0 1 87 400 1 0 0 1 87 400 1
Thereafter Tm is updated as described above. Unfortunately we don't have the widths of the font TT_1, so we cannot calculate the updated value.
0 -16 Td
This multiplies a translation matrix as described above from the left to the former value of the Tlm and sets Tm and Tlm to the result:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm 0 -16 1 87 400 1 87 384 1
So now we know the current Tm value again.
(Line end.) Tj
This draws glyphs transformed by the text rendering matrix
20 × 1 0 0 1 0 0 1 0 0 20 0 0
T = 0 20 0 * 0 1 0 * 0 1 0 = 0 20 0
rm 0 0 1 87 384 1 0 0 1 87 384 1
Thereafter Tm is updated as described above. Unfortunately we don't have the widths of the font TT_1, so we cannot calculate the updated value.
Example 4
We discussed example 4 in your answer and the comments to it.
Example 5
q 0.1 0 0 0.1 0 0 cm
/R108 gs
0 g
q
...
Q
0 0 1 rg
q
...
Q
4.05 w
0 G
722.023 4082.13 m
722.023 4490.28 l
S
723.961 4488.25 m
2872.98 4488.25 l
S
404.1 w
0 0 0.199951 0 K
723.961 4284.18 m
2872.98 4284.18 l
S
4.05 w
0 G
720 4080.2 m
2876.94 4080.2 l
S
2874.91 4082.13 m
2874.91 4490.28 l
S
0 g
q
Why does your view of those instructions show the numbers inaccurately? The above is copy&pasted from the stream contents, there is no need to change the numbers like that...
q
0.1 0 0 0.1 0 0 cm
Sets the CTM to
0.1 0 0
CTM = 0 0.1 0
0 0 1
/R108 gs
...
0 G
Nothing happens to the CTM
722.023 4082.13 m
722.023 4490.28 l
S
We have to apply the CTM to these coordinates
0.1 0 0
[722.023 4082.13] * 0 0.1 0 = [72.2023 408.213]
0 0 1
0.1 0 0
[722.023 4490.28] * 0 0.1 0 = [72.2023 449.028]
0 0 1
Thus, a line is stroked from 72.2023,408.213 to 72.2023,449.028.
723.961 4488.25 m
2872.98 4488.25 l
S
Just like above, a line is drawn from 72.3961,448.825 to 287.298,448.825.
404.1 w
0 0 0.199951 0 K
723.961 4284.18 m
2872.98 4284.18 l
S
And again, a line is drawn from 72.3961,428.418 to 287.298,428.418. The only notable thing here is that the line is quite wide, ca. 40 units, so this "line" actually looks more like a filled rectangle and represents the background of the text box with the Lorentz force characterization.
4.05 w
0 G
720 4080.2 m
2876.94 4080.2 l
S
Another line is drawn, narrow again and, therefore, looking like a line, this time from 72,408.02 to 287.694 408.02.
2874.91 4082.13 m
2874.91 4490.28 l
S
And finally the last line, this time from 287.491,408.213 to 287.491,449.028.
Example 4 solution:
The CTM starts as the Identity matrix.
q
This saves the current graphics state (Graphics state = Identity ---- 1).
1 0 0 1 62.692 277.671 cm
This updates the CTM by the given matrix:
1 0 0 1 0 0 1 0 0
CTM = 0 0 0 * 0 1 0 = 0 1 0 ----- 2
62.692 277.67 1 0 0 1 62.692 277.67 1
CTM is updated to above result
q
push the CTM save to graphics state(Graphics state = 1, 2)
q
Again push the same CTM matrix to graphics state(Graphics state = 1, 2, 2)
1 0 0 1 286.59 207.54 cm
Update the CTM by current matrix.
1 0 0 1 0 0 1 0 0
CTM = 0 1 0 * 0 0 0 = 0 1 0 ----- 3
286.49 207.54 1 62.692 277.67 1 349.18 485.21 1
CTM updated to above result matrix.
q
push the CTM save to graphics state(Graphics state = 1, 2,2,3)
.75 .75 .75 RG
n
11.33 19.84 171.67 232.146 re
S
This will nothing change in position matrices.
Q
Assign the CTM to last available graphics state CTM = 3. Q will remove the last graphics state from graphics stack.(Graphics state = 1, 2,2)
1 0 0 1 17.007 23.52 cm
Update the CTM by current matrix.
1 0 0 0 1 0 0 1 0
CTM = 0 1 0 * 0 0 0 = 0 1 0 ----- 4
17.007 23.52 1 349.18 485.21 1 366.18 508.73 1
CTM updated to above result matrix.
q
push the last CTM to graphics state(Graphics state = 1, 2,2,4)
Skip rg, RG
BT
assign Tm and Tlm to Identity matrix.
1 0 0 1 0 5.6 Tm
This sets the text matrix, Tm, and the text line matrix, Tlm, to the given matrix:
1 0 0
T = T = 0 1 0
m lm 0 5.6 1
Tm and Tlm updated.
46.22 0 Td
Translate the matrix according to above Td matrix.
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm 46.22 0 1 0 5.6 1 46.22 5.6 1
update the Tm and Tlm with above matrix. Next Tf you can leave.
12 TL
This sets the graphics state leading text parameter, no direct influence on Tm or Tlm.
(William Shakespeare) Tj
This draws the string glyph-by-glyph, the first glyph transformed by this text rendering matrix
8 × 1 0 0 1 0 0 1 0 0 8 0 0
T = 0 8 0 * 0 1 0 * 0 1 0 = 0 8 0
rm 0 0 1 46.22 5.6 1 366.18 508.73 1 412.4 514.33 1
While the string is rendered glyph-by-glyph, Tm is updated as described in ISO 32000-1 section 9.4.4, and so is the Trm.
T*
As the text leading currently is 12, T* is equivalent to 0 -12 Td, so:
1 0 0 1 0 0 1 0 0
T = T = 0 1 0 * 0 1 0 = 0 1 0
m lm 0 -12 1 46.22 5.6 1 46.22 -6.4 1
I'm working on a roll-your-own undersampling function, since imblearn does not work neatly with multi-label classification (e.g. it only accepts one dimensional y).
I want to iterate through X and y, removing a row every 2 or 3 rows that are part of the majority class. The goal is a quick and dirty way to reduce the number of rows in the majority class.
def undersample(X, y):
counter = 0
for index, row in y.itertuples():
if row['rectangle_here'] == 0:
counter += 1
if counter > 3:
counter = 0
X.drop(index, inplace=True)
y.drop(index, inplace=True)
return X, y
But it crashes my kernel on even a small amount of rows (~30,000).
y is something like this, where anytime f2 or f3 is present, f1 is present
So, let's count the number of times 0 happens in f1 and then delete a 0 row every 3rd time:
f1 f2 f3
0 0 0 0
1 0 0 0
2 0 0 0
3 1 0 1
4 0 0 0
5 0 0 0
6 0 0 0
7 0 0 0
8 0 0 0
9 0 0 0
it goes like this
q
0.1199951 0 0 0.1199951 0 0 cm
1 g
824 4101 267 389 re
f
Q
q
0.1199951 0 0 0.1199951 0 0 cm
1 g
824 4853 267 25 re
f
Q
q
0.1199951 0 0 0.1199951 0 0 cm
1 g
824 5241 267 25 re
f
Q
q
0.1199951 0 0 0.1199951 0 0 cm
1 g
1090 578 3081 1988 re
f
Q
q
0.1199951 0 0 0.1199951 0 0 cm
603 586 m
603 1800 l
649 1800 l
649 586 l
h
W n
8.3336724 0 0 8.3336724 0 0 cm
BT
/T1_0 5.04 Tf
0 1.0002 -1 0 76.8 70.32 Tm
(J)Tj
I want to ask what should be coordinate for J ?
My cropbox is 0 0 612 792 , Rotate value is 90.
So according to me
Th=1 default,
Tfs=5.04, from {/T1_0 5.04 Tf}
Trise=0 default,
teststatematrix
5.04 1 0
0 5.04 0
0 0 1
Tm
0 1.0002 0
-1 0 0
76.8 70.32 1
TRM = textstatematrix X Tm
-1 5.041 0
-5.040 0 0
76.800 70.320 1
So
[x,y,1] = [76.8, 70.32, 1] X TRM = [-354.413 457.469 1]
So x coordinate in user space is coming to be a negative number. Can you please Explain What mistake i am doing?
The matrix Trm calculated by the OP as
-1 5.041 0
-5.040 0 0
76.800 70.320 1
is the text rendering matrix described as follows in the PDF specification:
Conceptually, the entire transformation from text space to device space may be represented by a text rendering matrix, Trm:
(section 9.4.2, ISO 32000-1:2008)
The OP's mistake is not in calculating this matrix but in using it: This matrix contains the entire transformation from text space to device space,
Tj and other text-showing operators shall position the origin of the first glyph to be painted at the origin of text space.
(section 9.2.4 ISO 32000-1:2008)
and
The glyph origin is the point (0, 0) in the glyph coordinate system
(ibidem)
To determine, therefore, where the OP's
(J)Tj
puts the origin of the glyph J, one has to apply that matrix to the origin (0, 0), not to (76.8, 70.32) as the OP did.
Thus,
[x,y,1] = [0, 0, 1] X Trm = [76.8, 70.32, 1]
i.e. the coordinates of J are (76.8, 70.32) in device space. As the OP assumed the initial transformation matrix to have been the identity matrix, this device space essentially is the default user space.
Unfortunately the OP did not explain the coordinates in which coordinate system he is looking for. Thus, these coordinates probably are not the coordinates he was looking for.
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
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)