在PDF c#上从左下角到右上角拉伸水印#
Stretch watermark from bottom left to top right on PDF c#
这听起来像是一个非常简单的问题,但我似乎无法弄明白。
我想在我的 PDF 顶部添加水印,并且需要它从左下角开始到右上角结束。我的开始问题是我找不到左下角。我有一种感觉,根据我打开的 PDF 文档,0,0 坐标会发生变化。
这是我目前得到的:
static string fontname = "Calibri";
public void WaterMarkPDF3(string sourceFileName)
{
PdfDocument doc = PdfReader.Open(sourceFileName, PdfDocumentOpenMode.Modify);
foreach (PdfPage page in doc.Pages)
{
//First calculate the cross angle from bottom left to top right
double Angle= -Math.Atan(page.Height / page.Width) * 180 / Math.PI;
//here I calculate how long the diagonal is so that my string can have the same length
double watermarkWidth = page.Height / Math.Sin(Angle);
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
double emSize = 300;
XFont font = new XFont(fontname, emSize, XFontStyle.Bold);
XSize size = gfx.MeasureString("Teststring", font);
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(Angle);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
XGraphicsPath path = new XGraphicsPath();
path.AddString("Teststring", font.FontFamily, XFontStyle.Bold, emSize,
new XPoint(0, page.Height), XStringFormats.Default);
// add to color to the outline and filling
XPen pen = new XPen(XColor.FromArgb(75, 0, 152, 163), 2);
XBrush brush = new XSolidBrush(XColor.FromArgb(50, 0, 152, 163));
gfx.DrawPath(pen, brush, path);
}
doc.Save(sourceFileName);
}
据我了解,0,0 是左上角。通常,如果我在 new XPoint(0, page.Height)
处开始我的水印,我希望它位于左下角。正如您在所附图片中看到的那样,它位于“随机”查找位置。
可在此处找到水印示例:
https://github.com/empira/PDFsharp-samples/blob/master/samples/core/Watermark/Program.cs
示例和您的代码之间的一个区别是 XStringFormat
。这定义了字符串如何相对于给定点对齐。该示例只是将字符串的中心与页面的中心对齐。
您的 "question" 来自评论:emSize
是以磅为单位的字体大小 - 72 磅是 1 英寸。您的 300 点是 105.833 毫米或 4.167 英寸。
您的代码段围绕页面中心旋转。我认为这也会改变页面角的坐标。如果您想从页面的角开始字符串,那么围绕页面的角旋转可能更简单。
该示例在页面中央绘制水印,这是大多数人会要求的。
谢谢大家的帮助。
我将文字更改为图片(公司徽标)。该代码现在适用于所有类型的页面(A4、A3、....)
代码如下:
public void WaterMarkPDF4(string sourceFileName)
{
PdfDocument doc = PdfReader.Open(sourceFileName, PdfDocumentOpenMode.Modify);
foreach (PdfPage page in doc.Pages)
{
double hoek = Math.Atan(page.Height / page.Width);
double watermarkWidth = page.Height / Math.Sin(hoek);
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
XImage image = XImage.FromFile(TransparentImage.png");
double iWidth = image.PixelWidth * 72 / image.HorizontalResolution;
double iHeight = image.PixelHeight * 72 / image.HorizontalResolution;
double factor = watermarkWidth * 0.8 / iWidth;
XPoint punt = new XPoint();
punt.X = iWidth / 2;
punt.Y = iHeight / 2;
gfx.TranslateTransform(-iWidth / 2, -iHeight / 2);
gfx.RotateAtTransform(-hoek * 180 / Math.PI, punt);
gfx.ScaleAtTransform(factor,factor, iWidth / 2, iHeight / 2);
double pwidth = page.Width;
double pheight = page.Height;
double dx = Math.Cos(hoek) * pwidth / 2 - Math.Sin(hoek) * pheight / 2;
double dy = Math.Sin(hoek) * pwidth / 2 + Math.Cos(hoek) * pheight / 2;
double newHeight = (image.PixelHeight / page.Width) * watermarkWidth;
double y = page.Height/2 - newHeight / 2;
gfx.DrawImage(image, dx/factor, dy/factor);
gfx.Dispose();
}
doc.Save(sourceFileName);
}
这听起来像是一个非常简单的问题,但我似乎无法弄明白。
我想在我的 PDF 顶部添加水印,并且需要它从左下角开始到右上角结束。我的开始问题是我找不到左下角。我有一种感觉,根据我打开的 PDF 文档,0,0 坐标会发生变化。
这是我目前得到的:
static string fontname = "Calibri";
public void WaterMarkPDF3(string sourceFileName)
{
PdfDocument doc = PdfReader.Open(sourceFileName, PdfDocumentOpenMode.Modify);
foreach (PdfPage page in doc.Pages)
{
//First calculate the cross angle from bottom left to top right
double Angle= -Math.Atan(page.Height / page.Width) * 180 / Math.PI;
//here I calculate how long the diagonal is so that my string can have the same length
double watermarkWidth = page.Height / Math.Sin(Angle);
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
double emSize = 300;
XFont font = new XFont(fontname, emSize, XFontStyle.Bold);
XSize size = gfx.MeasureString("Teststring", font);
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(Angle);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
XGraphicsPath path = new XGraphicsPath();
path.AddString("Teststring", font.FontFamily, XFontStyle.Bold, emSize,
new XPoint(0, page.Height), XStringFormats.Default);
// add to color to the outline and filling
XPen pen = new XPen(XColor.FromArgb(75, 0, 152, 163), 2);
XBrush brush = new XSolidBrush(XColor.FromArgb(50, 0, 152, 163));
gfx.DrawPath(pen, brush, path);
}
doc.Save(sourceFileName);
}
据我了解,0,0 是左上角。通常,如果我在 new XPoint(0, page.Height)
处开始我的水印,我希望它位于左下角。正如您在所附图片中看到的那样,它位于“随机”查找位置。
可在此处找到水印示例:
https://github.com/empira/PDFsharp-samples/blob/master/samples/core/Watermark/Program.cs
示例和您的代码之间的一个区别是 XStringFormat
。这定义了字符串如何相对于给定点对齐。该示例只是将字符串的中心与页面的中心对齐。
您的 "question" 来自评论:emSize
是以磅为单位的字体大小 - 72 磅是 1 英寸。您的 300 点是 105.833 毫米或 4.167 英寸。
您的代码段围绕页面中心旋转。我认为这也会改变页面角的坐标。如果您想从页面的角开始字符串,那么围绕页面的角旋转可能更简单。
该示例在页面中央绘制水印,这是大多数人会要求的。
谢谢大家的帮助。
我将文字更改为图片(公司徽标)。该代码现在适用于所有类型的页面(A4、A3、....)
代码如下:
public void WaterMarkPDF4(string sourceFileName)
{
PdfDocument doc = PdfReader.Open(sourceFileName, PdfDocumentOpenMode.Modify);
foreach (PdfPage page in doc.Pages)
{
double hoek = Math.Atan(page.Height / page.Width);
double watermarkWidth = page.Height / Math.Sin(hoek);
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
XImage image = XImage.FromFile(TransparentImage.png");
double iWidth = image.PixelWidth * 72 / image.HorizontalResolution;
double iHeight = image.PixelHeight * 72 / image.HorizontalResolution;
double factor = watermarkWidth * 0.8 / iWidth;
XPoint punt = new XPoint();
punt.X = iWidth / 2;
punt.Y = iHeight / 2;
gfx.TranslateTransform(-iWidth / 2, -iHeight / 2);
gfx.RotateAtTransform(-hoek * 180 / Math.PI, punt);
gfx.ScaleAtTransform(factor,factor, iWidth / 2, iHeight / 2);
double pwidth = page.Width;
double pheight = page.Height;
double dx = Math.Cos(hoek) * pwidth / 2 - Math.Sin(hoek) * pheight / 2;
double dy = Math.Sin(hoek) * pwidth / 2 + Math.Cos(hoek) * pheight / 2;
double newHeight = (image.PixelHeight / page.Width) * watermarkWidth;
double y = page.Height/2 - newHeight / 2;
gfx.DrawImage(image, dx/factor, dy/factor);
gfx.Dispose();
}
doc.Save(sourceFileName);
}