【发布时间】: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