【问题标题】:PDFsharp - Watermark at bottom of landscape pagePDFsharp - 横向页面底部的水印
【发布时间】:2020-05-03 06:22:15
【问题描述】:

我正在使用 PDFsharp 尝试在横向 PDF 页面的底部添加一些文本,但它总是垂直偏移,我使文本越大,它越靠近页面底部,但我想使用大小 20字体。如果我使用循环来计算出最大可能的尺寸,水印将完美地位于页面底部。我尝试了许多不同的方法,但无法锻炼我做错了什么..

   For Each Page As PdfPage In PDF.Pages
            Dim Graphics As XGraphics = XGraphics.FromPdfPage(Page, XGraphicsPdfPageOptions.Append)
            Dim FontSize As Integer = 20
            Dim Font As New XFont("Arial", FontSize, XFontStyle.Bold)
            Dim WaterMarkSize As XSize = Graphics.MeasureString(Watermark, Font)
            Dim Brush As New XSolidBrush(XColor.FromArgb(255, 255, 0, 0))

            If Page.Orientation = PdfSharp.PageOrientation.Portrait Then
                Graphics.DrawString(Watermark, Font, Brush, New XRect(0, Page.Height, Page.Width, 0), XStringFormats.BottomCenter)
            Else
                Page.Orientation = PageOrientation.Portrait
                Graphics.TranslateTransform(Page.Width.Value / 2, Page.Height.Value / 2)
                Graphics.RotateTransform(90)
                Graphics.TranslateTransform(-Page.Width.Value / 2, -Page.Height.Value / 2)

                Dim Format As New XStringFormat()
                Format.Alignment = XStringAlignment.Near
                Format.LineAlignment = XLineAlignment.Near
                Graphics.DrawString(Watermark, Font, Brush, New XPoint((Page.Width.Value - WaterMarkSize.Width) / 2, Page.Width.Value), Format)
            End If
    Next

结果.....

有什么想法吗?

【问题讨论】:

  • 为什么要为 landscape 页面设置Page.Orientation = PageOrientation.Portrait
  • 我在另一个帖子forum.pdfsharp.net/viewtopic.php?f=2&t=333 上看到了这个,否则水印添加在错误的旋转位置
  • 啊,我明白了。但是如果你稍后看woody3k的回复,它似乎也需要page.Rotate = 0,然后他们继续解释出了什么问题以及需要“设置[...]横向页面的当前PageSize值正确保存。”
  • 是的,将 page.Rotate 设置为 0 会使最终结果变成纵向,这不是我想要的,我添加了在代码中的各个不同点设置 PageSize 的功能,但它没有不会影响结果。

标签: .net vb.net pdf watermark pdfsharp


【解决方案1】:

DrawString 调用中的 Y 坐标应基于 page.Height 而不是 page.Width

如果您使用 page.Width 而不调整字体高度,则必须使用不同的 LineAlignment

【讨论】:

  • 嗨,我确实尝试过,但使用了 page.Height(这将是合乎逻辑的),但它似乎将水印从页面上移开
  • 您必须设置适当的 LineAlignment 以使文本高于您指定的点。如果文本低于您指定的点,则文本是不可见的。
  • 我明白,但是如果我使用 page.height 作为 Y 坐标,那么无论我设置行对齐方式,水印都会离开页面。
猜你喜欢
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多