Rotate PDF Pages with Go
Contents
[
Hide
]
This section describes how to change the page orientation from landscape to portrait and vice versa in an existing PDF file using Go.
Rotating pages ensures proper alignment for printing or displaying PDFs in professional settings
- Open the PDF Document.
- Rotate PDF Pages. The PageRotate function applies a specific rotation (in this case, 180 degrees) to a given page.
- Save Changes to a New File. The SaveAs function creates a new PDF file to preserve the original while storing the updated version.
In this example, you rotate a specific page in a PDF document:
package main
import "github.com/aspose-pdf/aspose-pdf-go-cpp"
import "log"
func main() {
// Open(filename string) opens a PDF-document with filename
pdf, err := asposepdf.Open("sample.pdf")
if err != nil {
log.Fatal(err)
}
// PageRotate(num int32, rotation int32) rotates page
err = pdf.PageRotate(1, asposepdf.RotationOn180)
if err != nil {
log.Fatal(err)
}
// SaveAs(filename string) saves previously opened PDF-document with new filename
err = pdf.SaveAs("sample_page1_Rotate.pdf")
if err != nil {
log.Fatal(err)
}
// Close() releases allocated resources for PDF-document
defer pdf.Close()
}
You also have the option to rotate all PDF pages in your document:
package main
import "github.com/aspose-pdf/aspose-pdf-go-cpp"
import "log"
func main() {
// Open(filename string) opens a PDF-document with filename
pdf, err := asposepdf.Open("sample.pdf")
if err != nil {
log.Fatal(err)
}
// Rotate(rotation int32) rotates PDF-document
err = pdf.Rotate(asposepdf.RotationOn270)
if err != nil {
log.Fatal(err)
}
// SaveAs(filename string) saves previously opened PDF-document with new filename
err = pdf.SaveAs("sample_Rotate.pdf")
if err != nil {
log.Fatal(err)
}
// Close() releases allocated resources for PDF-document
defer pdf.Close()
}