Mik*_*oux 7 vb.net pdf rotation itext
我正在尝试将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, 0, -1, 1, 0, 0, page.Height)
Case 180
cb.AddTemplate(page, -1, 0, 0, -1, page.Width, page.Height)
Case 270
cb.AddTemplate(page, 0, 1, -1, 0, page.Width, 0)
Case Else
' Should not be here, but don't do anything
cb.AddTemplate(page, 1, 0, 0, 1, 0, 0)
End Select
Else
' No rotation; add as is
cb.AddTemplate(page, 1, 0, 0, 1, 0, 0)
End If
Next
inputDoc.Close()
End Using
Run Code Online (Sandbox Code Playgroud)
我尝试将以下代码添加到顶部以从现有页面获取页面大小,并在旋转角度为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()
Run Code Online (Sandbox Code Playgroud)
不幸的是,有旋转的效果每 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();
Run Code Online (Sandbox Code Playgroud)
而已.你可以玩,desiredPage
并desiredRot
获得你想要的任何效果.请享用.
归档时间: |
|
查看次数: |
13389 次 |
最近记录: |