When rendering the pdf document the watermark text of chinese text showing incorrect word in the textstamp.
sampledoc.pdf (222.5 KB)
sample.png (13.6 KB)
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(“C:\Users\Vignesh.Rajendran\Desktop\test100.pdf”);
string stampText = “Document Title 上海罗氏制药有限公司-200-0002.01 approved on 01Aug25 01:57:57 PM(IST)”;
TextStamp textStamp = new TextStamp(stampText);
textStamp.Background = false;
textStamp.TextState.Font = FontRepository.FindFont(“Arial”);
textStamp.TextState.FontSize = 10;
textStamp.TextState.ForegroundColor = Color.Black;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Top;
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(textStamp);
}
pdfDocument.Save(@“D:\All_Common_Docs\test101.pdf”, Aspose.Pdf.SaveFormat.Pdf);
@thiru1711
To resolve the issue of invalid characters appearing in Chinese text after rendering a PDF, it is essential to ensure that the font you are using supports the Chinese characters. Currently, you are using the “Arial” font, which may not include the necessary glyphs for Chinese characters.
Cause: The invalid characters are likely due to the use of a font that does not support Chinese characters.
Solution: Follow these steps to fix the issue:
-
Use a Font that Supports Chinese Characters: Replace “Arial” with a font that is known to support Chinese characters, such as “SimSun” or “Microsoft YaHei”.
-
Embed the Font: Ensure that the font is embedded in the PDF. This will help maintain the appearance of the text across different systems.
-
Check Font Accessibility: Verify that the font you choose is installed on the system where the PDF is being rendered. If it’s not available, the PDF viewer may substitute it with a different font that does not support the characters.
Here’s an updated code snippet that incorporates these suggestions:
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document("C:\\Users\\Vignesh.Rajendran\\Desktop\\test100.pdf");
string stampText = "Document Title 上海罗氏制药有限公司-200-0002.01 approved on 01Aug25 01:57:57 PM(IST)";
TextStamp textStamp = new TextStamp(stampText);
textStamp.Background = false;
textStamp.TextState.Font = FontRepository.FindFont("SimSun"); // Use a font that supports Chinese characters
textStamp.TextState.FontSize = 10;
textStamp.TextState.ForegroundColor = Color.Black;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Top;
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(textStamp);
}
pdfDocument.Save(@"D:\All_Common_Docs\test101.pdf", Aspose.Pdf.SaveFormat.Pdf);
Next Steps: Implement the changes in your code as outlined above. After making these adjustments, test the PDF output to confirm that the issue with invalid characters has been resolved.
By using a font that supports Chinese characters and ensuring it is embedded, you will effectively eliminate the issue of invalid characters in your PDF output. Please let me know if you need further assistance!
As per your suggestion, it is working fine. As per our requirement, we are using only Arial or Calibri font types. Please suggest how to create a text stamp using the mentioned font types. Also, is it possible to create a text stamp without specifying a font type?
@thiru1711
You can create TextStamp without mentioning what font to use
however in this case it will automatically decide what font to use - in your case I received automatic use of Arial Unicode MS.
In case of using just Arial or Calibri - you can, however the result will depend which languages are supported in these fonts. I investigated a bit and for example it seems Arial hasn’t direct support for Chinese as there’s no glyphs that could draw Chinese characters.
Last thing, it also may be caused by version. While I investigated your issue I tried to recreate your situation from scratch on version Aspose Pdf 25.6 and result seems fine
here’s source
sampledoc.pdf (231.6 KB)
here’s result
sampledoc_out.pdf (411.0 KB)
Code I used:
static void stamp_issue()
{
string input = InputFolder + "sampledoc.pdf";
string output = OutputFolder + "sampledoc_out.pdf";
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(input);
string stampText = "Document Title 上海罗氏制药有限公司 - 200 - 0002.01 approved on 01Aug25 01:57:57 PM(IST)";
TextStamp textStamp = new TextStamp(stampText);
textStamp.Background = false;
//textStamp.TextState.Font = FontRepository.FindFont("Arial");
textStamp.TextState.FontSize = 10;
textStamp.TextState.ForegroundColor = ap.Color.Black;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Top;
textStamp.YIndent = 300;
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(textStamp);
}
pdfDocument.Save(output, Aspose.Pdf.SaveFormat.Pdf);
}
Hi , I have used the same code You have suggested but additionally added Font Type. But still i’m getting issue when converting the chinese character. As per our requirement, we are using only Arial or Calibri font types. Please suggest how to create a text stamp using the mentioned font types.
sampledoc.pdf (222.5 KB)
Code I used:
static void stamp_issue()
{
string input = InputFolder + “sampledoc.pdf”;
string output = OutputFolder + “sampledoc_out.pdf”;
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(input);
string stampText = “Document Title 上海罗氏制药有限公司 - 200 - 0002.01 approved on 01Aug25 01:57:57 PM(IST)”;
TextStamp textStamp = new TextStamp(stampText);
textStamp.Background = false;
textStamp.TextState.Font = FontRepository.FindFont(“Arial”);
textStamp.TextState.FontSize = 10;
textStamp.TextState.ForegroundColor = ap.Color.Black;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Top;
textStamp.YIndent = 300;
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(textStamp);
}
pdfDocument.Save(output, Aspose.Pdf.SaveFormat.Pdf);
}
@thiru1711
Do you have an option to install Arial Unicode MS?
In case you not, you’ll run into issue that fonts you insist on using just doesn’t support properly language you are trying to add into document - it just doesn’t have glyph data to draw such characters
For example in case I’ll modify the code I provided in the following way I’ll get exception that SimSun - widely used Chinese characters supporting font - isn’t found
static void stamp_issue()
{
string input = InputFolder + "sampledoc.pdf";
string output = OutputFolder + "sampledoc_out.pdf";
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(input);
string stampText = "Document Title 上海罗氏制药有限公司 - 200 - 0002.01 approved on 01Aug25 01:57:57 PM(IST)";
TextStamp textStamp = new TextStamp(stampText);
textStamp.Background = false;
//disable access to system font folder
FontRepository.Sources.Clear();
//folder in my project that contains only Arial & Calibri fonts
FontRepository.Sources.Add(new FolderFontSource(InputFolder + "fonts"));
textStamp.TextState.Font = FontRepository.FindFont("Arial");
textStamp.TextState.FontSize = 10;
textStamp.TextState.ForegroundColor = ap.Color.Black;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Top;
textStamp.YIndent = 300;
foreach (Page page in pdfDocument.Pages)
{
//Aspose.Pdf.FontNotFoundException: 'Font SimSun was not found'
page.AddStamp(textStamp);
}
pdfDocument.Save(output, Aspose.Pdf.SaveFormat.Pdf);
}
This problem disappears when Unicode supporting version of Arial is added
So currently it seems as the most direct solution for your issue that takes into account fonts you want to use