You are on page 1of 3

vb.net - Rotate a single page 90 degrees with iTextShar...

http://stackoverflow.com/ !estions/""#0$9%/rotate-a-si...

sign up

log in

tour

help

careers 2.0

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Rotate a single page 90 degrees with iTextSharp/VB in an existing multi-page PDF

I am trying to integrate iTextSharp into an existing Document Imaging application that allows users to rotate individual pages that may have been scanned in at an incorrect angle (it happens more often than I would have thought). I have the actual page data rotating correctly across 90/180 degrees, but the page orientation is not rotating along with it. I have just started working with iTextSharp, so I'm still a little unfamiliar with its methods, but was able to cobble together what I have so far using posts from StackOverflow. It's close, but not quite there. Here's what I have so far: ' Get the input document and total number of pages Dim inputPdf As New iTextSharp.text.pdf.PdfReader fileName! Dim page"ount As #nteger $ inputPdf.Number%fPages ' Load the input document Dim inputDoc As New iTextSharp.text.Document inputPdf.GetPageSizeWithRotation & ' Set up the file stream for our output document Dim out'ileName As String $ Path."hange(xtension fileName) *pdf*! +sing fs As New 'ileStream out'ileName) 'ile,ode."reate! ' Create the output writer Dim output-riter As iTextSharp.text.pdf.PdfWriter $ iTextSharp.text.pdf.PdfWriter inputDoc.Open ! ' "op. pages from input to output document Dim cb As iTextSharp.text.pdf.Pdf"ontent/.te $ output-riter.Direct"ontent 'or index As #nteger $ & To page"ount inputDoc.SetPageSi0e inputPdf.GetPageSi0e-ithRotation index!! inputDoc.NewPage ! ' If this is our page to be rotated) perform the desired transform ' T%D% 1 23 degree rotations need to change the page orientation as well Dim page As iTextSharp.text.pdf.Pdf#mportedPage $ output-riter.Get#mportedPage inputPdf) i #f index $ pageNum Then Select "ase angle "ase 23 cb.AddTemplate page) 3) 1&) &) 3) 3) page.4eight! "ase &53 cb.AddTemplate page) 1&) 3) 3) 1&) page.-idth) page.4eight! "ase 673 cb.AddTemplate page) 3) &) 1&) 3) page.-idth) 3! "ase (lse ' Should not be here) but don't do an.thing cb.AddTemplate page) &) 3) 3) &) 3) 3! I tried adding the following code to the top to grab the page size from the existing page and swap the dimensions if the rotation angle was 90 or 270: or index As Integer $ & !o page"ount Dim pageSi0e As iTextSharp.text.Rectangle $ inputPdf.GetPageSizeWithRotation If angle $ 23 Or"lse angle $ 673 !hen ' 'or 231degree rotations) change the orientation of the page) too

& of '

(/(0/(0&" ':'$ )*

vb.net - Rotate a single page 90 degrees with iTextShar...

http://stackoverflow.com/ !estions/""#0$9%/rotate-a-si...

pageSi0e $ New iTextSharp.text.Rectangle pageSi0e.4eight) pageSi0e.-idth! (nd #f inputDoc.SetPageSi0e pageSi0e! inputDoc.NewPage ! Unfortunately, this had the effect of rotating every page 90 degrees, and the data on the page I wanted rotated didn't show up in the correct spot anyway (it was shifted down and off the page a bit). Like I said, I'm not really familiar with the inner workings of the API. I have checked out the examples online at the sourceforge page, and have taken a look at the book (both editions), but I don't see anything that fits the bill. I saw an example here that shows page orientation for newly-composed PDFs, but nothing for existing ones. Can anybody help me out? Thanks!
vb.net pdf rotation itextsharp

asked Dec 17 '10 at 13:31 Mike Loux 135 3 12

1 nswer
You're making this harder than it needs to be. Rather than rotating the page contents, you want to rotate the page itself: PdfReader reader $ new PdfReader path!8 PdfStamper stamper $ new PdfStamper reader) outStream !8 PdfDictionar# pageDict $ reader.getPageN desiredPage!8 int desiredRot $ 238 // 90 degrees clockwise from what it is now PdfNum$er rotation $ pageDict.getAsNumber PdfName.R%TAT(!8 if rotation 9$ null! : desiredRot ;$ rotation.int<alue !8 desiredRot =$ >?38 // must be 0, 90, 180, or 270 @ pageDict.put PdfName.R%TAT() new PdfNum$er desiredRot!8

stamper.close !8 That's it. You can play around with desiredPage and desiredRot to get whatever effect you're after. Enjoy.
edited Dec 17 '10 at 20:36 answered Dec 17 '10 at 18:01 Mark Storer 9,507 1 15 43

That...is absolutely perfect. I assumed you meant desiredRot in the pageDict.put command, and not curRot, and it seems to have done the trick. Hmm, I may have to re-evaluate how I'm doing my page flip transform as well. Thank you VERY much! Mike Loux Dec 17 '10 at 18:28 Correct, sorry about that. I edited the code to fix it. Glad to help. Mark Storer Dec 17 '10 at 20:37 I don't suppose this trick would work to do page flips as well, would it? Nah, probably not. I do a transform right now to handle flips, but it has the effect of killing any form data. Then again, I'm guessing that getting scanned PDFs that are flipped will be a very rare thing, so I probably don't need to worry about it much. Thanks again! Mike Loux Dec 19 '10 at 12:45 Nope. Flipping on an axis would take a bit more work. You'd have to transform the page in place (-1 0 0 1 pageWid 0 or 1 0 0 -1 0 pageHei, for horizontal and vertical respectively) and then apply the same transformation to all the annotations' rectangles. Not all that hard with AffineTransform, but still a fair amount of code to write. If you want more detail, ask another question. Mark Storer Dec 20 '10 at 19:33 Yeah, that's what I figured (and, as it turns out, exactly what I did, using cb.AddTemplate). Not knowing too much about matrices (or, rather, forgetting everything I learned in graphics design back in 1994 or so), I'm pleased to see that I at least got my numbers right. :-). I was able to use PDF Stamper to flatten the PDF before doing the flip and that took care of the form field issue. Yay! Mike Loux Dec 27 '10 at 14:03

!ot the answer "ou#re loo$ing %or& Browse other 'uestions tagged ()*net pd%
rotation itextsharp or as$ "our own 'uestion*

( of '

(/(0/(0&" ':'$ )*

vb.net - Rotate a single page 90 degrees with iTextShar...

http://stackoverflow.com/ !estions/""#0$9%/rotate-a-si...

' of '

(/(0/(0&" ':'$ )*

You might also like