pdf_import/shift_imported_page new
Place an imported PDF page and shift it horizontally and vertically.
Download Java Code Switch to PHP Code Show Output
/*
* Place an imported PDF page and shift it horizontally and vertically
*
* Required software: PDFlib+PDI/PPS 9
* Required data: input PDF
*/
package com.pdflib.cookbook.pdflib.pdf_import;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class shift_imported_page {
public static void main (String argv[])
{
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
/* By default annotations are also imported. In some cases this
* requires the Noto fonts for creating annotation appearance streams.
* We therefore set the searchpath to also point to the font directory.
*/
String fontpath = "../resource/font";
String outfile = "shift_imported_page.pdf";
String title = "Shift Imported Page";
pdflib p = null;
int exitcode = 0;
String pdffile = "lionel.pdf";
try {
int doc, page;
/* Amount to shift imported page to the right and up */
int shift_x = 50, shift_y = 50;
p = new pdflib();
p.set_option("searchpath={" + searchpath + "}");
p.set_option("searchpath={" + fontpath + "}");
/* This means we must check return values of load_font() etc. */
p.set_option("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);
/* Load page of the input PDF document */
doc = p.open_pdi_document(pdffile, "");
if (doc == -1)
throw new Exception("Error: " + p.get_errmsg());
page = p.open_pdi_page(doc, 1, "");
if (page == -1)
throw new Exception("Error: " + p.get_errmsg());
/* --------------------------------------------------------
* Place imported page at the lower left corner of the page
* --------------------------------------------------------
*/
p.begin_page_ext(0, 0, "width=a4.height height=a4.width");
/* Place PDF page in lower left corner */
p.fit_pdi_page(page, 0, 0, "");
p.end_page_ext("");
/* --------------------------------------------------------
* Output the page at the with a shift of the page
* --------------------------------------------------------
*/
p.begin_page_ext(0, 0, "width=a4.height height=a4.width");
/* Place PDF page in its original size, but shift it to the right and up */
p.fit_pdi_page(page, shift_x, shift_y, "");
p.end_page_ext("");
p.close_pdi_page(page);
p.close_pdi_document(doc);
p.end_document("");
} catch (PDFlibException e) {
System.err.println("PDFlib exception occurred:");
System.err.println("[" + e.get_errnum() + "] " + e.get_apiname() +
": " + e.get_errmsg());
exitcode = 1;
} catch (Exception e) {
System.err.println(e);
exitcode = 1;
} finally {
if (p != null) {
p.delete();
}
System.exit(exitcode);
}
}
}