You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.NET中使用iText7添加90度旋转的PDF文本印章?

解决方案:在iText7 VB.NET中添加90度旋转的文本印章

方法一:直接通过PdfCanvas实现旋转(无需Paragraph)

基于你现有的代码,只需在文本绘制流程中加入旋转矩阵设置即可实现90度旋转,核心是利用SetTextMatrix定义旋转变换。

修改后的完整代码:

Shared Sub itext7_stamp_rotated_text_on_pdf(mypdfname As String, myfoldername As String)
    Dim src As String = myfoldername & "\" & mypdfname
    Dim dest As String = myfoldername & "\Stamped " & mypdfname

    Dim pdfDoc As PdfDocument = New PdfDocument(New PdfReader(src), New PdfWriter(dest))
    Dim canvas As PdfCanvas
    Dim n As Integer = pdfDoc.GetNumberOfPages()
    Dim font As PdfFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA)
    Dim fontSize As Single = 12
    Dim rotateAngle As Integer = 90 ' 旋转角度
    Dim x As Single = 100 ' 旋转原点X坐标
    Dim y As Single = 100 ' 旋转原点Y坐标
    Dim text As String = "ROTATED SAMPLE TEXT"

    For i = 1 To n
        Dim page As PdfPage = pdfDoc.GetPage(i)
        canvas = New PdfCanvas(page)

        With canvas
            .SetFontAndSize(font, fontSize)
            .BeginText()
            ' 设置文本矩阵:以(x,y)为原点旋转90度(需将角度转为弧度计算)
            .SetTextMatrix(
                Math.Cos(rotateAngle * Math.PI / 180),
                Math.Sin(rotateAngle * Math.PI / 180),
                -Math.Sin(rotateAngle * Math.PI / 180),
                Math.Cos(rotateAngle * Math.PI / 180),
                x,
                y
            )
            .ShowText(text)
            .EndText()
        End With
    Next

    pdfDoc.Close()
End Sub

关键说明:

  • SetTextMatrix的前四个参数对应旋转变换的矩阵值,通过三角函数计算90度旋转的余弦、正弦值;最后两个参数是旋转原点的坐标,可按需调整文本位置。
  • 90度旋转后文本会沿垂直方向向上排列,修改x、y值可改变文本在页面上的位置。

方法二:使用Paragraph布局元素实现旋转

如果你倾向于用iText7的布局组件实现,VB.NET代码如下:

Shared Sub itext7_stamp_rotated_text_with_paragraph(mypdfname As String, myfoldername As String)
    Dim src As String = myfoldername & "\" & mypdfname
    Dim dest As String = myfoldername & "\Stamped " & mypdfname

    Dim pdfDoc As PdfDocument = New PdfDocument(New PdfReader(src), New PdfWriter(dest))
    Dim document As Document = New Document(pdfDoc)
    Dim n As Integer = pdfDoc.GetNumberOfPages()
    Dim font As PdfFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA)
    Dim fontSize As Single = 12
    Dim text As String = "ROTATED SAMPLE TEXT"
    Dim x As Single = 100
    Dim y As Single = 100

    For i = 1 To n
        Dim paragraph As New Paragraph(text) With {
            .SetFont(font),
            .SetFontSize(fontSize),
            .SetRotationAngle(Math.PI / 2) ' 90度对应的弧度值
        }
        ' 将Paragraph对齐到指定坐标,旋转中心为文本左下角
        document.ShowTextAligned(paragraph, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, Math.PI / 2)
    Next

    pdfDoc.Close()
    document.Close()
End Sub

关键说明:

  • SetRotationAngle接收弧度值,90度对应Math.PI / 2。
  • ShowTextAligned的最后一个参数为旋转角度(弧度),需与Paragraph的旋转角度保持一致;通过TextAlignment和VerticalAlignment可调整文本的对齐基准点。

内容的提问来源于stack exchange,提问作者Ash_stack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 05:50:22