Hi Praetorion,
Thanks for contacting support.
I have tested the scenario using [Aspose.Pdf for .NET 8.5.0] where I have used the following code snippet to replace English text with Thai text and I am unable to notice any issue. Can you please try using the latest release and in case the problem still persists, please share the source PDF file so that we can test the scenario at our end. We are sorry for your inconvenience.
[C#]
//open document
Document pdfDocument = new Document("c:/pdftest/TextReplaced.pdf");
//create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Replaced");
//accept the absorber for a particular page
pdfDocument.Pages[1].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 = "สวัสดี test สวัสดี";
textFragment.TextState.Font = FontRepository.FindFont("Arial Unicode MS");
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.Yellow;
}
pdfDocument.Save("c:/pdftest/Thai_Chars_output.pdf");
Below is my implementation code.
private byte[] Parse(Stream binaryFile, List<KeyValuePair<string, string>> keyValuePair)
Document doc = new Document(binaryFile);
List trList = new List();
//look for all tokens
foreach (KeyValuePair<string, string> item in keyValuePair)
{
Aspose.Pdf.Text.TextOptions.TextSearchOptions tsOpt = new Aspose.Pdf.Text.TextOptions.TextSearchOptions(true);
TextFragmentAbsorber txtFragAbsorber = new TextFragmentAbsorber(item.Key);
doc.Pages.Accept(txtFragAbsorber);
TextFragmentCollection txtFragColl = txtFragAbsorber.TextFragments;
foreach (TextFragment txtFrag in txtFragColl)
{
float fontSize = txtFrag.TextState.FontSize;
string fontName = txtFrag.TextState.Font.FontName;
Aspose.Pdf.Color fontColor = txtFrag.TextState.ForegroundColor;
Position position = txtFrag.Position;
Font newFont = newFont = FontRepository.FindFont(“Verdana”);
try
{
newFont = FontRepository.FindFont(fontName);
}
catch
{
newFont = FontRepository.FindFont(“Verdana”);
}
TokenReplaceList token = new TokenReplaceList();
token.FText = new FormattedText();
token.FXPosition = (float)position.XIndent;
token.FYPosition = (float)position.YIndent-2;
if (item.Value.Contains("
") || item.Value.Contains("
"))
{
txtFrag.Text = “”;
string[] lines = Regex.Split(item.Value, “
”);
foreach (string line in lines)
{
token.FText.AddNewLineText(line);
}
}
else if (item.Value.Contains("\r\n"))
{
txtFrag.Text = “”;
string[] blines = Regex.Split(item.Value, “\r\n”);
foreach (string line in blines)
{
token.FText.AddNewLineText(line);
}
}
else
{
token.FText = new FormattedText(item.Value);
txtFrag.Text = string.Empty;
txtFrag.TextState.FontSize = fontSize;
txtFrag.TextState.Font = newFont;
txtFrag.TextState.ForegroundColor = fontColor;
}
trList.Add(token);
}
}
MemoryStream ms = new MemoryStream();
PdfFileMend mender = new PdfFileMend(doc);
mender.IsWordWrap = true;
mender.WrapMode = WordWrapMode.Default;
foreach (TokenReplaceList tr in trList)
{
mender.AddText(tr.FText, 1, tr.FXPosition, tr.FYPosition, 550, 550);
}
mender.Save(ms);
//doc.Save(ms);
byte[] result = ms.ToArray();
ms.Dispose();
mender.Close();
return result;
}
internal class TokenReplaceList
{
public FormattedText FText { get; set; }
public float FXPosition { get; set; }
public float FYPosition { get; set; }
}
Hi there,
Console project attached.
Hi there,