Alter PDF - Text repositioning

HABJAN picture HABJAN · Feb 4, 2012 · Viewed 19k times · Source

Is there any way to shift / move the text inside existing pdf page to some other position?

Like there is some text at area x=100, y=100, w=100, h=100 and i want to move it to x=50, y=200, w=100, h=100.

I did a lot of research and it seems iTextSharp cannot do that. PDFSharp claims that it can be done but i could not find any examples.

One way is to make a bitmap of specific area of the text i want to shift, draw white rectangle over that area and insert bitmap at new location. I don't want to use this solution as i work with large pdf files with more than 1K pages where each page has to be altered.

What i found out is that i need to find a way to change text-positioning operators (text matrix and the text state parameters) which is not that simple.

Anyone has any ideas?

Answer

I think it can be done if all the PDF files are simple (not complex) coming from the same application.
If you need this for e.g. a website where users can upload files, then better forget it: you'll never get a solution that will work perfectly with any PDF file.

PDFsharp can help - but AFAIK PDFsharp only does half of what you need. PDFsharp will give you the blocks that make up the PDF file. You have to parse the blocks to find the drawing instructions, check the positions, and relocate them.
Some applications don't even draw words, so a simple word such as "Hello" could be drawn in 3 chunks (maybe "He", "ll" and "o"). You may have to pay attention to this; maybe not if all files come from the same application.

I think the code shown here to extract text could be helpful:
http://forum.pdfsharp.net/viewtopic.php?p=4010#p4010
To relocate text you have to find it in the first place - a lot of additional work still needed ...