vb.net – 在现有的多页PDF中使用iTextSharp / VB将单页旋转90度

我正在尝试将iTextSharp集成到现有的Document Imaging应用程序中,该应用程序允许用户旋转可能以不正确的角度扫描的单个页面(它发生的次数比我想象的要多).

我有实际页面数据在90/180度范围内正确旋转,但页面方向不随其一起旋转.我刚刚开始使用iTextSharp,所以我对它的方法仍然有些不熟悉,但是能够使用StackOverflow的帖子拼凑到目前为止的内容.它很接近,但并不完全.

这是我到目前为止所拥有的:

' Get the input document and total number of pages
Dim inputPdf As New iTextSharp.text.pdf.PdfReader(fileName)
Dim pageCount As Integer = inputPdf.NumberOfPages

' Load the input document 
Dim inputDoc As New iTextSharp.text.Document(inputPdf.GetPageSizeWithRotation(1))

' Set up the file stream for our output document
Dim outFileName As String = Path.ChangeExtension(fileName,"pdf")
Using fs As New FileStream(outFileName,FileMode.Create)
    ' Create the output writer
    Dim outputWriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(inputDoc,fs)
    inputDoc.open()

    ' copy pages from input to output document
    Dim cb As iTextSharp.text.pdf.PdfContentByte = outputWriter.DirectContent
    For index As Integer = 1 To pageCount
        inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(index))
        inputDoc.NewPage()

        ' If this is our page to be rotated,perform the desired transform
        ' Todo - 90 degree rotations need to change the page orientation as well
        Dim page As iTextSharp.text.pdf.PdfImportedPage = outputWriter.GetImportedPage(inputPdf,index)
        If index = pageNum Then
            Select Case angle
                Case 90
                    cb.AddTemplate(page,-1,1,page.Height)
                Case 180
                    cb.AddTemplate(page,page.Width,page.Height)
                Case 270
                    cb.AddTemplate(page,0)
                Case Else
                    ' Should not be here,but don't do anything
                    cb.AddTemplate(page,0)
            End Select
        Else
            ' No rotation; add as is
            cb.AddTemplate(page,0)
        End If
    Next
    inputDoc.Close()
End Using

我尝试将以下代码添加到顶部以从现有页面获取页面大小,并在旋转角度为90或270时交换尺寸:

For index As Integer = 1 To pageCount
Dim pageSize As iTextSharp.text.Rectangle = inputPdf.GetPageSizeWithRotation(index)
If angle = 90 OrElse angle = 270 Then
    ' For 90-degree rotations,change the orientation of the page,too
    pageSize = New iTextSharp.text.Rectangle(pageSize.Height,pageSize.Width)
End If
inputDoc.SetPageSize(pageSize)
inputDoc.NewPage()

不幸的是,这会产生将每页旋转90度的效果,并且我想要旋转的页面上的数据无论如何都没有显示在正确的位置(它向下移动了一些页面).

就像我说的,我并不熟悉API的内部工作原理.我已经在sourceforge页面上在线查看了这些示例,并查看了本书(两个版本),但我没有看到任何符合该法案的内容.我在这里看到一个示例,它显示了新组合PDF的页面方向,但没有显示现有PDF格式的页面方向.有人可以帮帮我吗?谢谢!

你做得比你需要的更难.

您想要旋转页面本身,而不是旋转页面内容

PdfReader reader = new PdfReader(path);
pdfstamper stamper = new pdfstamper( reader,outStream );

PdfDictionary pageDict = reader.getPageN(desiredPage);
int desiredRot = 90; // 90 degrees clockwise from what it is Now
PdfNumber rotation = pageDict.getAsNumber(PdfName.ROTATE);
if (rotation != null) {
  desiredRot += rotation.intValue();
  desiredRot %= 360; // must be 0,90,180,or 270
}
pageDict.put(PdfName.ROTATE,new PdfNumber(desiredRot);


stamper.close();

而已.您可以使用desiredPage和desiredRot来玩,以获得您所追求的任何效果.请享用.

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...