Zelda Zelda - 25 days ago 9
C# Question

iTextSharp PdfStamper puts barcode into an existing PDF according to the latest matrix

I have an existing PDF, that has this as the last matrix transformation:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm
(which is 45 deg rotation and some transition)

I use PdfStamper to insert an image into OverContent (using method GetOverContent). The problem is, that the last transformation sets 45 degrees rotation, so the barcode is rotated as well. And I cannot reset this matrix. I cannot even get the last transformation matrix.

I have already tried PdfReaderContentParser to get all the matrices and multiply them, so that I could get inverted matrix to use ConcatCTM with this inverted matrix to produce Identity matrix again, but the parser does not return all the matrices. It only gets matrices for images, but not matrices for texts.

I have already spend hours trying different solutions (creating templates, layers, ...), none working, everything is rotated according to the last matrix.

How to reset the matrix back to identity in existing PDF? So that the inserted barcode by PdfStamper is not rotated by 45 degrees.

Pleas don't propose setting -45 degrees rotation for the barcode image, it does not resolve anything, because the PDF can have or does not have to have the rotation matrix at the end.

I really need to reset the transformation matrix to identity matrix for OverContent.

Thank you very much, guys...

Edit:
This is the full content:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm

q BT 141.73 274.96 Td 5 Tr (some text) Tj ET

Q

0.57 w

BT /F1 20.00 Tf ET

q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm

q BT 155.91 246.61 Td 5 Tr (some text) Tj ET

Q

Answer

Your PDF has a syntax problem. I see a "save state" operator q, but I don't see a "restore state" operator Q.

Instead of:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm

You should have:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm Q

Well, actually, if you don't have any other content following the PDF syntax you share, that full snippet should be removed, because it doesn't make sense to introduce coordinate system transformations, and then do nothing with it.

If you want to fix this for this specific PDF, you could do this:

overContent.SetLiteral(" Q ");

This will restore the graphics state (and "reset the matrix" as you put it). Don't do this for all your PDFs, only for this faulty one.

Update:

You say that the syntax of your code looks like this:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm
q BT 141.73 274.96 Td 5 Tr (some text) Tj ET
Q
0.57 w
BT /F1 20.00 Tf ET
q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm
q BT 155.91 246.61 Td 5 Tr (some text) Tj ET
Q

This code is wrong. The person who created this PDF either used a crappy tool, or he used a good tool in a very bad way. The correct syntax would be:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm
BT 141.73 274.96 Td 5 Tr (some text) Tj ET
Q
q 0.57 w
BT /F1 20.00 Tf ET
q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm
BT 155.91 246.61 Td 5 Tr (some text) Tj ET
Q
Q

You can fix the problem like this:

q 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm
q BT 141.73 274.96 Td 5 Tr (some text) Tj ET
Q
0.57 w
BT /F1 20.00 Tf ET
q 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm
q BT 155.91 246.61 Td 5 Tr (some text) Tj ET
Q
Q
Q

Now the q/Q sequences are balanced. I'm adding numbers to show you which q corresponds with which Q:

q[1] 0.70711 0.70711 -0.70711 0.70711 141.73 274.96 cm 1 0 0 1 -141.73 -274.96 cm
q[2] BT 141.73 274.96 Td 5 Tr (some text) Tj ET
Q[2]
0.57 w
BT /F1 20.00 Tf ET
q[3] 1.00000 0.00000 0.00000 1.00000 155.91 246.61 cm 1 0 0 1 -155.91 -246.61 cm
q[4] BT 155.91 246.61 Td 5 Tr (some text) Tj ET
Q[4]
Q[3]
Q[1]

This syntax is wrong (obviously), but the numbers show you why there are two Q statements missing.