PDFsharp 水印文本从文档的中心开始
PDFsharp watermark text starts in center on document
我正在尝试在 PDFsharp 中使用水印。我正在按照他们网站上的示例进行操作,但是当我尝试绘制水印时,文本从屏幕中央开始,而不是左下角。
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
var font = new XFont("Helvetica", 30, XFontStyle.Regular);
var watermark = "DRAFT";
XSize size = gfx.MeasureString(watermark, font);
// Define a rotation transformation at the center of the page
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
// Create a graphical path
XGraphicsPath path = new XGraphicsPath();
// Add the text to the path
path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
new XPoint((page.Width - (size.Width * 2)) / 2, (page.Height - size.Height) / 2),
XStringFormats.Default);
// Create a dimmed red pen and brush
XPen pen = new XPen(XColor.FromArgb(50, 75, 0, 130), 3);
XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205));
// Stroke the outline of the path
gfx.DrawPath(pen, brush, path);
如果您使用的是 PDFsharp 1.50 beta,则在调用 path.AddString
时将 XStringFormats.Default
替换为 XStringFormats.TopLeft
。
更新:你调用MeasureString
的字体大小是30,但是绘制的文字是150的字体,所以文字的位置是根据不正确的文本宽度和高度值计算,水印绘制在错误的位置。
XStringFormats.TopLeft
是正确的(PDFsharp 1.50需要它,PDFsharp 1.32忽略它),但没有解决计算和绘图使用不同字体大小引起的问题。
我正在尝试在 PDFsharp 中使用水印。我正在按照他们网站上的示例进行操作,但是当我尝试绘制水印时,文本从屏幕中央开始,而不是左下角。
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
var font = new XFont("Helvetica", 30, XFontStyle.Regular);
var watermark = "DRAFT";
XSize size = gfx.MeasureString(watermark, font);
// Define a rotation transformation at the center of the page
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
// Create a graphical path
XGraphicsPath path = new XGraphicsPath();
// Add the text to the path
path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
new XPoint((page.Width - (size.Width * 2)) / 2, (page.Height - size.Height) / 2),
XStringFormats.Default);
// Create a dimmed red pen and brush
XPen pen = new XPen(XColor.FromArgb(50, 75, 0, 130), 3);
XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205));
// Stroke the outline of the path
gfx.DrawPath(pen, brush, path);
如果您使用的是 PDFsharp 1.50 beta,则在调用 path.AddString
时将 XStringFormats.Default
替换为 XStringFormats.TopLeft
。
更新:你调用MeasureString
的字体大小是30,但是绘制的文字是150的字体,所以文字的位置是根据不正确的文本宽度和高度值计算,水印绘制在错误的位置。
XStringFormats.TopLeft
是正确的(PDFsharp 1.50需要它,PDFsharp 1.32忽略它),但没有解决计算和绘图使用不同字体大小引起的问题。