Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Nov 05, 2020 4:52 pm

Hello I have problem with process for replace text into pdf file.
I use this code (from yours example):
Code: Select all
PdfTextFindCollection collection = page.FindText(source, TextFindParameter.IgnoreCase);
PdfBrush brush = new PdfSolidBrush(Color.Chartreuse);
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 24f, FontStyle.Bold));
rec = find.Bounds;
page.Canvas.DrawRectangle(PdfBrushes.White, rec);
page.Canvas.DrawString(newText, font, brush, rec);
find.ApplyRecoverString(newText, Color.Transparent);


1) not changhe color and not change size text also if i install commercial Spire.PDF
2) if I delete row code find.ApplyRecoverString the newText is not displayed

I attach Pdf generate from print at file Word origin

I Hope in your feddback.
Thanks

gianfranco.facchini
 
Posts: 6
Joined: Mon Nov 02, 2020 1:44 pm

Fri Nov 06, 2020 8:34 am

I solved it as I understand the real problem.
The text becomes a figure and therefore must be treated as such.
So we need to think in figure dimensions.
My code:
Code: Select all
                           //Creates a brush
                            PdfBrush brush = new PdfSolidBrush(Color.Black);
                           //Defines a font
                            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Regular));
                            RectangleF rect;
                            rect = find.Bounds;
                            float hRect = rect.Height;
                            float yRect = rect.Y;
                            rect.Height = (rect.Height - 0.5).ToFloat(); // per evitare che sovvrascriva delle righe bordi di tabella in cui è il testo
                            page.Canvas.DrawRectangle(PdfBrushes.White, rect); // pulisco il testo precedente
                            rect.Height = hRect; // memorizza altezza rettancolo testo originale
                            SizeF size = font.MeasureString(newText); // recupera la dimensione del nuovo testo in base al nuovo font da impostare
                            float widthstring = size.Width > rect.Width ? size.Width : rect.Width; // imposta nuova larghezza
                            float heightstring = size.Height> rect.Height ? (size.Height+1) : rect.Height; // imposta nuova larghezza
                            // se la nuova altezza del testo è maggiore della precedete allora reimposto posizione verticale
                            float diffHeigth = (size.Height - rect.Height);
                            if (diffHeigth > 0) rect.Y = (rect.Y - (diffHeigth/2));
                            RectangleF textRect = new RectangleF(rect.X, rect.Y, widthstring, heightstring);
                            page.Canvas.DrawString(newText, font, brush, textRect);


One problem remains. To the right of the text appears an underscore "_" I don't understand why.

gianfranco.facchini
 
Posts: 6
Joined: Mon Nov 02, 2020 1:44 pm

Fri Nov 06, 2020 10:46 am

Hello,

Thanks for your feedback.
Glad to hear that your previous issue has been solved. As for the underscore issue, I did an initial test with the latest Spire.PDF Pack (Hot Fix) Version:6.11.0 but did not reproduce it. To help us look into it, could you please provide us with your input file? You could send it to us ([email protected]) via email. Don’t worry, we promise to keep your document confidential and will not use it for any other purpose.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF