List of usage examples for com.itextpdf.text.pdf PdfReader getNumberOfPages
public int getNumberOfPages()
From source file:watermark.java
public static void main(String[] args) { try {//w w w . j a v a2s . c o m PdfReader reader = new PdfReader(args[0]); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(args[1])); stamp.getWriter().setCompressionLevel(9); // without this code it'll 3 times bigger int bottom_margin = Integer.parseInt(args[2]); String lic = String.format(LIC, args[3]); BaseFont bf = BaseFont.createFont(FONTFILE, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Chunk c = new Chunk(lic, new Font(bf, 7, Font.NORMAL, TCOLOR)); c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 1 / 800f, null); Phrase phrase = new Phrase(c); int pages = reader.getNumberOfPages(); int xpos = 90; for (int i = 1; i <= pages; i++) { if (i % 2 == 0) xpos = 160; else xpos = 90; PdfContentByte under = stamp.getUnderContent(i); ColumnText.showTextAligned(under, Element.ALIGN_LEFT, phrase, xpos, bottom_margin, -0); } stamp.setFullCompression(); // without this code it'll 3 times bigger stamp.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Billing.java
public boolean savePaymentRemittanceToPDF(ArrayList<String> info) { try {/* w ww .j av a 2s . c om*/ // TODO add your handling code here: BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt")); String path = ""; try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } path = sb.toString().trim(); } finally { br.close(); } Document doc = new Document(); FileOutputStream fos = new FileOutputStream( path + "\\residentBilling\\" + info.get(0) + "Payment Remittance.pdf"); PdfWriter.getInstance(doc, fos); PreparedStatement preparedStatement = connection.prepareStatement( "SELECT * FROM billing WHERE residentIdnum = ? AND billingStatus LIKE 'unpaid'"); preparedStatement.setInt(1, Integer.parseInt(info.get(7))); ResultSet rs = preparedStatement.executeQuery(); rs.next(); PdfReader pdfReader = new PdfReader("Payment Remittance.pdf"); PdfStamper pdfStamper = new PdfStamper(pdfReader, fos); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { PdfContentByte content = pdfStamper.getOverContent(i); //Text over the existing page BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED); content.beginText(); content.setFontAndSize(bf, 11); content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Control Number: " + info.get(4), 300, 260, 0); if (info.get(5).equals("cash")) { content.showTextAligned(PdfContentByte.ALIGN_LEFT, "X", 346, 218, 0); } else if (info.get(5).equals("cheque")) { content.showTextAligned(PdfContentByte.ALIGN_LEFT, "X", 347, 198, 0); } else { content.showTextAligned(PdfContentByte.ALIGN_LEFT, "X", 347, 178, 0); } content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(6), 330, 140, 0); //Date content.showTextAligned(PdfContentByte.ALIGN_LEFT, new SimpleDateFormat("MM/dd/yyyy") .format(dateLabel.getDateFormat().parse(dateLabel.getText())), 460, 260, 0); //Resident name content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(0), 90, 250, 0); //Purpose of payment content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Resident Monthly Billing", 90, 223, 0); //Room Details content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(1), 125, 183, 0); //Amount content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(2), 125, 158, 0); //Received By admin name! content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(3), 125, 105, 0); content.endText(); } pdfStamper.close(); pdfReader.close(); fos.close(); return true; } catch (DocumentException | SQLException | ParseException ex) { // Logger.getLogger(Billing.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); return false; } catch (FileNotFoundException ex) { // Logger.getLogger(Billing.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); return false; } catch (IOException ex) { // Logger.getLogger(Billing.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); return false; } }
From source file:Separator.java
License:Apache License
public static void crop(String inFileName, String outFileName, int split, boolean vert) throws Exception { PdfReader reader = new PdfReader(new File(inFileName).getAbsolutePath()); String PDFName = outFileName.substring(outFileName.lastIndexOf("Folder\\") + 7, outFileName.indexOf(".pdf")); File fn = new File(scanFolder + PDFName + "\\" + PDFName + "_split_" + split + ".pdf"); fn.getParentFile().mkdirs();//w w w . java2s. c o m int count = reader.getNumberOfPages(); Document doc = new Document(); PdfCopy copy = new PdfCopy(doc, new FileOutputStream(fn.getAbsolutePath())); doc.open(); if (vert) { for (int i = 1; i <= count; i++) { reader.getPageN(i).put(PdfName.CROPBOX, new PdfRectangle(PageSize.LETTER)); copy.addPage(copy.getImportedPage(reader, i)); } } else { if (!doubSided) { for (int j = 1; j <= count; j++) { reader.getPageN(j).put(PdfName.CROPBOX, new PdfRectangle((new Rectangle(0, 180, 792, 792)))); PdfDictionary pageDict; int rot = reader.getPageRotation(j); pageDict = reader.getPageN(j); pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 90)); copy.addPage(copy.getImportedPage(reader, j)); } } else { for (int j = 1; j <= count; j++) { reader.getPageN(j).put(PdfName.CROPBOX, new PdfRectangle(new Rectangle(0, 180, 792, 792))); PdfDictionary pageDict; pageDict = reader.getPageN(j); if (j % 2 == 0) { // even pageDict.put(PdfName.ROTATE, new PdfNumber(270)); } else { // odd pageDict.put(PdfName.ROTATE, new PdfNumber(90)); } copy.addPage(copy.getImportedPage(reader, j)); } } } doc.close(); }
From source file:Registration_Tenant.java
private void saveToPdf() { try {/*from w w w . j av a2 s. com*/ String gend = ""; Document doc = new Document(); BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt")); String path = ""; try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } path = sb.toString().trim(); } finally { br.close(); } FileOutputStream fos = new FileOutputStream( path + "\\registration\\" + lastNameTextField.getText().trim() + ", " + firstNameTextField.getText().trim() + "RegistrationResidentForm.pdf"); PdfWriter pdfWriter = PdfWriter.getInstance(doc, fos); PdfReader pdfReader = new PdfReader("RegistrationResidentForm.pdf"); PdfStamper pdfStamper = new PdfStamper(pdfReader, fos); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { PdfContentByte content = pdfStamper.getOverContent(i); byte[] cc = null; if (file != null) { File thisFile = new File(file); cc = Files.readAllBytes(thisFile.toPath()); } if (cc != null) { Image image1 = Image.getInstance(cc); image1.scaleAbsolute(95, 105); image1.setAbsolutePosition(465f, 635f); content.addImage(image1); } //Text over the existing page BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED); content.beginText(); content.setFontAndSize(bf, 10); //last name content.showTextAligned(PdfContentByte.ALIGN_LEFT, lastNameTextField.getText().trim(), 175, 610, 0); //first name content.showTextAligned(PdfContentByte.ALIGN_LEFT, firstNameTextField.getText().trim(), 325, 610, 0); //middle name content.showTextAligned(PdfContentByte.ALIGN_LEFT, middleNameTextField.getText().trim(), 470, 610, 0); //college content.showTextAligned(PdfContentByte.ALIGN_LEFT, collegeTextField.getText().trim(), 180, 582, 0); //Course and year content.showTextAligned(PdfContentByte.ALIGN_LEFT, courseTextField.getText().trim() + " - " + yearTextField.getText().trim(), 180, 568, 0); //department content.showTextAligned(PdfContentByte.ALIGN_LEFT, departmentTextField.getText().trim(), 400, 582, 0); String g; if (male.isSelected()) { g = "Male";//6 } else { g = "Female";//6 } //Sex content.showTextAligned(PdfContentByte.ALIGN_LEFT, g, 400, 568, 0); //Home address content.showTextAligned(PdfContentByte.ALIGN_LEFT, homeAddressTextArea.getText(), 110, 540, 0); //resident mobile no1 content.showTextAligned(PdfContentByte.ALIGN_LEFT, MobileNumber1.getText().trim(), 187, 485, 0); //resident mobile no2 content.showTextAligned(PdfContentByte.ALIGN_LEFT, MobileNumber2.getText().trim(), 187, 470, 0); //student email content.showTextAligned(PdfContentByte.ALIGN_LEFT, email.getText().trim(), 397, 485, 0); //fathers name content.showTextAligned(PdfContentByte.ALIGN_LEFT, fatherNameTextField.getText().trim(), 110, 443, 0); //fathers area code content.showTextAligned(PdfContentByte.ALIGN_LEFT, fACodeTextField.getText().trim(), 300, 443, 0); //father landline content.showTextAligned(PdfContentByte.ALIGN_LEFT, fphoneNumberTextField.getText().trim(), 355, 443, 0); //father mobile content.showTextAligned(PdfContentByte.ALIGN_LEFT, fMobileNumberTextField.getText().trim(), 490, 443, 0); //father email content.showTextAligned(PdfContentByte.ALIGN_LEFT, fEmailTextField.getText().trim(), 110, 430, 0); //mother name content.showTextAligned(PdfContentByte.ALIGN_LEFT, motherNameTextField.getText().trim(), 110, 402, 0); //mother area code content.showTextAligned(PdfContentByte.ALIGN_LEFT, mACodeTextField.getText().trim(), 300, 402, 0); //mother landline content.showTextAligned(PdfContentByte.ALIGN_LEFT, mphoneNumberTextField.getText().trim(), 355, 402, 0); //mother mobile content.showTextAligned(PdfContentByte.ALIGN_LEFT, mMobileNumberTextField.getText().trim(), 490, 402, 0); //mother email content.showTextAligned(PdfContentByte.ALIGN_LEFT, mEmailTextField.getText().trim(), 110, 387, 0); //guardian name content.showTextAligned(PdfContentByte.ALIGN_LEFT, guardianNameTextField.getText().trim(), 170, 335, 0); //guardian contact number content.showTextAligned(PdfContentByte.ALIGN_LEFT, gContactNumber1.getText().trim(), 470, 335, 0); //guardian address content.showTextAligned(PdfContentByte.ALIGN_LEFT, guardianAddress.getText().trim(), 125, 322, 0); //guardian relation content.showTextAligned(PdfContentByte.ALIGN_LEFT, relationToGuardian.getText().trim(), 170, 307, 0); content.setFontAndSize(bf, 12); //room details content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Room No.: " + roomNumberTextField.getSelectedItem().toString().trim() + " - " + roomTypeTextField.getText().trim(), 355, 235, 0); content.endText(); } pdfStamper.close(); pdfReader.close(); fos.close(); pdfWriter.close(); } catch (DocumentException ex) { // Logger.getLogger(Registration_Tenant.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); } catch (FileNotFoundException ex) { // Logger.getLogger(Registration_Tenant.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); } catch (IOException ex) { // Logger.getLogger(Registration_Tenant.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); } }
From source file:Registration_Transient.java
private void saveToPdf() { try {//from w ww .j av a2s . c o m com.itextpdf.text.Document doc = new com.itextpdf.text.Document(); BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt")); String path = ""; try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } path = sb.toString().trim(); } finally { br.close(); } FileOutputStream fos = new FileOutputStream(path + "\\transient\\" + lastNameTextField.getText().trim() + ", " + firstNameTextField.getText().trim() + "TransientForm.pdf"); PdfWriter.getInstance(doc, fos); PdfReader pdfReader = new PdfReader("TransientForm.pdf"); PdfStamper pdfStamper = new PdfStamper(pdfReader, fos); //doc.open(); //doc.add(new Paragraph(contents.getText().trim() + "-markram")); float varPosition = 0; for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { PdfContentByte content = pdfStamper.getOverContent(i); //Text over the existing page BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED); content.beginText(); content.setFontAndSize(bf, 10); varPosition = 15; //last name content.showTextAligned(PdfContentByte.ALIGN_LEFT, lastNameTextField.getText().trim() + ", " + firstNameTextField.getText().trim(), 100, 661 + varPosition, 0); //first name content.showTextAligned(PdfContentByte.ALIGN_LEFT, new SimpleDateFormat("MM/dd/yyyy").format(Calendar.getInstance().getTime()), 400, 661 + varPosition, 0); if (residentNameCheckBox.isSelected()) { content.showTextAligned(PdfContentByte.ALIGN_LEFT, residentComboBox.getSelectedItem().toString(), 125, 633 + varPosition, 0); content.showTextAligned(PdfContentByte.ALIGN_LEFT, residentComboBox.getSelectedItem().toString(), 470, 633 + varPosition, 0); } else { content.showTextAligned(PdfContentByte.ALIGN_LEFT, "N / A", 125, 633 + varPosition, 0); content.showTextAligned(PdfContentByte.ALIGN_LEFT, "N / A", 470, 633 + varPosition, 0); } content.showTextAligned(PdfContentByte.ALIGN_LEFT, mobileNoTextField.getText(), 135, 590 + varPosition, 0); content.showTextAligned(PdfContentByte.ALIGN_LEFT, emailTextField.getText(), 400, 590 + varPosition, 0); content.showTextAligned(PdfContentByte.ALIGN_LEFT, addressTextField.getText(), 175, 577 + varPosition, 0); varPosition = 35; content.showTextAligned(PdfContentByte.ALIGN_LEFT, roomReservedTextField.getText(), 135, 507 + varPosition, 0); content.showTextAligned(PdfContentByte.ALIGN_LEFT, dateArrivalChooserCombo.getText(), 135, 493 + varPosition, 0); content.showTextAligned(PdfContentByte.ALIGN_LEFT, dateDepartureChooserCombo.getText(), 435, 493 + varPosition, 0); content.showTextAligned(PdfContentByte.ALIGN_LEFT, guestSpinner.getValue().toString(), 180, 478 + varPosition, 0); for (int count = 0; count < guestTable.getRowCount(); count++) { float x = 50; if (count <= 3) { content.showTextAligned(PdfContentByte.ALIGN_LEFT, guestTable.getValueAt(count, 0).toString(), x, 435 + varPosition, 0); varPosition -= 13; if (count == 3) { varPosition = 35; } } else if (count >= 4 & count <= 7) { content.showTextAligned(PdfContentByte.ALIGN_LEFT, guestTable.getValueAt(count, 0).toString(), x + 200, 435 + varPosition, 0); varPosition -= 13; if (count == 7) { varPosition = 35; } } else { content.showTextAligned(PdfContentByte.ALIGN_LEFT, guestTable.getValueAt(count, 0).toString(), x + 400, 435 + varPosition, 0); varPosition -= 13; } } content.endText(); } pdfStamper.close(); pdfReader.close(); } catch (DocumentException ex) { // Logger.getLogger(Registration_Transient.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); } catch (FileNotFoundException ex) { // Logger.getLogger(Registration_Transient.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); } catch (IOException ex) { // Logger.getLogger(Registration_Transient.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); } }
From source file:Billing_v2.java
public boolean saveStatementOfAccountToPDF(ArrayList<String> info) { try {//from w w w .j a va2 s . com BufferedReader br = new BufferedReader(new FileReader("dir\\defaults.txt")); String path = ""; try { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } path = sb.toString().trim(); } finally { br.close(); } com.itextpdf.text.Document doc = new com.itextpdf.text.Document(); FileOutputStream fos = new FileOutputStream( path + "\\residentStatementOfAccount\\" + info.get(0) + "statementOfAccounts.pdf"); PdfWriter pdfWriter = PdfWriter.getInstance(doc, fos); PdfReader pdfReader = new PdfReader("statementOfAccounts.pdf"); PdfStamper pdfStamper = new PdfStamper(pdfReader, fos); java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(info.get(6)); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { PdfContentByte content = pdfStamper.getOverContent(i); BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED); content.beginText(); content.setFontAndSize(bf, 10); //month content.showTextAligned(PdfContentByte.ALIGN_LEFT, new SimpleDateFormat("MMMM").format(date), 205, 625, 0); //name content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(0), 125, 610, 0); //roomNumber content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(1), 130, 598, 0); //monthly rate content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(2), 205, 560, 0); //Shuttle rate content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(3), 205, 545, 0); //gadget rate content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(4), 205, 530, 0); //additional fee content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(5), 205, 518, 0); //total amount Double totalAmount = Double.parseDouble(info.get(2)) + Double.parseDouble(info.get(3)) + Double.parseDouble(info.get(4)); content.showTextAligned(PdfContentByte.ALIGN_LEFT, totalAmount + "", 205, 490, 0); //due date Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, Calendar.MONTH - 1); content.showTextAligned(PdfContentByte.ALIGN_LEFT, info.get(7), 130, 463, 0); content.endText(); } pdfStamper.close(); pdfReader.close(); fos.close(); pdfWriter.close(); return true; } catch (DocumentException | ParseException ex) { // Logger.getLogger(Billing_v2.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); return false; } catch (FileNotFoundException ex) { // Logger.getLogger(Billing_v2.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); return false; } catch (IOException ex) { // Logger.getLogger(Billing_v2.class.getName()).log(Level.SEVERE, null, ex); new MessageDialog().error(this, ex.getMessage()); return false; } }
From source file:adams.flow.transformer.PDFExtract.java
License:Open Source License
/** * Executes the flow item./*from w ww . j a v a 2 s . c o m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; File file; int i; Document document; PdfCopy copy; PdfReader reader; int[] pages; int page; FileOutputStream fos; result = null; // get file if (m_InputToken.getPayload() instanceof File) file = (File) m_InputToken.getPayload(); else file = new PlaceholderFile((String) m_InputToken.getPayload()); fos = null; try { if (isLoggingEnabled()) getLogger().info("Extracting pages from '" + file + "' into '" + m_Output + "'"); document = new Document(); fos = new FileOutputStream(m_Output.getAbsolutePath()); copy = new PdfCopy(document, fos); document.open(); document.addCreationDate(); document.addCreator(Environment.getInstance().getProject()); document.addAuthor(System.getProperty("user.name")); reader = new PdfReader(file.getAbsolutePath()); if (isLoggingEnabled()) getLogger().info("- #pages: " + reader.getNumberOfPages()); m_Pages.setMax(reader.getNumberOfPages()); pages = m_Pages.getIntIndices(); for (i = 0; i < pages.length; i++) { page = pages[i] + 1; copy.addPage(copy.getImportedPage(reader, page)); if (isLoggingEnabled()) getLogger().info("- adding page: " + page); } copy.freeReader(reader); document.close(); } catch (Exception e) { result = handleException("Failed to extract pages: ", e); } finally { FileUtils.closeQuietly(fos); } if (result == null) m_OutputToken = new Token(m_Output.getAbsolutePath()); return result; }
From source file:adams.flow.transformer.PDFInfo.java
License:Open Source License
/** * Executes the flow item./*from w w w. j a v a 2 s . c om*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; File file; PdfReader reader; result = null; m_Queue = new ArrayList(); file = null; if (m_InputToken.getPayload() instanceof String) file = new PlaceholderFile((String) m_InputToken.getPayload()); else if (m_InputToken.getPayload() instanceof File) file = new PlaceholderFile((File) m_InputToken.getPayload()); else result = "Unhandled input type: " + Utils.classToString(m_InputToken.getPayload()); reader = null; if (result == null) { try { reader = new PdfReader(file.getAbsolutePath()); } catch (Exception e) { result = handleException("Failed to open PDF file: " + file, e); } } if (result == null) { m_PageIndex.setMax(reader.getNumberOfPages()); switch (m_Type) { case NUM_PAGES: m_Queue.add(reader.getNumberOfPages()); break; case WIDTH: m_Queue.add(new Double(reader.getPageSize(m_PageIndex.getIntIndex() + 1).getWidth())); break; case HEIGHT: m_Queue.add(new Double(reader.getPageSize(m_PageIndex.getIntIndex() + 1).getHeight())); break; default: result = "Unhandled info type: " + m_Type; } } if (reader != null) reader.close(); return result; }
From source file:adams.flow.transformer.PDFMerge.java
License:Open Source License
/** * Executes the flow item./* www . j a va 2 s . c o m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; PlaceholderFile[] files; int i; int n; int pages; Document document; PdfCopy copy; PdfReader reader; FileOutputStream fos; result = null; // get files files = FileUtils.toPlaceholderFileArray(m_InputToken.getPayload()); fos = null; try { if (isLoggingEnabled()) getLogger().info("Merging PDFs into: " + m_Output); document = new Document(); fos = new FileOutputStream(m_Output.getAbsolutePath()); copy = new PdfCopy(document, fos); document.open(); document.addCreationDate(); document.addCreator(Environment.getInstance().getProject()); document.addAuthor(System.getProperty("user.name")); for (i = 0; i < files.length; i++) { if (isLoggingEnabled()) getLogger().info("Adding file: " + files[i]); reader = new PdfReader(files[i].getAbsolutePath()); // loop over the pages in that document pages = reader.getNumberOfPages(); if (isLoggingEnabled()) getLogger().info("- #pages: " + pages); for (n = 1; n <= pages; n++) { copy.addPage(copy.getImportedPage(reader, n)); if (isLoggingEnabled()) getLogger().info("- adding page: " + n); } copy.freeReader(reader); } document.close(); } catch (Exception e) { result = handleException("Failed to merge PDF files: ", e); } finally { FileUtils.closeQuietly(fos); } if (result == null) m_OutputToken = new Token(m_Output.getAbsolutePath()); return result; }
From source file:adams.flow.transformer.pdfproclet.PDFGenerator.java
License:Open Source License
/** * Opens the document for appending content. * * @throws Exception if opening fails/* w w w. jav a 2s . c o m*/ */ public void append() throws Exception { PdfReader reader; File tmpFile; int i; PdfImportedPage page; PdfContentByte cb; if (isLoggingEnabled()) getLogger().info("Appending document: " + m_Output); resetState(); tmpFile = TempUtils.createTempFile("adams", ".pdf"); if (tmpFile.exists()) { if (!tmpFile.delete()) throw new IllegalStateException("Failed to delete temporary PDF file '" + tmpFile + "'!"); } if (!FileUtils.copy(m_Output, tmpFile)) throw new IllegalStateException( "Failed to move original PDF file '" + m_Output + "' to '" + tmpFile + "'!"); reader = new PdfReader(tmpFile.getAbsolutePath()); if (reader.getNumberOfPages() > 0) { m_Document = new Document(reader.getPageSize(1)); } else { if (m_PageOrientation == PageOrientation.PORTRAIT) m_Document = new Document(determinePageSize()); else m_Document = new Document(determinePageSize().rotate()); } m_Stream = new FileOutputStream(m_Output.getAbsoluteFile()); m_Writer = PdfWriter.getInstance(m_Document, m_Stream); m_Document.open(); m_Document.addCreationDate(); m_Document.addCreator(Environment.getInstance().getProject()); m_Document.addAuthor(System.getProperty("user.name")); m_State = new DocumentState(); cb = m_Writer.getDirectContent(); for (i = 0; i < reader.getNumberOfPages(); i++) { page = m_Writer.getImportedPage(reader, i + 1); m_Document.newPage(); cb.addTemplate(page, 0, 0); } }