Alter PDF - Text repositioning

20,480

Solution 1

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

Solution 2

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;
    }
Share:
20,480
HABJAN
Author by

HABJAN

Blog: http://habjan.blogspot.com Ghostscript.NET: https://github.com/jhabjan/Ghostscript.NET

Updated on May 27, 2020

Comments

  • HABJAN
    HABJAN about 4 years

    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?