我试图将iTextSharp整合到现有的文档图像应用程序中,该应用程序允许用户旋转可能以不正确的角度扫描的单页它发生的次数比我想象的要多)。使用iTextSharp/VB在现有的多页PDF中旋转单页90度
我的实际页面数据在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
我尝试添加以下代码到顶部抢从现有页面的页面大小和交换的尺寸如果旋转角度为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文件没有任何内容。有人可以帮我吗?谢谢!
那......绝对是完美的。我认为你的意思是期望在pageDict.put命令中运行,而不是curRot,并且它似乎已经成功了。嗯,我可能不得不重新评估我如何进行翻页转换。非常感谢你! – 2010-12-17 18:28:28
正确,对不起。我编辑了代码来修复它。乐意效劳。 – 2010-12-17 20:37:30
我不认为这个技巧也可以做页面翻转,是吗?不,可能不是。我现在正在进行转换来处理翻转,但它具有杀死任何表单数据的效果。然后再次,我猜测,获得扫描的PDF翻转将是一件非常罕见的事情,所以我可能不需要担心它太多。再次感谢! – 2010-12-19 12:45:40