List of usage examples for com.lowagie.text Document close
boolean close
To view the source code for com.lowagie.text Document close.
Click Source Link
From source file:lucee.runtime.text.pdf.PDFUtil.java
License:Open Source License
public static void encrypt(PDFDocument doc, OutputStream os, String newUserPassword, String newOwnerPassword, int permissions, int encryption) throws ApplicationException, DocumentException, IOException { byte[] user = newUserPassword == null ? null : newUserPassword.getBytes(); byte[] owner = newOwnerPassword == null ? null : newOwnerPassword.getBytes(); PdfReader pr = doc.getPdfReader();// www . java2s . c o m List bookmarks = SimpleBookmark.getBookmark(pr); int n = pr.getNumberOfPages(); Document document = new Document(pr.getPageSizeWithRotation(1)); PdfCopy writer = new PdfCopy(document, os); if (encryption != ENCRYPT_NONE) writer.setEncryption(user, owner, permissions, encryption); document.open(); PdfImportedPage page; for (int i = 1; i <= n; i++) { page = writer.getImportedPage(pr, i); writer.addPage(page); } PRAcroForm form = pr.getAcroForm(); if (form != null) writer.copyAcroForm(pr); if (bookmarks != null) writer.setOutlines(bookmarks); document.close(); }
From source file:managedbean.aas.reportController.java
public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) { Document document = new Document(); try {//from w w w. ja v a 2 s . c o m List<InputStream> pdfs = streamOfPDFFiles; List<PdfReader> readers = new ArrayList<PdfReader>(); int totalPages = 0; Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { InputStream pdf = iteratorPDFs.next(); PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF // data PdfImportedPage page; int currentPageNumber = 0; int pageOfCurrentReaderPDF = 0; Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; currentPageNumber++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); // Code for pagination. if (paginate) { cb.beginText(); cb.setFontAndSize(bf, 9); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0); cb.endText(); } } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (document.isOpen()) { document.close(); } try { if (outputStream != null) { outputStream.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } } }
From source file:mitm.common.pdf.MessagePDFBuilder.java
License:Open Source License
public void buildPDF(MimeMessage message, String replyURL, OutputStream pdfStream) throws DocumentException, MessagingException, IOException { Document document = createDocument(); PdfWriter pdfWriter = createPdfWriter(document, pdfStream); document.open();/*from www.j a v a2 s . com*/ String[] froms = null; try { froms = EmailAddressUtils.addressesToStrings(message.getFrom(), true /* mime decode */); } catch (MessagingException e) { logger.warn("From address is not a valid email address."); } if (froms != null) { for (String from : froms) { document.addAuthor(from); } } String subject = null; try { subject = message.getSubject(); } catch (MessagingException e) { logger.error("Error getting subject.", e); } if (subject != null) { document.addSubject(subject); document.addTitle(subject); } String[] tos = null; try { tos = EmailAddressUtils.addressesToStrings(message.getRecipients(RecipientType.TO), true /* mime decode */); } catch (MessagingException e) { logger.warn("To is not a valid email address."); } String[] ccs = null; try { ccs = EmailAddressUtils.addressesToStrings(message.getRecipients(RecipientType.CC), true /* mime decode */); } catch (MessagingException e) { logger.warn("CC is not a valid email address."); } Date sentDate = null; try { sentDate = message.getSentDate(); } catch (MessagingException e) { logger.error("Error getting sent date.", e); } Collection<Part> attachments = new LinkedList<Part>(); String body = BodyPartUtils.getPlainBodyAndAttachments(message, attachments); attachments = preprocessAttachments(attachments); if (body == null) { body = MISSING_BODY; } /* * PDF does not have tab support so we convert tabs to spaces */ body = StringReplaceUtils.replaceTabsWithSpaces(body, tabWidth); PdfPTable headerTable = new PdfPTable(2); headerTable.setHorizontalAlignment(Element.ALIGN_LEFT); headerTable.setWidthPercentage(100); headerTable.setWidths(new int[] { 1, 6 }); headerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER); Font headerFont = createHeaderFont(); FontSelector headerFontSelector = createHeaderFontSelector(); PdfPCell cell = new PdfPCell(new Paragraph("From:", headerFont)); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); headerTable.addCell(cell); String decodedFroms = StringUtils.defaultString(StringUtils.join(froms, ", ")); headerTable.addCell(headerFontSelector.process(decodedFroms)); cell = new PdfPCell(new Paragraph("To:", headerFont)); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); headerTable.addCell(cell); headerTable.addCell(headerFontSelector.process(StringUtils.defaultString(StringUtils.join(tos, ", ")))); cell = new PdfPCell(new Paragraph("CC:", headerFont)); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); headerTable.addCell(cell); headerTable.addCell(headerFontSelector.process(StringUtils.defaultString(StringUtils.join(ccs, ", ")))); cell = new PdfPCell(new Paragraph("Subject:", headerFont)); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); headerTable.addCell(cell); headerTable.addCell(headerFontSelector.process(StringUtils.defaultString(subject))); cell = new PdfPCell(new Paragraph("Date:", headerFont)); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); headerTable.addCell(cell); headerTable.addCell(ObjectUtils.toString(sentDate)); cell = new PdfPCell(new Paragraph("Attachments:", headerFont)); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); headerTable.addCell(cell); headerTable .addCell(headerFontSelector.process(StringUtils.defaultString(getAttachmentHeader(attachments)))); document.add(headerTable); if (replyURL != null) { addReplyLink(document, replyURL); } /* * Body table will contain the body of the message */ PdfPTable bodyTable = new PdfPTable(1); bodyTable.setWidthPercentage(100f); bodyTable.setSplitLate(false); bodyTable.setSpacingBefore(15f); bodyTable.setHorizontalAlignment(Element.ALIGN_LEFT); addBodyAndAttachments(pdfWriter, document, bodyTable, body, attachments); Phrase footer = new Phrase(FOOTER_TEXT); PdfContentByte cb = pdfWriter.getDirectContent(); ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, footer, document.right(), document.bottom(), 0); document.close(); }
From source file:model.relatorio.java
public void getRelatorioTodosAlunos() throws DocumentException, FileNotFoundException, IOException { Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("Relatorio Alunos.pdf")); doc.open();//from ww w . java 2 s . c o m Image imagem = Image.getInstance(caminhoImagemRelatorio); imagem.scaleToFit(550, 100); doc.add(imagem); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" RELATORIO ALUNOS")); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); PreparedStatement stmt = null; Connection conn = null; try { conn = ConexaoBanco.getAbreConexao(); stmt = conn.prepareStatement("select * from aluno"); ResultSet rs = stmt.executeQuery(); while (rs.next()) { doc.add(new Paragraph("ID: " + rs.getInt("id_aluno") + " Nome : " + rs.getString("nome") + "\nCpf : " + rs.getString("cpf") + " Endereo : " + rs.getString("endereco") + "\nTelefone : " + rs.getString("telefone") + " Idade : " + rs.getInt("idade") + " Altura : " + rs.getString("altura") + " Peso : " + rs.getString("peso"))); doc.add(new Paragraph(" ")); doc.add(new Paragraph( "_____________________________________________________________________________")); doc.add(new Paragraph(" ")); } } catch (SQLException | HeadlessException e) { JOptionPane.showMessageDialog(null, " Erro \n" + e); } doc.close(); JOptionPane.showMessageDialog(null, " Arquivo Gerado com Sucesso! "); Desktop.getDesktop().open(new File("Relatorio Alunos.pdf")); }
From source file:model.relatorio.java
public void getRelatorioTodosProfessores() throws DocumentException, FileNotFoundException, IOException { Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("Relatorio Professores.pdf")); doc.open();/*from w w w .j a v a 2s.co m*/ Image imagem = Image.getInstance(caminhoImagemRelatorio); imagem.scaleToFit(550, 100); doc.add(imagem); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" RELATORIO PROFESSORES")); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); PreparedStatement stmt = null; Connection conn = null; try { conn = ConexaoBanco.getAbreConexao(); stmt = conn.prepareStatement("select * from professor"); ResultSet rs = stmt.executeQuery(); while (rs.next()) { doc.add(new Paragraph("ID: " + rs.getInt("id_prof") + " Nome : " + rs.getString("nome") + "\nCpf : " + rs.getString("cpf") + " Endereo : " + rs.getString("endereco") + "\nTelefone : " + rs.getString("telefone") + " Salario : " + rs.getDouble("salario"))); doc.add(new Paragraph(" ")); doc.add(new Paragraph( "_____________________________________________________________________________")); doc.add(new Paragraph(" ")); } } catch (SQLException | HeadlessException e) { JOptionPane.showMessageDialog(null, " Erro \n" + e); } doc.close(); JOptionPane.showMessageDialog(null, " Arquivo Gerado com Sucesso! "); Desktop.getDesktop().open(new File("Relatorio Professores.pdf")); }
From source file:model.relatorio.java
public void getRelatorioTodasAulas() throws DocumentException, FileNotFoundException, IOException { Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("Relatorio Aulas.pdf")); doc.open();//from w w w . j av a2 s . c om Image imagem = Image.getInstance(caminhoImagemRelatorio); imagem.scaleToFit(550, 100); doc.add(imagem); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" RELATORIO AULAS")); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); PreparedStatement stmt = null; Connection conn = null; try { conn = ConexaoBanco.getAbreConexao(); stmt = conn.prepareStatement("select * from aula"); ResultSet rs = stmt.executeQuery(); while (rs.next()) { doc.add(new Paragraph("ID: " + rs.getInt("id_aula") + " Nome : " + rs.getString("nome") + "\nPreo : " + rs.getDouble("preco") + " Horario : " + rs.getString("horario"))); doc.add(new Paragraph(" ")); doc.add(new Paragraph( "_____________________________________________________________________________")); doc.add(new Paragraph(" ")); } } catch (SQLException | HeadlessException e) { JOptionPane.showMessageDialog(null, " Erro \n" + e); } doc.close(); JOptionPane.showMessageDialog(null, " Arquivo Gerado com Sucesso! "); Desktop.getDesktop().open(new File("Relatorio Aulas.pdf")); }
From source file:model.relatorio.java
public void getRelatorioAula(String idParaProcurar) throws DocumentException, FileNotFoundException, IOException { Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("Relatorio Aula ID " + idParaProcurar + ".pdf")); doc.open();/*from w w w. j a v a 2s .c o m*/ Image imagem = Image.getInstance(caminhoImagemRelatorio); imagem.scaleToFit(550, 100); doc.add(imagem); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" RELATORIO AULA")); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); PreparedStatement stmt = null; Connection conn = null; try { conn = ConexaoBanco.getAbreConexao(); stmt = conn.prepareStatement("select * from aula where id_aula = " + idParaProcurar); ResultSet rs = stmt.executeQuery(); while (rs.next()) { doc.add(new Paragraph("ID: " + rs.getInt("id_aula") + " Nome : " + rs.getString("nome") + " Preo : " + rs.getDouble("preco") + " Horario : " + rs.getString("horario"))); } } catch (SQLException | HeadlessException e) { JOptionPane.showMessageDialog(null, " Erro \n" + e); } doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ALUNOS MATRICULADOS:")); try { conn = ConexaoBanco.getAbreConexao(); stmt = conn.prepareStatement("select aluno.id_aluno, aluno.nome from aluno\n" + "inner join matricula\n" + "on aluno.id_aluno = matricula.id_aluno\n" + "inner join aula\n" + "on aula.id_aula = matricula.id_aula\n" + " where aula.id_aula =" + idParaProcurar); ResultSet rs = stmt.executeQuery(); while (rs.next()) { doc.add(new Paragraph("\nID: " + rs.getInt("id_aluno") + " | Nome : " + rs.getString("nome"))); } doc.close(); JOptionPane.showMessageDialog(null, " Arquivo Gerado com Sucesso! "); Desktop.getDesktop().open(new File("Relatorio Aula ID " + idParaProcurar + ".pdf")); } catch (SQLException ex) { Logger.getLogger(relatorio.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:model.relatorio.java
public void getRelatorioAluno(String idParaProcurar) throws DocumentException, FileNotFoundException, IOException { double totalAulas = 0; Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("Relatorio Aluno ID " + idParaProcurar + ".pdf")); doc.open();/*from w ww .j av a2s. co m*/ Image imagem = Image.getInstance(caminhoImagemRelatorio); imagem.scaleToFit(550, 100); doc.add(imagem); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" RELATORIO ALUNO")); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); PreparedStatement stmt = null; Connection conn = null; try { conn = ConexaoBanco.getAbreConexao(); stmt = conn.prepareStatement("select * from aluno where id_aluno = " + idParaProcurar); ResultSet rs = stmt.executeQuery(); while (rs.next()) { doc.add(new Paragraph("ID: " + rs.getInt("id_aluno") + " Nome : " + rs.getString("nome") + "\nCpf : " + rs.getString("cpf") + " Endereo : " + rs.getString("endereco") + "\nTelefone : " + rs.getString("telefone") + " Idade : " + rs.getInt("idade") + " Altura : " + rs.getString("altura") + " Peso : " + rs.getString("peso"))); } } catch (SQLException | HeadlessException e) { JOptionPane.showMessageDialog(null, " Erro \n" + e); } doc.add(new Paragraph(" ")); doc.add(new Paragraph(" AULAS MATRICULADAS")); try { conn = ConexaoBanco.getAbreConexao(); stmt = conn.prepareStatement("select matricula.id_matricula, aula.nome from aluno\n" + "inner join matricula\n" + "on aluno.id_aluno = matricula.id_aluno\n" + "inner join aula\n" + "on aula.id_aula = matricula.id_aula\n" + "where aluno.id_aluno =" + idParaProcurar); ResultSet rt = stmt.executeQuery(); while (rt.next()) { doc.add(new Paragraph( "ID: " + rt.getInt("id_matricula") + " | Nome : " + rt.getString("nome") + "\n")); } } catch (SQLException | HeadlessException e) { JOptionPane.showMessageDialog(null, " Erro \n" + e); } try { conn = ConexaoBanco.getAbreConexao(); stmt = conn.prepareStatement("select sum(aula.preco) Valor from aluno\n" + "inner join matricula\n" + "on aluno.id_aluno = matricula.id_aluno\n" + "inner join aula\n" + "on aula.id_aula = matricula.id_aula\n" + "where aluno.id_aluno =" + idParaProcurar); ResultSet rt = stmt.executeQuery(); while (rt.next()) { totalAulas += (rt.getDouble("valor")); } } catch (SQLException | HeadlessException e) { JOptionPane.showMessageDialog(null, " Erro \n" + e); } doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); doc.add(new Paragraph("MENSALIDADE: " + totalAulas)); doc.close(); JOptionPane.showMessageDialog(null, " Arquivo Gerado com Sucesso! "); Desktop.getDesktop().open(new File("Relatorio Aluno ID " + idParaProcurar + ".pdf")); }
From source file:model.relatorio.java
public void getRelatorioProfessor(String idParaProcurar) throws DocumentException, FileNotFoundException, IOException { Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("Relatorio Professor ID " + idParaProcurar + ".pdf")); doc.open();//w w w . j a v a 2 s . c o m Image imagem = Image.getInstance(caminhoImagemRelatorio); imagem.scaleToFit(550, 100); doc.add(imagem); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" RELATORIO PROFESSOR")); doc.add(new Paragraph(" ")); doc.add(new Paragraph(" ")); PreparedStatement stmt = null; Connection conn = null; try { conn = ConexaoBanco.getAbreConexao(); stmt = conn.prepareStatement("select * from professor where id_prof = " + idParaProcurar); ResultSet rs = stmt.executeQuery(); while (rs.next()) { doc.add(new Paragraph("ID: " + rs.getInt("id_prof") + " Nome : " + rs.getString("nome") + "\nCpf : " + rs.getString("cpf") + " Endereo : " + rs.getString("endereco") + "\nTelefone : " + rs.getString("telefone") + " Salario : " + rs.getDouble("salario"))); } } catch (SQLException | HeadlessException e) { JOptionPane.showMessageDialog(null, " Erro \n" + e); } doc.add(new Paragraph(" ")); doc.add(new Paragraph(" AULAS: ")); try { conn = ConexaoBanco.getAbreConexao(); stmt = conn.prepareStatement("select aula.horario, aula.nome from professor\n" + "inner join aula\n" + "on aula.id_prof = professor.id_prof\n" + "where professor.id_prof = " + idParaProcurar); ResultSet rt = stmt.executeQuery(); while (rt.next()) { doc.add(new Paragraph( "Horario: " + rt.getString("horario") + " | Nome : " + rt.getString("nome") + "\n")); } } catch (SQLException | HeadlessException e) { JOptionPane.showMessageDialog(null, " Erro \n" + e); } doc.close(); JOptionPane.showMessageDialog(null, " Arquivo Gerado com Sucesso! "); Desktop.getDesktop().open(new File("Relatorio Professor ID " + idParaProcurar + ".pdf")); }