iTextSharp的最新版本是5.5.11。
当我对5.5.0运行完全相同的代码时,它可以工作。
这是一个非常简单的请求 - 使用iTextSharp在内存中的现有PDF上设置水印。
以下是有问题的代码:
private static Stream Watermark(Stream inStream, WatermarkRequest request)
{
using (var outStream = new MemoryStream((int)(inStream.Length + 4096)))
{
using (var reader = new PdfReader(inStream))
using (var stamper = new PdfStamper(reader, outStream))
{
var opacity = request.Opacity.HasValue ? request.Opacity.Value : 0.75f;
var angle = request.Angle.HasValue ? request.Angle.Value : 45;
var size = request.Size.HasValue ? request.Size.Value : 72f;
var color = new BaseColor(1f, 0f, 0f, 0f);
if (!String.IsNullOrEmpty(request.Color))
{
var rgba = PDF.Lib.RGBA.convert(request.Color);
color = new BaseColor((float)rgba.R, (float)rgba.G, (float)rgba.B, 0f);
}
for (var n = 1; n <= reader.NumberOfPages; n++)
{
var pcb = stamper.GetOverContent(n);
var gstate = new PdfGState() { FillOpacity = opacity, StrokeOpacity = opacity };
pcb.SaveState();
pcb.SetGState(gstate);
pcb.SetColorFill(color);
pcb.BeginText();
pcb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, Encoding.Default.WebName, true), size);
var ps = reader.GetPageSize(n);
var x = (ps.Right + ps.Left) / 2;
var y = (ps.Bottom + ps.Top) / 2;
pcb.ShowTextAligned(Element.ALIGN_CENTER, request.Text, x, y, angle);
pcb.EndText();
pcb.RestoreState();
}
}
return new MemoryStream(outStream.ToArray());
}
}