List of usage examples for com.itextpdf.text.pdf PdfStamper setRotateContents
public void setRotateContents(final boolean rotateContents)
From source file:app.App.java
private void clonePDF() throws DocumentException, FileNotFoundException, IOException, ClassNotFoundException { PdfReader reader = new PdfReader(INPUT); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(OUTPUT)); stamper.setRotateContents(false); int n = reader.getNumberOfPages(); for (int i = 0; i < n; i++) { setLink(i, stamper);//from w ww . j a va 2 s .c om } stamper.close(); reader.close(); }
From source file:controller.DownloadCVServlet.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * * @param request servlet request/*w w w. j a v a 2 s . c o m*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //protect servlet HttpSession session = request.getSession(); Admin loggedInAdmin = (Admin) session.getAttribute("admin"); //check if admin is logged in if (loggedInAdmin == null) { response.sendRedirect("login.jsp"); return; } String[] appIDs = request.getParameterValues("download"); ServletOutputStream sOut = response.getOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); //prepare fonts Font font = FontFactory.getFont("Arial", 10); for (String appIDStr : appIDs) { int appID = Integer.parseInt(appIDStr); Application application = ApplicationDAO.retrieveByAppID(appID); Job job = JobDAO.retrieveJobById(application.getJobID()); ZipEntry entry = new ZipEntry(application.getFullname() + "_CV.pdf"); zos.putNextEntry(entry); String path = System.getenv("OPENSHIFT_DATA_DIR"); if (path == null) { path = getServletContext().getRealPath("/templates/Personal_Particulars_Form.pdf"); } else { path += "Personal_Particulars_Form.pdf"; } try { //Prepare PdfStamper PdfReader reader = new PdfReader(path); PdfStamper stamper = new PdfStamper(reader, zos); stamper.setRotateContents(false); stamper.getWriter().setCloseStream(false); //Get first page PdfContentByte canvas = stamper.getOverContent(1); //Application ID ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getAppID() + "", font), 110, 555, 0); //Date Applied ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getDateApplied(), font), 110, 526, 0); //Position Applied ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(job.getPostingTitle(), font), 36, 442, 0); //Job ID ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getJobID() + "", font), 405, 442, 0); //Name ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getFullname(), font), 36, 350, 0); //Street ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getBlkStreetUnit(), font), 36, 305, 0); //Postal Code ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getPostalCode(), font), 377, 305, 0); //Nationality ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getNricType(), font), 36, 260, 0); //NRIC ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getNric(), font), 289, 260, 0); //DOB ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getDob(), font), 36, 215, 0); //Gender ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getGender(), font), 379, 215, 0); //Contact Number ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getContactNo(), font), 36, 170, 0); //Email Address ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(application.getEmailAddress(), font), 36, 125, 0); //Declaration ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("X", font), 50, 80, 0); //Generated on DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Singapore")); Date date = new Date(); String today = dateFormat.format(date); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(today, font), 437, 15, 0); stamper.close(); reader.close(); } catch (DocumentException e) { e.printStackTrace(); } } zos.close(); response.setContentType("application/zip"); response.addHeader("Content-Disposition", "attachment; filename=CVs.zip"); sOut.write(baos.toByteArray()); sOut.close(); }
From source file:watermarkpdf.FXMLDocumentController.java
@FXML private void handleButtonAction(ActionEvent event) throws DocumentException, IOException { if (tfPhrase.getLength() > 0) { lbl_Result.setText(""); PdfReader reader = new PdfReader(tf_PathFile.getText()); Rectangle mediabox = reader.getPageSize(1); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream( tf_PathFile.getText().substring(0, tf_PathFile.getLength() - 4) + "_out.pdf")); stamper.setRotateContents(false); BaseFont bf = BaseFont.createFont("TNR.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font = new Font(bf); int iPos = tfPhrase.getText().indexOf('#'); for (int i = 1; i <= reader.getNumberOfPages(); i++) { PdfContentByte canvas = stamper.getOverContent(i); if (iPos >= 0) { if (cbox_LU.isSelected()) { switch (i) { case 1: ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase((tfPhrase.getText().substring(0, iPos - 5) + "? ? " + tfPhrase.getText().substring(iPos + 1)), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); break; case 2: ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase((tfPhrase.getText().substring(0, iPos - 5) + " ? " + tfPhrase.getText().substring(iPos + 1)), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); break; default: ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase((tfPhrase.getText().substring(0, iPos) + (i - 1) + tfPhrase.getText().substring(iPos + 1)), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); }/*from w w w.j av a2 s . com*/ } else { if (i == 1) ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase((tfPhrase.getText().substring(0, iPos - 5) + " ? " + tfPhrase.getText().substring(iPos + 1)), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); else ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase((tfPhrase.getText().substring(0, iPos) + (i) + tfPhrase.getText().substring(iPos + 1)), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); } } else { ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(tfPhrase.getText(), font), mediabox.getRight() / 3, mediabox.getTop() - 40, 0); } ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(tf_append.getText(), font), mediabox.getRight() * 2 / 3, mediabox.getTop() - 20, 0); } stamper.close(); reader.close(); if (tf_PagePrint.getLength() > 0) { reader = new PdfReader( tf_PathFile.getText().substring(0, tf_PathFile.getLength() - 4) + "_out.pdf"); reader.selectPages(tf_PagePrint.getText()); stamper = new PdfStamper(reader, new FileOutputStream( tf_PathFile.getText().substring(0, tf_PathFile.getLength() - 4) + "_out_cut.pdf")); stamper.close(); reader.close(); } lbl_Result.setText(" "); } }