我試圖將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