List of usage examples for org.apache.pdfbox.pdmodel PDDocument save
public void save(OutputStream output) throws IOException
From source file:org.nuxeo.pdf.service.PDFTransformationServiceImpl.java
License:Apache License
protected FileBlob saveInTempFile(PDDocument PdfDoc) throws IOException, COSVisitorException { File tempFile = Framework.createTempFile("nuxeo-pdfutils-", ".pdf"); PdfDoc.save(tempFile); return new FileBlob(tempFile, MIME_TYPE, tempFile.getName()); }
From source file:org.nuxeo.typeDocPkg.TestPdfBoxN.java
License:Apache License
public void create(String message, String outfile) throws IOException, COSVisitorException { PDDocument doc = null; try {/* ww w .j a va2s . c o m*/ doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(doc, page); PDFont font = PDType1Font.HELVETICA; contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(100, 700); contentStream.drawString(message); contentStream.endText(); contentStream.close(); doc.save(outfile); doc.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.olat.course.certificate.manager.CertificatePDFFormWorker.java
License:Apache License
public File fill(CertificateTemplate template, File destinationDir, String certificateFilename) { PDDocument document = null; InputStream templateStream = null; try {/*from w ww. j a v a 2s . c o m*/ File templateFile = null; if (template != null) { templateFile = certificatesManager.getTemplateFile(template); } if (templateFile != null && templateFile.exists()) { templateStream = new FileInputStream(templateFile); } else { templateStream = CertificatesManager.class.getResourceAsStream("template.pdf"); } document = PDDocument.load(templateStream); PDDocumentCatalog docCatalog = document.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); if (acroForm != null) { fillUserProperties(acroForm); fillRepositoryEntry(acroForm); fillCertificationInfos(acroForm); fillAssessmentInfos(acroForm); } if (!destinationDir.exists()) { destinationDir.mkdirs(); } File certificateFile = new File(destinationDir, certificateFilename); OutputStream out = new FileOutputStream(certificateFile); document.save(out); out.flush(); out.close(); return certificateFile; } catch (Exception e) { log.error("", e); return null; } finally { IOUtils.closeQuietly(document); IOUtils.closeQuietly(templateStream); } }
From source file:org.olat.course.certificate.ui.UploadCertificateController.java
License:Apache License
private boolean validatePdf(File template) { boolean allOk = true; PDDocument document = null; try (InputStream in = Files.newInputStream(template.toPath())) { document = PDDocument.load(in);//w ww .j av a 2 s . co m if (document.isEncrypted()) { fileEl.setErrorKey("upload.error.encrypted", null); allOk &= false; } else { //check if we can write the form PDDocumentCatalog docCatalog = document.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); if (acroForm != null) { @SuppressWarnings("unchecked") List<PDField> fields = acroForm.getFields(); for (PDField field : fields) { field.setValue("test"); } } document.save(new DevNullOutputStream()); } } catch (IOException ex) { logError("", ex); if (ex.getMessage() != null && ex.getMessage().contains("Don't know how to calculate the position for non-simple fonts")) { fileEl.setErrorKey("upload.error.simplefonts", null); } else { fileEl.setErrorKey("upload.unkown.error", null); } allOk &= false; } catch (Exception ex) { logError("", ex); fileEl.setErrorKey("upload.unkown.error", null); allOk &= false; } finally { IOUtils.closeQuietly(document); } return allOk; }
From source file:org.oscarehr.document.web.SplitDocumentAction.java
License:Open Source License
public ActionForward split(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String docNum = request.getParameter("document"); String[] commands = request.getParameterValues("page[]"); Document doc = documentDAO.getDocument(docNum); // String docdownload = oscar.OscarProperties.getInstance().getProperty("DOCUMENT_DIR"); // new File(docdownload); String docdownload = EDocUtil.getDocumentDir(doc.getDocfilename()); String newFilename = doc.getDocfilename(); String dbPrefix = EDocUtil.getDocumentPrefix(doc.getDocfilename()); // FileInputStream input = new FileInputStream(docdownload + doc.getDocfilename()); FileInputStream input = new FileInputStream(EDocUtil.getDocumentPath(newFilename)); PDFParser parser = new PDFParser(input); parser.parse();// w w w . ja v a2 s . co m PDDocument pdf = parser.getPDDocument(); PDDocument newPdf = new PDDocument(); List pages = pdf.getDocumentCatalog().getAllPages(); if (commands != null) { for (String c : commands) { String[] command = c.split(","); int pageNum = Integer.parseInt(command[0]); int rotation = Integer.parseInt(command[1]); PDPage p = (PDPage) pages.get(pageNum - 1); p.setRotation(rotation); newPdf.addPage(p); } } //newPdf.save(docdownload + newFilename); if (newPdf.getNumberOfPages() > 0) { LoggedInInfo loggedInInfo = LoggedInInfo.loggedInInfo.get(); // EDoc newDoc = new EDoc("","", newFilename, "", loggedInInfo.loggedInProvider.getProviderNo(), doc.getDoccreator(), "", 'A', oscar.util.UtilDateUtilities.getToday("yyyy-MM-dd"), "", "", "demographic", "-1",0); EDoc newDoc = new EDoc("", "", EDocUtil.getDocumentFileName(newFilename), "", loggedInInfo.loggedInProvider.getProviderNo(), doc.getDoccreator(), "", 'A', oscar.util.UtilDateUtilities.getToday("yyyy-MM-dd"), "", "", "demographic", "-1", 0); newDoc.setFileName(dbPrefix + '.' + newDoc.getFileName()); newDoc.setDocPublic("0"); newDoc.setContentType("application/pdf"); newDoc.setNumberOfPages(newPdf.getNumberOfPages()); String newDocNo = EDocUtil.addDocumentSQL(newDoc); // //newPdf.save(docdownload + newDoc.getFileName()); System.gc(); //avoid Windows lock on channel newPdf.save(docdownload + EDocUtil.getDocumentFileName(newDoc.getFileName())); newPdf.close(); WebApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(request.getSession().getServletContext()); ProviderInboxRoutingDao providerInboxRoutingDao = (ProviderInboxRoutingDao) ctx .getBean("providerInboxRoutingDAO"); providerInboxRoutingDao.addToProviderInbox("0", newDocNo, "DOC"); List<ProviderInboxItem> routeList = providerInboxRoutingDao.getProvidersWithRoutingForDocument("DOC", docNum); for (ProviderInboxItem i : routeList) { providerInboxRoutingDao.addToProviderInbox(i.getProviderNo(), newDocNo, "DOC"); } providerInboxRoutingDao.addToProviderInbox(loggedInInfo.loggedInProvider.getProviderNo(), newDocNo, "DOC"); QueueDocumentLinkDao queueDocumentLinkDAO = (QueueDocumentLinkDao) ctx.getBean("queueDocumentLinkDAO"); Integer qid = 1; Integer did = Integer.parseInt(newDocNo.trim()); queueDocumentLinkDAO.addToQueueDocumentLink(qid, did); ProviderLabRoutingDao providerLabRoutingDao = (ProviderLabRoutingDao) SpringUtils .getBean("providerLabRoutingDao"); List<ProviderLabRoutingModel> result = providerLabRoutingDao.getProviderLabRoutingDocuments(docNum); if (!result.isEmpty()) { new ProviderLabRouting().route(newDocNo, result.get(0).getProviderNo(), "DOC"); } PatientLabRoutingDao patientLabRoutingDao = (PatientLabRoutingDao) SpringUtils .getBean("patientLabRoutingDao"); List<PatientLabRouting> result2 = patientLabRoutingDao.findDocByDemographic(docNum); if (!result2.isEmpty()) { PatientLabRouting newPatientRoute = new PatientLabRouting(); newPatientRoute.setDemographicNo(result2.get(0).getDemographicNo()); newPatientRoute.setLabNo(Integer.parseInt(newDocNo)); newPatientRoute.setLabType("DOC"); patientLabRoutingDao.persist(newPatientRoute); } DocumentDAO documentDao = (DocumentDAO) SpringUtils.getBean("documentDAO"); CtlDocument result3 = documentDao.getCtrlDocument(Integer.parseInt(docNum)); if (result3 != null) { CtlDocumentPK ctlDocumentPK = new CtlDocumentPK(Integer.parseInt(newDocNo), "demographic"); CtlDocument newCtlDocument = new CtlDocument(ctlDocumentPK, result3.getModuleId()); newCtlDocument.setStatus(result3.getStatus()); documentDao.saveCtlDocument(newCtlDocument); } } pdf.close(); input.close(); return mapping.findForward("success"); }
From source file:org.oscarehr.document.web.SplitDocumentAction.java
License:Open Source License
public ActionForward rotate180(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Document doc = documentDAO.getDocument(request.getParameter("document")); // String docdownload = oscar.OscarProperties.getInstance().getProperty("DOCUMENT_DIR"); String docdownload = EDocUtil.getDocumentPath(doc.getDocfilename()); if (doc.getContenttype().equals("application/pdf")) { FileInputStream input = null; PDDocument pdf = null; try {//w w w . j av a 2 s .com // FileInputStream input = new FileInputStream(docdownload + doc.getDocfilename()); input = new FileInputStream(docdownload); PDFParser parser = new PDFParser(input); parser.parse(); pdf = parser.getPDDocument(); int x = 1; for (Object p : pdf.getDocumentCatalog().getAllPages()) { PDPage pg = (PDPage) p; Integer r = (pg.getRotation() != null ? pg.getRotation() : 0); pg.setRotation((r + 180) % 360); ManageDocumentAction.deleteCacheVersion(doc, x); x++; } // pdf.save(docdownload + doc.getDocfilename()); pdf.save(docdownload); } finally { if (pdf != null) pdf.close(); input.close(); } } else if (doc.getContenttype().equals("image/jpg") || doc.getContenttype().equals("image/png") || doc.getContenttype().equals("image/gif")) { String documentDir = EDocUtil.getDocumentDir(doc.getDocfilename()); File file = new File(documentDir + doc.getDocfilename()); BufferedImage image = ImageIO.read(file); if (image == null) return null; BufferedImage rotatedImage = new BufferedImage(image.getHeight(), image.getWidth(), BufferedImage.TYPE_INT_ARGB); String suffix = null; String contentType = doc.getContenttype(); if (contentType.equalsIgnoreCase("image/jpg") || contentType.equalsIgnoreCase("image/jpeg")) { suffix = "jpg"; } else if (contentType.equalsIgnoreCase("image/png")) { suffix = "png"; } else if (contentType.equalsIgnoreCase("image/gif")) { suffix = "gif"; } AffineTransform tx = AffineTransform.getScaleInstance(1, -1); tx = AffineTransform.getScaleInstance(-1, -1); tx.translate(-image.getWidth(null), -image.getHeight(null)); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); image = op.filter(image, null); ImageIO.write(image, suffix, file); } else { //umknown type - does nothing } return null; }
From source file:org.oscarehr.document.web.SplitDocumentAction.java
License:Open Source License
public ActionForward rotate90(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Document doc = documentDAO.getDocument(request.getParameter("document")); FileInputStream input = new FileInputStream(EDocUtil.getDocumentPath(doc.getDocfilename())); PDFParser parser = new PDFParser(input); parser.parse();//from w ww . ja va 2 s .c om PDDocument pdf = parser.getPDDocument(); int x = 1; for (Object p : pdf.getDocumentCatalog().getAllPages()) { PDPage pg = (PDPage) p; Integer r = (pg.getRotation() != null ? pg.getRotation() : 0); pg.setRotation((r + 90) % 360); ManageDocumentAction.deleteCacheVersion(doc, x); x++; } pdf.save(EDocUtil.getDocumentPath(doc.getDocfilename())); pdf.close(); input.close(); return null; }
From source file:org.oscarehr.document.web.SplitDocumentAction.java
License:Open Source License
public ActionForward removeFirstPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Document doc = documentDAO.getDocument(request.getParameter("document")); // String docdownload = oscar.OscarProperties.getInstance().getProperty("DOCUMENT_DIR"); String docdownload = EDocUtil.getDocumentPath(doc.getDocfilename()); // FileInputStream input = new FileInputStream(docdownload + doc.getDocfilename()); FileInputStream input = new FileInputStream(docdownload); PDFParser parser = new PDFParser(input); parser.parse();//from ww w. j av a 2s.c om PDDocument pdf = parser.getPDDocument(); // Documents must have at least 2 pages, for the first page to be removed. if (pdf.getNumberOfPages() <= 1) { return null; } int x = 1; for (Object p : pdf.getDocumentCatalog().getAllPages()) { ManageDocumentAction.deleteCacheVersion(doc, x); x++; } pdf.removePage(0); EDocUtil.subtractOnePage(request.getParameter("document")); // pdf.save(docdownload + doc.getDocfilename()); System.gc(); //avoid Win channel lock problem pdf.save(docdownload); pdf.close(); input.close(); return null; }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public List<String> split(final String inputUri, final String outputUri, final List<Integer> pages) throws IOException, COSVisitorException { final List<String> result = new ArrayList<String>(); if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && CollectionUtils.isNotEmpty(pages)) { final PDDocument doc = PDDocument.load(inputUri); final List<PDDocument> splittedDocs = new ArrayList<PDDocument>(); @SuppressWarnings("unchecked") final List<PDPage> pagesList = doc.getDocumentCatalog().getAllPages(); // This section creates a new document for each split // indicated into the list, except the last one. Integer currentPage = 0;/*from w ww . j a va2s .c o m*/ for (final Integer page : pages) { final PDDocument document = new PDDocument(); for (Integer i = currentPage; i <= page - 2; i++) { document.addPage(pagesList.get(i)); } splittedDocs.add(document); currentPage = page - 1; document.close(); } // This section splits the last document final PDDocument lastDocument = new PDDocument(); for (Integer i = currentPage; i < pagesList.size(); i++) { lastDocument.addPage(pagesList.get(i)); } splittedDocs.add(lastDocument); lastDocument.close(); Integer subIndex = 1; for (final PDDocument document : splittedDocs) { final String extension = this.converterUtils.addSubIndexBeforeExtension(outputUri, subIndex++); document.save(extension); result.add(extension); } doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } return result; }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public List<String> split(final String inputUri, final String outputUri, final Integer pages) throws IOException, COSVisitorException { final List<String> result = new ArrayList<String>(); if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && pages != null) { final PDDocument doc = PDDocument.load(inputUri); final Splitter splitter = new Splitter(); splitter.setSplitAtPage(pages);//from w w w .ja v a2 s .c o m final List<PDDocument> splittedDocs = splitter.split(doc); Integer subIndex = 1; for (final PDDocument document : splittedDocs) { final String extension = this.converterUtils.addSubIndexBeforeExtension(outputUri, subIndex++); document.save(extension); result.add(extension); document.close(); } doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } return result; }