【问题标题】:ITextSharp rotate all text on a page about a pointITextSharp 围绕一个点旋转页面上的所有文本
【发布时间】:2015-05-19 07:40:48
【问题描述】:

我刚刚花了一个小时想出了一个解决方案,我敢肯定这不是最好的;如果有更好的解决方案,我会很高兴。

我需要围绕一个点旋转页面上的所有文本。我尝试将点转换为原点,执行旋转然后再转换回来,但似乎最终的转换是相对于旋转转换发生的(考虑一下,这是有道理的)。

这是我的代码的降低复杂度的版本:

imports itextsharp.text

public class Example

    public shared function RotateAboutPoint(byval sourceReader as pdf.pdfreader, byval rads as double, byval centreX as double, byval centreY as double) as pdf.pdfreader            

        Using ms As New System.IO.MemoryStream
            Using doc As New Document
                Using writer As pdf.PdfWriter = pdf.PdfWriter.GetInstance(doc, ms)
                    doc.Open()
                    For i As Integer = 1 To sourceReader.NumberOfPages
                        Dim p As pdf.PdfTemplate = writer.GetImportedPage(sourceReader, i)

                        dim trans as new itextsharp.awt.geom.affinetransform
                        trans.translate(-centrex, -centrey)
                        trans.rotate(-rads)
                        trans.translate(centerx, centery)

                        doc.setpagesize(sourcereader.getpagesizewithrotation(I))
                        doc.newpage()

                        writer.DirectContent.AddTemplate(p, trans)
                    Next i
                    doc.Close()
                End Using
            End Using
            sourceReader.Close()
            sourceReader.Dispose()
            return New pdf.PdfReader(ms.ToArray)
        End Using
    end function
end class

【问题讨论】:

    标签: .net pdf rotation itextsharp affinetransform


    【解决方案1】:

    这是我遇到的解决方案,基本上是在没有平移的情况下执行旋转,然后通过添加两个向量进行平移,一个从旋转原点到所需的旋转中心,一个从(所需的旋转中心将被旋转到)到旋转原点

    imports itextsharp.text
    
    public class Example
    
        public shared function RotateAboutPoint(byval sourceReader as pdf.pdfreader, byval rads as double, byval centreX as double, byval centreY as double) as pdf.pdfreader            
    
            Using ms As New System.IO.MemoryStream
                Using doc As New Document
                    Using writer As pdf.PdfWriter = pdf.PdfWriter.GetInstance(doc, ms)
                        doc.Open()
                        For i As Integer = 1 To sourceReader.NumberOfPages
                            Dim p As pdf.PdfTemplate = writer.GetImportedPage(sourceReader, i)
    
                            Dim transAdjust As New iTextSharp.awt.geom.AffineTransform
                            transAdjust.SetToIdentity()
                            transAdjust.Translate(centreX - ((centreX * System.Math.Cos(rads)) + (centreY * System.Math.Sin(rads))), centreY - ((centreX * -System.Math.Sin(rads)) + (centreY * System.Math.Cos(rads))))
                            Dim transRotate As New iTextSharp.awt.geom.AffineTransform
                            transRotate.SetToIdentity()
                            transRotate.Rotate(-rads)
    
                            Dim finalTrans As New iTextSharp.awt.geom.AffineTransform
                            finalTrans.SetToIdentity()
                            finalTrans.Concatenate(transAdjust)
                            finalTrans.Concatenate(transRotate)
    
                            doc.setpagesize(sourcereader.getpagesizewithrotation(I))
                            doc.newpage()
    
                            writer.DirectContent.AddTemplate(p, finalTrans)
                        Next i
                        doc.Close()
                    End Using
                End Using
                sourceReader.Close()
                sourceReader.Dispose()
                return New pdf.PdfReader(ms.ToArray)
            End Using
        end function
    end class
    

    感觉很乱,但它确实有效 - 如果有更简单的方法使用转换和旋转的组合,我会非常感兴趣。

    【讨论】:

    • 如果旋转角度只能是 0、90、180 或 270(中间没有值),那么有一种更简单的方法可以实现您的目标。如果角度可以有 any 值(不仅仅是 90 的倍数),那么您确实需要进行数学运算,使用 AffineTransform 是更 人类可读 的方式去做(虽然它看起来仍然很复杂)。
    • @BrunoLowagie 感谢您的评论。恐怕我需要旋转任意数量,所以我不能使用页面旋转的东西。您是否知道使用链式 AffineTransforms 实现我的目标的更简单方法,而无需使用所有三角函数?
    • 您可以计算“乘”不同的变换,得到 6 个值 abcdef使用其他AddTemplate() 方法。 ef 值定义了翻译。在轮换的情况下,ad 将是 0,而 b 将是 -sin(angle)d 将是 cos(angle)。检查The ABC of PDF with iText 的第 4.2.5.1 节以获取正确的数学(我只是从头顶写这个)。
    猜你喜欢
    • 2023-04-03
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 2013-05-20
    相关资源
    最近更新 更多