Hi Aspose team,
Recently we are using [AsposePdfReplaceTextPages] to replace text on particular pages (on-premise version). However, we have requirements to show replaced text font in bold.
For example, we are uploading one document where text are in bold which are to be replaced. Once replaced, the replaced text should also in bold but we are getting as normal text only.
Thank you for your attention to this topic.
@rakasaha7
To replace text in a PDF document using Aspose and ensure that the replaced text appears in bold, you can utilize the TextState
method. This method allows you to modify various text properties, including font weight.
Here’s a general approach to achieve this:
- Create an instance of the
PdfContentEditor
class.
- Bind your PDF document using the
BindPdf
method.
- Use the
ReplaceText
method to replace the desired text.
- Set the
TextState
for the replaced text to make it bold.
Here’s a sample code snippet demonstrating how to replace text and set it to bold:
#include <Aspose.Pdf.h>
#include <Aspose.Pdf.Text.h>
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;
void ReplaceTextWithBoldFont(const std::string& inputFilePath, const std::string& outputFilePath, const std::string& oldText, const std::string& newText) {
// Load the PDF document
auto document = System::MakeObject<Document>(inputFilePath);
// Create a TextFragment for the new text
auto textFragment = System::MakeObject<TextFragment>(newText);
// Set the TextState to bold
textFragment->get_TextState()->set_FontStyle(FontStyles::Bold);
// Replace the text in the document
for (auto page : document->get_Pages()) {
page->Replace(oldText, textFragment);
}
// Save the updated document
document->Save(outputFilePath);
}
In this example, replace inputFilePath
, outputFilePath
, oldText
, and newText
with your actual file paths and text values. This code will ensure that the replaced text appears in bold, matching the original formatting of the document.
Make sure you have the necessary Aspose libraries referenced in your project to use the above code effectively.
If you have any further questions or need additional assistance, feel free to ask!
We need the code in Javascript via C++
@rakasaha7
We are checking it and will get back to you shortly.
Thank you for reaching out.
To clarify, are you expecting the replaced text to always be bold, or only when the original text being replaced is bold? Based on our testing with version 25.2 (see the attached image), when replacing bold text, the new text should also retain the bold formatting.
Could you please provide the original document where this issue occurs? If sharing the full document is not possible, could you specify an example of the exact word being replaced and the new word being inserted? This would help us investigate the behavior more precisely.
Additionally, could you confirm whether you are using a valid license? Some features may behave differently in licensed and unlicensed modes.
Looking forward to your response.
result.png (2.9 KB)