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

Free Support Forum - aspose.com

Some chinese charactors become messy code when I add a pdfpagestamp to a pdf file

I have a pdf file contains some chinese charactors, when I add pdfpagestamp to this file, the chinese charactor in the file become messy code.



watermarkSetting=new WatermarkSetting
{
Watermark=“hello from louis”,
WatermarkFontSize=18,
WatermarkFont=“Calibri”
}

Code:

public static Byte[] AddContentWatermarkForPdf(Byte[] pdfBytes, WatermarkSetting watermarkSetting)
{
using (var pdfStream = new MemoryStream(pdfBytes))
using (var pdfDocument = new Aspose.Pdf.Document(pdfStream))
using (var singlePdfStream = new MemoryStream(GetSinglePdf(pdfBytes, watermarkSetting)))
using (var resultStream = new MemoryStream())
{
var pageCount = pdfDocument.Pages.Count;
var pdfStamp = new PdfPageStamp(singlePdfStream, 1);
pdfStamp.XIndent = 0;
pdfStamp.YIndent = 0;
pdfStamp.Opacity = watermarkSetting.Opacity;
for (int index = 1; index <= pageCount; index++)
{
pdfDocument.Pages[index].AddStamp(pdfStamp);
}
pdfDocument.Save(resultStream, SaveFormat.Pdf);
return resultStream.ToBytes();
}
}




private static Byte[] GetSinglePdf(Byte[] pdfBytes, WatermarkSetting watermarkSetting)
{
using (var stream = new MemoryStream(pdfBytes))
using (var pdfDocument = new Aspose.Pdf.Document(stream))
{
return GetSinglePdf(pdfDocument.Pages[1].MediaBox.Height, pdfDocument.Pages[1].MediaBox.Width, watermarkSetting);
}
}

private static Byte[] GetSinglePdf(Double height, Double width, WatermarkSetting watermarkSetting)
{
try
{
Aspose.Pdf.Text.FontRepository.FindFont(watermarkSetting.WatermarkFont);
}
catch
{
watermarkSetting.WatermarkFont = “Calibri”;
}
watermarkSetting.WatermarkFontSize = watermarkSetting.WatermarkFontSize.Replace(“pt”, “”).Replace(“Pt”, “”);
var fontSize = 48;
Int32.TryParse(watermarkSetting.WatermarkFontSize, out fontSize);
watermarkSetting.WatermarkFontSize = fontSize + “”;

using (var g = Graphics.FromImage(new Bitmap(800, 800)))
using (var ms = new MemoryStream())
{
g.PageUnit = GraphicsUnit.Pixel;

using (var transparentImg = new Bitmap((Int32)width, (Int32)height))
using (var transparentG = Graphics.FromImage(transparentImg))
using (var transparentPNGMS = new MemoryStream())
{
transparentG.Clear(System.Drawing.Color.Transparent);
transparentImg.Save(transparentPNGMS, ImageFormat.Png);
using (var middleStream = new MemoryStream(transparentPNGMS.ToArray()))
{
var PdfConvertorFactory = new PdfConvertorFactory();
using (var transparentPdfStream = PdfConvertorFactory.GetConvertor(“png”).Convert(middleStream))
using (var mmStream = new MemoryStream(transparentPdfStream.ToBytes()))
using (var singlePdf = new Aspose.Pdf.Document(mmStream))
using (var resultStream = new MemoryStream())
{
singlePdf.Background = Aspose.Pdf.Color.Transparent;
var formatedSingleText = formateSingleText(watermarkSetting);
var textStamp = new TextStamp(formatedSingleText);
SetSingleStamp(textStamp, watermarkSetting, width, height);
System.Drawing.Font _Font = new System.Drawing.Font(watermarkSetting.WatermarkFont, (float)fontSize, System.Drawing.FontStyle.Regular);
SizeF _SizeF = g.MeasureString(watermarkSetting.Watermark, _Font);
var textHeight = _SizeF.Height;
var textWidth = _SizeF.Width;
//add stamp to particular page
if (watermarkSetting.IsRepeated)
{
if (watermarkSetting.Direction == -90)
{
for (int i = 0; i < width / (textWidth - 25); i++)
{
for (int j = 0; j < height / (textHeight + 25); j++)
{

//setStamp(ts, entity);
textStamp.XIndent = j * (textHeight - 25) + 10;
textStamp.YIndent = i * (textWidth + 25);
singlePdf.Pages[1].AddStamp(textStamp);
}
}
}
else
{
for (int i = 0; i < height / (textHeight + 25); i++)
{
for (int j = 0; j < width / (textWidth - 25); j++)
{
//setStamp(ts, entity);
textStamp.XIndent = j * (textWidth - 25) + 10;
textStamp.YIndent = i * (textHeight + 25);
singlePdf.Pages[1].AddStamp(textStamp);
}
}
}
}
else
{
singlePdf.Pages[1].AddStamp(textStamp);
}
singlePdf.Save(resultStream, SaveFormat.Pdf);
return resultStream.ToArray();
}
}
}
}
}









public class WatermarkSetting
{

public String Watermark { get; set; }

public String WatermarkFont { get; set; }

public String WatermarkFontSize { get; set; }
}

Hi LouisGuo,

Thanks for your inquiry. I have tested your scenario with shared document using Aspose.Pdf for .NET 11.5.0 and managed to observe the reported issue. For further investigation, I have logged an issue in our issue tracking system as PDFNEWNET-40706 and also linked your request to it. We will keep you updated via this thread regarding the issue status.

We are sorry for the inconvenience caused.

<span style=“font-size:10.0pt;font-family:“Arial”,“sans-serif”;mso-fareast-font-family:
Calibri;color:#333333;mso-ansi-language:EN-US;mso-fareast-language:EN-US;
mso-bidi-language:AR-SA”>Best Regards,