Tested with Aspose 20.5 with no improvement. The original text in PDF file is Playername. Aspose recognizes these fragments from it: Play and ame.
Trying to initialize the absorber as in your example, i.e.
auto absorber = MakeObject(L"Aspose.Pdf");
doesn’t compile.
I attach the whole code, including the function StringtoU16:
bool CAspose::ReplaceTextInDocument(const char* from, const char *to, bool &textChanged)
{
try
{
textChanged = false;
for (int i = 0; i < m_Document->get_Pages()->get_Count(); i++)
{
auto absorber = MakeObject(StringtoU16(from).c_str());
m_Document->get_Pages()->idx_get(i + 1)->Accept(absorber);
for (int j = 0; j < absorber->get_TextFragments()->get_Count(); j++)
{
auto text = absorber->get_TextFragments()->idx_get(j + 1);
System::SharedPtr<Aspose::Pdf::Text::Position> position = text->get_Position();
double pageWidth = text->get_Page()->get_Rect()->get_Width();
double pageHeight = text->get_Page()->get_Rect()->get_Height();
double oldTotalHorizontalMargin = pageWidth - text->get_Rectangle()->get_Width();
double oldTotalVerticalMargin = pageHeight - text->get_Rectangle()->get_Height();
double leftMarginProportion = position->get_XIndent() / oldTotalHorizontalMargin;
double topMarginProportion = position->get_YIndent() / oldTotalVerticalMargin;
text->set_Text(StringtoU16(to).c_str());
textChanged = true;
double newTotalHorizontalMargin = pageWidth - text->get_Rectangle()->get_Width();
double newTotalVerticalMargin = pageHeight - text->get_Rectangle()->get_Height();
position->set_XIndent(leftMarginProportion * newTotalHorizontalMargin);
position->set_YIndent(topMarginProportion * newTotalVerticalMargin);
text->set_Position(position);
}
}
return true;
}
catch (...)
{
}
return false;
}
//--------------------------------------------------------------------------------------------------
std::u16string CAspose::StringtoU16(const std::string &str) const
{
std::u16string wstr = u"";
char16_t c16str[3] = u"\0";
mbstate_t mbs;
for (const auto& it: str)
{
memset (&mbs, 0, sizeof (mbs));//set shift state to the initial state
memmove(c16str, u"\0\0\0", 3);
mbrtoc16 (c16str, &it, 3, &mbs);
wstr.append(std::u16string(c16str));
}
return wstr;
}