Programmatically Rotate PDF Files
The Problem: A friend’s work photocopier continually scans files in with the wrong page orientation which is obviously both annoying and time consuming going through each PDF file changing the page orientation. After spending a few hours searching on Google I was unable to find a quick fix to this problem so my next stop was the adobe forums. A quick search their revealed a only one topic with someone posting about the exact same problem but other than a totally unhelpful reply from an Adobe employee saying look in the Acrobat API there was no solution.
Thinking it would be easy to find the Acrobat API documentation I begin searching the site, another hour went by and I could not find the documentation anywhere until I found another user’s post linking to it (API attached at the bottom of this post). From here I managed to write the below application to rotate PDF files. Unfortunately you have to have Acrobat professional installed, which can be downloaded on a trial basis if this is a one of project for you. First add the following imports at the top of your page
Imports System.IO
Imports AcroPDFLib
Imports AdobePDFMakerX
Imports Acrobat
Imports System.Runtime.InteropServices
Here is the code for your button
'make sure they selected a folder
If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
'get the path to the directory
Dim dir As DirectoryInfo = New DirectoryInfo(FolderBrowserDialog1.SelectedPath)
'crete an array of file information for all the pdf files in
'this directory
Dim fileInfo(dir.GetFiles("*.pdf").Length - 1) As FileInfo
'get the files
fileInfo = dir.GetFiles("*.pdf")
'loop through each file
For i As Integer = 0 To fileInfo.GetUpperBound(0)
Dim pdf As AcroPDDoc
Try
'create a new pdf object for each file
pdf = New AcroPDDoc
Catch ex As COMException
MessageBox.Show("You need to install Adobe Acrobat Professional 7 or later!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
'try open the file
If Not pdf.Open(fileInfo(i).FullName.ToString) Then
MessageBox.Show("Could not open file" & vbNewLine & fileInfo(i).FullName.ToString, _
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
'loop through each page in the pdf file and and rotate it
For j As Integer = 0 To pdf.GetNumPages - 1
'declare page
Dim page As AcroPDPage
'get next page
page = pdf.AcquirePage(j)
'rotate the page
If page.SetRotate(90) Then
Else
'if we could not rotate the page
MessageBox.Show("Could not rotate page", "Error", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Exit Sub
End If
'save the edited pdf file back to the same location
pdf.Save(1, fileInfo(i).FullName.ToString)
Next
'add the file to the list box when completed
lstFiles.Items.Add(fileInfo(i))
Next
lblResult.Text = "Completed"
MessageBox.Show("All files completed", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Simply add the above code to a button and then all you have to do is add these references. To add a reference go to Project > Show All Files then in the right hand pane right click the references folder and add these references from under COM
- AcroPDFLib
- AdobePDFMakerX
- Acrobat
That's it you should be done, run the application and select the folder containing your pdf files. If you look in the code I have set the rotate to 90 this puts the current top page edge to 90 degrees. If anyone else has tackled this problem I would like to hear of your solutions especially if they avoid having to use Acrobat Professional. Files: API Documentation PS: The reason I did not subit the full solution was due to Adobe having restrictions on been able to distribute their .DLL files. If you email me I will help you set the application up.
Comments
Can this be done on Acrobat 9 Standard?
I believe it required a DLL only found with the professional version but it was quite some time ago. You could try the code above and see how you go.
Excellent post! I never would have thought there was an API for Acrobat... I've got to look into that! :D
Wouldn't it be nice to mention what programming language this blog entry applies to???
I thought it was obvious by the code snippets but i've added a tag for you.
vb.net pdf reverse page…
vb.net pdf reverse page order is what you are trying to do is impossible, what you could try to do is show vb.net pdf reverse page orderwas that what you were trying?, if so, try something like rasteredge
Add new comment