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.

Mon Jun 05, 2023 5:29 am

Hi

I am using freespire.pdf(8.6.0) Version in .Net Core Project (.Net - 6 )

As I am try to replacing texts in pdf with new words but it replace with only white space,no new word is replaced there

I also tried with different way and example provied on e-iceblue official site but not works

Please see below my code

var templatePath = Path.Combine(GoatSignConsts.TemplatePath, template.TenantId.ToString(), template.FileName);

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(templatePath);
PdfUsedFont pdfFont = doc.UsedFonts[0];

PdfTextFindCollection collection = page.FindText("{{Name}}", TextFindParameter.WholeWord);

RectangleF rec;

//Creates a brush
PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);

foreach (PdfTextFind find in collection.Finds)
{
float.TryParse((pdfFont.Size + 2).ToString(), out float fontSize);
PdfTrueTypeFont font;

try
{
font = new PdfTrueTypeFont(new Font(new FontFamily(pdfFont.Name), 12, FontStyle.Regular), fontSize, true);
}
catch (Exception)
{
font = new PdfTrueTypeFont(new Font(new FontFamily("Arial"), 12, FontStyle.Regular), fontSize, true);
}

// Gets the bound of the found text in page
rec = find.TextBounds.FirstOrDefault();
//rec.Y = rec.Y + 1;
page.Canvas.DrawRectangle(PdfBrushes.White, rec);
page.Canvas.DrawString("Replaced One", font, brush, rec);
}


We have also applied other methods but we are facing lots of issue with layout and words position

Also Tried this

foreach (PdfTextFind find in collection.Finds)
{
//replace word in pdf
find.ApplyRecoverString(dVariable.Value, Color.White, true);
}

Please take a look and because we tried with different applications

jenishItact
 
Posts: 18
Joined: Tue Mar 21, 2023 5:34 am

Mon Jun 05, 2023 9:29 am

Hi,

Thanks for your inquiry.
I did a test with the document you provided, using the free Spire.PDF_8.6.0, but I did not reproduce your issue, the newly added words are displayed in the result document, I have attached it for your reference. Partial text display is incomplete due to insufficient rectangular width.

If you have any further questions or concerns, just feel free to contact us.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Jun 05, 2023 11:15 am

Hi Thanks For your reply

Please try with document file i have attached names Template2.pdf

Here i am replacing {{Name}} {{Profession}} {{template}} {{Answer}} with new words

Also i have attached result of above sample 638215628561506445_Template2.pdf but result show white space replaced not text replaced
Note : its with trial licence version but you can see result is same for licence and free version

In ImportantWplaceholders.zip you can try to replace {{goal}} with new word give same result with blank space


here

page.Canvas.DrawRectangle(PdfBrushes.White, rec); //This line is replacing white object
page.Canvas.DrawString(newText, font, brush, rec); // but this line is not working

jenishItact
 
Posts: 18
Joined: Tue Mar 21, 2023 5:34 am

Tue Jun 06, 2023 4:51 am

Hi,

Thanks for your feedback.
After debugging the issue, I found that the font size you used to draw the new text was larger than the original font size (11.04), the height of the newly added text box is lower than the height of the newly added text, which caused the new text not to be displayed. To resolve this issue, you can set the font size to be smaller than or equal to the font size of the text being replaced, or use the following code to force the text to be displayed.
Code: Select all
PdfStringFormat format = new PdfStringFormat();
format.LineLimit = true;
page.Canvas.DrawRectangle(PdfBrushes.White, rec);
page.Canvas.DrawString("hello", font, brush, rec,format);

Regarding your another question, our latest version of the PDF product has changed its licensing method. Please refer to the following code:
Code: Select all
Spire.Pdf.License.LicenseProvider.SetLicenseKey("");

If you have any further questions or concerns, please don't hesitate to contact us.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Tue Jun 06, 2023 1:52 pm

Thank you for your response

It is working now and have to set font size here

Once again Thanks

jenishItact
 
Posts: 18
Joined: Tue Mar 21, 2023 5:34 am

Wed Jun 07, 2023 2:20 am

Hi,

Thanks for your feedback.
Glad to hear that you have solved this issue. If you have any further questions or concerns, just feel free to contact us.

Have a nice day! :D

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.PDF

cron