Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter PDF - Text repositioning

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?

like image 303
HABJAN Avatar asked Feb 04 '12 21:02

HABJAN


People also ask

How do I move text in a PDF?

To move the text box, place the pointer over the line of the bounding box (avoid the selection handles). When the cursor changes to Move pointer , drag the box to the new location. To maintain alignment with the other list items, press Shift as you drag.

How do I crop and move text in a PDF?

Keyboard Commands: Select your text while in the editor. Hold down the CTRL key and press X to cut. Hold down the CTRL key and press C to copy.

Why does PDF change alignment?

Many times you will encounter PDF alignment problems when you are editing PDF files. This is most common when working with files that have been converted from other formats. Most of the time, online conversion tools have this issue – a Word file may be uploaded for conversion when all text is properly aligned.


1 Answers

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 ...

like image 114
I liked the old Stack Overflow Avatar answered Sep 20 '22 00:09

I liked the old Stack Overflow