ITextSharp旋转页面上关于某个点的所有文本

时间:2015-05-19 07:40:49

标签: .net pdf rotation itextsharp affinetransform

我只是花了一个小时来解决这个问题,我确信这不是最好的解决方案;我很高兴听到是否有更好的解决方案。

我需要在页面上旋转关于某个点的所有文本。我尝试将点转换为原点,执行旋转然后转换回来,但似乎相对于旋转变换发生了最终的变换(考虑到它,这是有意义的。)

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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