{var param=new PdfAddWaterMarkParam{
WaterMark=“我是水印文字”,
Typeface=“KaiTi”
};
var pdfDoc = new Aspose.Pdf.Document(stream);
var fonts = pdfDoc.FontUtilities.GetAllFonts();
var fontStr = new StringBuilder();
foreach (var f in fonts)
{
fontStr.Append(f.FontName + "\n");
}
Logger.Error($"获取水印字体:{fontStr}");
var font = Aspose.Pdf.Text.FontRepository.FindFont(param.Typeface);
var textState = new TextState
{
FontSize = param.FontSize,
ForegroundColor = param.AsposeFontColor,
Font = font
};
Logger.Error($"水印字体类型:{font.FontName}||水印文字:{param.WaterMark}");
for (int i = 1; i <= pdfDoc.Pages.Count; i++)
{
WatermarkArtifact artifact = new();
artifact.SetTextAndState(param.WaterMark, textState);
artifact.Rotation = param.Rotation;
artifact.Opacity = param.FillOpacity;
artifact.IsBackground = false;
artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Left;
artifact.ArtifactVerticalAlignment = VerticalAlignment.Top;
}
// 保存文件
var ms = new MemoryStream();
pdfDoc.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
public class PdfAddWaterMarkParam
{
public string Typeface { get; set; }
public string WaterMark { get; }
}
}
异常如下:
image.png (9.1 KB)
看日志是执行到: artifact.SetTextAndState(param.WaterMark, textState);