We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Setting TextFragment.Text changes Font

I have an existing PDF document that I am trying to use a TextFragmentAbsorber to do a Find and Replace. I am able to find the fragment and I can see that the font name is currently "Gotham HTF Book,Italic". However, as soon as I change .Text to a different string, the font name changes to "TimesNewRoman". I want it to retain the original font. This also seems to cause an error when opening the PDF, but really I just need the font to remain the same and I believe that will address the error as well.

Any ideas? The relevant code fragment is below. Thanks!

int pageNum = 0;

foreach (Aspose.Pdf.Page pdfPage in sectionPdf.Pages)
{
TextFragmentAbsorber tfAbsorber = new TextFragmentAbsorber("PAGENUM");
pdfPage.Accept(tfAbsorber);

foreach (TextFragment fragment in tfAbsorber.TextFragments)
{
fragment.Text = pageNum.ToString();
}


pageNum++;
}

Hi Seth,


Thanks for using our products and sorry for the delayed response.

In order to replace the text string with desired font, you may also specify the name of font using textFragment.TextState.Font = FontRepository.FindFont(“Gotham
HTF Book,Italic
”);
. In case the problem still persists, can you please share the source PDF file so that we can test the scenario at our end. We are really sorry for this inconvenience.

Thanks for the reply.

Unfortunately FontRepository.FindFont("Gotham HTF Book,Italic") returns an ApplicationException "Font Gotham HTF Book,Italic was not found"

Do I need to add it to the FontRepository? How would I do that? The font is already in use in many other parts of the PDF (including the text that is being replaced). Is there a way to pull it from the PDF?

Hi Seth,


Before callling FontRepository.FindFont(…) method, please ensure that Gotham HTF Book,Italic font is installed over system. I have tested the scenario using following code snippet over Windows 7 X64 while using VisualStudio 2010 project and as per my observations, the text is properly appearing in Gotham Book font (I have installed Gotham Book over my system). In case you still face any issue, can you please share the source PDF and the respective font file so that we can test the scenario at our end. We are sorry for this inconvenience.

[C#]

//open document<o:p></o:p>

Document pdfDocument = new Document("c:/pdftest/output.pdf");

//create TextAbsorber object to find all instances of the input search phrase

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Contract:");

//accept the absorber for all the pages

pdfDocument.Pages.Accept(textFragmentAbsorber);

//get the extracted text fragments

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

//loop through the fragments

foreach (TextFragment textFragment in textFragmentCollection)

{

//update text and other properties

textFragment.Text = "New Phrase";

textFragment.TextState.Font = FontRepository.FindFont("Gotham Book");

}

pdfDocument.Save("c:/pdftest/Font_Replaced_output.pdf");

The issue was partially resolved. I was running it locally on my development machine and did not have Gotham HTF Book installed. Once I installed it, the FontRepository no longer threw an exception and the PDF saves fine.

However, when I open the PDF I get an error message from Adobe Reader X. I've attached a sample PDF, but this time it is using Lucida Sans instead of Gotham HTF Book, because I believe Lucida Sans is a standard font. I cannot post the Gotham HTF Book file that I have because it is a licensed font that we purchased.

Page 3 is the only page in the attached PDF where I am trying to replace some text. The text is at the bottom of the page in a gold/orange color. When I get to page 3 in Acrobat Reader I get the error message "Cannot find or create the font 'QVHIRS+LuciaSansItalic'. Some characters may not display or print correctly." The "QVHIRS" string will change every time I generate the pdf and replace the text.

I also tried this with Arial and that worked fine. I tried it with Garamond and that did not. I have not been able to determine the pattern in what works and what does not, but I believe all these fonts (other than the original Gotham HTF Book) are standard Windows fonts. My application is built and running under Visual Studio 2010 (.NET 3.5) and I am running Windows 7 SP 1.

Thanks for the help.

Hi Seth,


Thanks for sharing the details.

Can you please share the original/input PDF file so that we can test the scenario at our end. We are really sorry for this inconvenience.

I don't have an input pdf for you but I may have the solution. Without getting into too many complicated details, this is just one step of a process. My inputs are not physical PDFs but several byte arrays that each represents a PDF. I loop through the list of byte arrays, convert each to a PDF object and have an inner loop within that loop that goes through each page replacing the text. After these loops finish, I concatenate the PDF objects together, then stream it back to the user (this is in an ASPX page).

I pared it down to a single page and the did a PDF.Save("before.pdf") right before the text replace and a PDF.Save("after.pdf") right after the text replace, and all of a sudden it started working. I experimented some more, and if I do a PDF.Save() (no parameters) after I am done replacing the text in each PDF but before I move on to the next PDF or do any concatenation, everything works fine. There must be something that the Save does before the Concatentation that fixes the issue.

Thanks for your help! Here is a quick summary of what did not work and how it was fixed:

Before (causes error in Adobe Reader):
For each PDF
For each Page
Replace Text
End For
Add PDF to InputPDFArray
End For
PdfFileEditor.Concatente(InputPDFArray, OutputPDF)
OutputPDF.Save(memorystream)
Send memorystream to user using HTTPResponse.

After (works successfully):
For each PDF
For each Page
Replace Text
End For
PDF.Save()
Add PDF to InputPDFArray
End For
PdfFileEditor.Concatente(InputPDFArray, OutputPDF)
OutputPDF.Save(memorystream)
Send memorystream to user using HTTPResponse.

Hi Seth,


Thanks for sharing the details and sorry for replying you so late.

I have tested the scenario using following code snippet where I have used VisualStudio 2010 application with target .NET framework set to .NET 3.5 and I am unable to notice any issue. After text replace, the new string/word is appearing in previously used font. Please take a look over the attached PDF file.

I have used Aspose.Pdf for .NET 7.6.0.

[C#]

//open document<o:p></o:p>

Document pdfDocument = new Document("c:/pdftest/Hello+World (1).pdf");

//create TextAbsorber object to find all instances of the input search phrase

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Hello");

//accept the absorber for all the pages

pdfDocument.Pages.Accept(textFragmentAbsorber);

//get the extracted text fragments

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

//loop through the fragments

foreach (TextFragment textFragment in textFragmentCollection)

{

//update text and other properties

textFragment.Text = "Ola";

// textFragment.TextState.Font = FontRepository.FindFont("Gotham Book");

}

MemoryStream ms = new MemoryStream();

pdfDocument.Save(ms);

// pdfDocument.Save("c:/pdftest/Font_Replaced_output.pdf");

//open document

Document pdfDocument2 = new Document("c:/pdftest/Hello+World (1) - Copy.pdf");

//create TextAbsorber object to find all instances of the input search phrase

TextFragmentAbsorber textFragmentAbsorber2 = new TextFragmentAbsorber("World");

//accept the absorber for all the pages

pdfDocument2.Pages.Accept(textFragmentAbsorber2);

//get the extracted text fragments

TextFragmentCollection textFragmentCollection2 = textFragmentAbsorber2.TextFragments;

//loop through the fragments

foreach (TextFragment textFragment2 in textFragmentCollection2)

{

//update text and other properties

textFragment2.Text = "Planet";

// textFragment2.TextState.Font = FontRepository.FindFont("Gotham Book");

}

MemoryStream ms2 = new MemoryStream();

pdfDocument2.Save(ms2);

//create PdfFileEditor object

PdfFileEditor pdfEditor = new PdfFileEditor();

//concatenate files

pdfEditor.Concatenate(ms,ms2,new FileStream("c:/pdftest/Concatenated_Text_Repalced.pdf", FileMode.Create));

ms.Close();

ms2.Close();