AnSharp
AnSharp

Reputation: 21

Remove or Overwrite Text on PDF in a specific position

i'm searching a solution for my problem.

I'm trying to modify a text that appears in a specific position of a pdf document using itextsharp.

Could someone please help?

SOLUTION:

I've resolved writing this:

public bool StampOnPDF(string _PathPDF, string _text, string _Total)
    {
        string _fileName = Path.GetFileName(_PathPDF);

        string oldFile = _PathPDF;
        string BackupPDF = _PathPDF.Replace(".pdf", "_old.pdf");

        File.Copy(oldFile, BackupPDF);

        iTextSharp.text.Rectangle Zone1 = new iTextSharp.text.Rectangle(495, 157, 540, 148);
        iTextSharp.text.Rectangle Zone2 = new iTextSharp.text.Rectangle(495, 130, 540, 105);

        using (PdfReader reader = new PdfReader(BackupPDF))
        using (PdfStamper stamper = new PdfStamper(reader, new FileStream(oldFile, FileMode.Create)))
        {                
            PdfContentByte pbover = stamper.GetOverContent(1);
            Zone1.BackgroundColor = BaseColor.WHITE;
            pbover.Rectangle(Zone1);
            Zone2.BackgroundColor = BaseColor.WHITE;
            pbover.Rectangle(Zone2);               

            // select the font properties
            var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
            var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);

            normalFont.Size = 8;
            boldFont.Size = 8;

            string text = _testo;                
            ColumnText.ShowTextAligned(pbover, Element.ALIGN_CENTER, new Phrase(text, normalFont), 300, 180, 0);

            text = _Total;
            ColumnText.ShowTextAligned(pbover, Element.ALIGN_CENTER, new Phrase(text, boldFont), 523, 115, 0);
            ColumnText.ShowTextAligned(pbover, Element.ALIGN_CENTER, new Phrase(text, normalFont), 523, 150, 0);

        }    
        return true;                                  
    }               

Upvotes: 1

Views: 3394

Answers (1)

Joris Schellekens
Joris Schellekens

Reputation: 9057

This problem is not a trivial one. To understand why, let's look at a piece of a PDF document.

[a, -28.7356, p, 27.2652, p, 27.2652, e, -27.2652, a, -28.7356, r, 64.6889, a, -28.7356, n, 27.2652, c, -38.7594, e, 444] TJ
/R10 10.44 Tf
68.16 0.24 Td
[", 17.1965, P, -18.7118, i, -9.35592, l, -9.35592, o, -17.2414, t, -9.35636, ", 17.1965, , 250] TJ

This piece of code tells the viewer to render the word "appearance". What you see here is each individual letter being rendered. The syntax being <kerning information> <letter> TJ (=text render instruction).

This should give you an idea of how hard it would be to replace a piece of text by something else. If you make an existing word shorter, you would need to move all other letters again. This problem is known as "reflowing" text. Reflowing, is not something that can be trivially done with pdf documents. To achieve reflow, you need high level information (such as which words belong to which paragraphs). This level of information is generally not present in a pdf document.

As @mkl indicated, if you simply want to remove the text (perhaps covering it with a black box to indicate it was removed) iText can certainly help you.

If you want to overwrite the text, that's (generally) not possible. It can be done if the word you're replacing it with has the same letters and you don't care that much about layout. (Since a word like "iText" might not take up the same amount of space as "Jazzy").

Upvotes: 3

Related Questions