Rotate the pages of an existing PDF document. Import a PDF page and place it in the output document with a different orientation.
Download Java Code Show Output PDF
* Rotate Pages:
* Rotate the pages of an existing PDF document
*
* Import a PDF page and place it in the output document with a different
* orientation.
*
* Required software: PDFlib+PDI/PPS 7
* Required data: PDF document
*/
package com.pdflib.cookbook.pdflib.pdf_import;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class rotate_pages
{
public static void main (String argv[])
{
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "rotate_pages.pdf";
String title = "Rotate Pages";
pdflib p = null;
String pdffile = "PDFlib-real-world.pdf";
int indoc, pageno, endpage, page;
try {
p = new pdflib();
p.set_parameter("SearchPath", searchpath);
/* This means we must check return values of load_font() etc. */
p.set_parameter("errorpolicy", "return");
if (p.begin_document(outfile, "") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title + " $Revision: 1.10 $");
/* Open the input PDF */
indoc = p.open_pdi_document(pdffile, "");
if (indoc == -1)
throw new Exception("Error: " + p.get_errmsg());
endpage = (int) p.pcos_get_number(indoc, "/Root/Pages/Count");
/* Loop over all pages of the input document */
for (pageno = 1; pageno <= endpage; pageno++)
{
page = p.open_pdi_page(indoc, pageno, "");
if (page == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Dummy page size; will be adjusted later */
p.begin_page_ext(10, 10, "");
/* Place the imported page on the output page. Adjust the page size
* automatically to the size of the imported page. Orientate the
* page to the west; similarly you can orientate it to the east or
* south, if required.
*/
p.fit_pdi_page(page, 0, 0, "adjustpage orientate=west");
p.close_pdi_page(page);
p.end_page_ext("");
}
p.end_document("");
} catch (PDFlibException e) {
System.err.print("PDFlib exception occurred:\n");
System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +
": " + e.get_errmsg() + "\n");
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
if (p != null) {
p.delete();
}
}
}
}