加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-17 00:23:18 所属栏目:大数据 来源:网络整理
导读:我正在尝试将iTextSharp集成到现有的Document Imaging应用程序中,该应用程序允许用户旋转可能以不正确的角度扫描的单个页面(它发生的次数比我想象的要多). 我有实际页面数据在90/180度范围内正确旋转,但页面方向不随其一起旋转.我刚刚开始使用iTextSharp,所
我正在尝试将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来玩,以获得您所追求的任何效果.请享用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读