Alter PDF - Text repositioning
Asked Answered
T

3

5

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?

Tern answered 4/2, 2012 at 21:19 Comment(2)
is this for a single pdf, or do you need something that will move text for arbitrary PDFs?Emblematize
User can select area (rectangle) of the text that he wants to shift. It's for single pdf and all it's pages.Tern
F
7

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

Finland answered 6/2, 2012 at 14:34 Comment(0)
S
1

You can remove an object using Page.Contents.Elements.RemoveAt(8) Validate the element count by checking Page.Contents.Elements.Count.

you can get the string value of each element (to do some string validation) you can fetch the data as below.

public static string GetElementStream(PdfPage page, int elementIndex)
    {
        string strStreamValue;
        byte[] streamValue;
        strStreamValue = "";

        if (page.Contents.Elements.Count >= elementIndex)
        {
            PdfDictionary.PdfStream stream = page.Contents.Elements.GetDictionary(elementIndex).Stream;
            streamValue = stream.Value;

            foreach (byte b in streamValue)
            {
                strStreamValue += (char)b;
            }
        }
        return strStreamValue;
    }
Shiau answered 25/4, 2013 at 5:37 Comment(0)
K
-1

Or you could draw over and create a read only text form at the new location

Kimball answered 5/2, 2012 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.