List of usage examples for com.itextpdf.text Document open
boolean open
To view the source code for com.itextpdf.text Document open.
Click Source Link
From source file:com.pearson.controller.PdfGentrate.java
public static void main(String[] args) { try {/* w w w.java2 s .c o m*/ /*Document document = new Document(PageSize. A4, 50, 50, 50, 50); PdfWriter writer = PdfWriter.getInstance (document, new FileOutputStream("C:\\my.pdf")); document.open(); // create a chunk object using chunk class of itext library. Chunk underlined = new Chunk("Welcome to Pearson Q-Service Portal : "); // set the distance between text and line. underlined.setTextRise(8.0f); // set the width of the line, 'y' position, color and design of the line underlined.setUnderline(new Color(0x00, 0x00, 0xFF),0.0f, 0.2f, 3.0f, 0.0f, PdfContentByte.LINE_CAP_PROJECTING_SQUARE); // finally add object to the document. document.add(underlined); document.add(new Paragraph("Hi username", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200))));; document.add(new Paragraph("Tiltle",catFont)); document.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ smallBold)); document.add(new Paragraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).", redFont)); PdfPTable table = new PdfPTable(3); // 3 columns. table.setWidthPercentage(100); //Width 100% table.setSpacingBefore(10f); //Space before table table.setSpacingAfter(10f); //Space after table //Set Column widths float[] columnWidths = {1f, 1f, 1f}; table.setWidths(columnWidths); PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1")); cell1.setBorderColor(BaseColor.BLUE); cell1.setPaddingLeft(10); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2")); cell2.setBorderColor(BaseColor.GREEN); cell2.setPaddingLeft(10); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3")); cell3.setBorderColor(BaseColor.RED); cell3.setPaddingLeft(10); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); //To avoid having the cell border and the content overlap, if you are having thick cell borders //cell1.setUserBorderPadding(true); //cell2.setUserBorderPadding(true); //cell3.setUserBorderPadding(true); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); document.add(table); document.close(); writer.close(); document.close(); } catch (Exception e2) { System.out.println(e2.getMessage()); } }*/ Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("AddTableExample.pdf")); document.open(); PdfPTable table = new PdfPTable(3); // 3 columns. table.setWidthPercentage(100); //Width 100% table.setSpacingBefore(10f); //Space before table table.setSpacingAfter(10f); //Space after table //Set Column widths float[] columnWidths = { 1f, 1f, 1f }; table.setWidths(columnWidths); PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1")); cell1.setBorderColor(BaseColor.BLUE); cell1.setPaddingLeft(10); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2")); cell2.setBorderColor(BaseColor.GREEN); cell2.setPaddingLeft(10); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3")); cell3.setBorderColor(BaseColor.RED); cell3.setPaddingLeft(10); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); //To avoid having the cell border and the content overlap, if you are having thick cell borders //cell1.setUserBorderPadding(true); //cell2.setUserBorderPadding(true); //cell3.setUserBorderPadding(true); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); document.add(table); document.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.photon.phresco.framework.docs.impl.DocConvertor.java
License:Apache License
/** * @param fileUrl/* w w w. jav a 2s . c o m*/ * @return * @throws FileNotFoundException * @throws IOException * @throws DocumentException */ private static PdfInput convertPdf(String fileUrl) throws IOException, DocumentException { if (isDebugEnabled) { S_LOGGER.debug("Entering Method DocConvertor.convertPdf(String fileUrl)"); } PdfReader reader = new PdfReader(new FileInputStream(fileUrl)); int numberOfPages = reader.getNumberOfPages(); Document doc = new Document(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfCopy copy = new PdfCopy(doc, os); doc.open(); //page number in PDF starts at 1 for (int i = 1; i <= numberOfPages; i++) { copy.addPage(copy.getImportedPage(reader, i)); } doc.close(); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); os.close(); PdfInput input = new PdfInput(); input.setInputStream(new FileInputStream(fileUrl)); return input; }
From source file:com.photon.phresco.framework.docs.impl.DocumentUtil.java
License:Apache License
/** * Adds title section.//from ww w.j ava2 s .c om * @param info the project info object * @return PDF input stream * @throws PhrescoException */ public static InputStream getTitleSection(ApplicationInfo info) throws PhrescoException { if (isDebugEnabled) { S_LOGGER.debug(" Entering Method DocumentUtil.getTitleSection(ProjectInfo info)"); } if (isDebugEnabled) { S_LOGGER.debug("getTitleSection() projectCode=" + info.getCode()); } try { //create output stream com.itextpdf.text.Document docu = new com.itextpdf.text.Document(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfWriter.getInstance(docu, os); docu.open(); //add standard title section with supplied info object Paragraph paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_CENTER); paragraph.setFont(DocConstants.TITLE_FONT); addBlankLines(paragraph, MAGICNUMBER.DOCLINES); paragraph.add(info.getName()); addBlankLines(paragraph, MAGICNUMBER.BLANKLINESFOUR); docu.add(paragraph); paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_CENTER); addBlankLines(paragraph, MAGICNUMBER.DOCLINES); String techName = info.getTechInfo().getName(); if (StringUtils.isNotEmpty(info.getTechInfo().getVersion())) { paragraph.add(techName + " - " + info.getTechInfo().getVersion()); } else { paragraph.add(techName); } docu.add(paragraph); paragraph = new Paragraph(); addBlankLines(paragraph, MAGICNUMBER.DOCLINES); paragraph.setAlignment(Element.ALIGN_CENTER); paragraph.add(DocumentMessages.getString("Documents.version.name") + getVersion(info)); //$NON-NLS-1$ addBlankLines(paragraph, MAGICNUMBER.BLANKLINESSEVEN); docu.add(paragraph); if (StringUtils.isNotEmpty(info.getDescription())) { paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setFont(DocConstants.DESC_FONT); paragraph.setFirstLineIndent(MAGICNUMBER.BLANKLINESEIGHT); docu.add(paragraph); } docu.close(); //Create an inputstream to return. return new ByteArrayInputStream(os.toByteArray()); } catch (DocumentException e) { e.printStackTrace(); throw new PhrescoException(e); } }
From source file:com.photon.phresco.framework.docs.impl.DocumentUtil.java
License:Apache License
/** * Creates and returns PDF input stream for the supplied string. * @param string to be printed in the PDF * @return PDF input stream.// ww w .j a va2 s . c o m * @throws PhrescoException */ public static InputStream getStringAsPDF(String string) throws PhrescoException { if (isDebugEnabled) { S_LOGGER.debug("Entering Method DocumentUtil.getStringAsPDF(String string)"); } try { com.itextpdf.text.Document docu = new com.itextpdf.text.Document(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfWriter.getInstance(docu, os); docu.open(); Paragraph paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_LEFT); paragraph.setFirstLineIndent(MAGICNUMBER.INDENTLINE); paragraph.add("\n"); //$NON-NLS-1$ paragraph.add(string); paragraph.add("\n\n"); //$NON-NLS-1$ docu.add(paragraph); docu.close(); //Create an inputstream to return. return new ByteArrayInputStream(os.toByteArray()); } catch (DocumentException e) { e.printStackTrace(); throw new PhrescoException(e); } }
From source file:com.photon.phresco.framework.docs.impl.DocumentUtil.java
License:Apache License
/** * Process tuple beans to generate Documnets for a speific entity type. * @param dependencyManager dependency manager * @param modules list of tuple beans// w w w. ja v a 2s . c om * @param type Entity type * @return PDF input stream. * @throws PhrescoException */ public static InputStream getDocumentStream(List<ArtifactGroup> modules) throws PhrescoException { if (isDebugEnabled) { S_LOGGER.debug( "Entering Method DocumentUtil.getDocumentStream(RepositoryManager repoManager,List<TupleBean> modules, EntityType type)"); } try { if (CollectionUtils.isNotEmpty(modules)) { com.itextpdf.text.Document docu = new com.itextpdf.text.Document(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(docu, os); docu.open(); List<ArtifactGroup> coreModules = new ArrayList<ArtifactGroup>(); List<ArtifactGroup> externalModules = new ArrayList<ArtifactGroup>(); List<ArtifactGroup> jsLibraries = new ArrayList<ArtifactGroup>(); List<ArtifactGroup> components = new ArrayList<ArtifactGroup>(); for (ArtifactGroup artifactGroup : modules) { if (artifactGroup.getType().name().equals(Type.FEATURE.name())) { if (artifactGroup.getAppliesTo().get(0).isCore() == true) { coreModules.add(artifactGroup); } else { externalModules.add(artifactGroup); } } else if (artifactGroup.getType().name().equals(Type.JAVASCRIPT.name())) { jsLibraries.add(artifactGroup); } else if (artifactGroup.getType().name().equals(Type.COMPONENT.name())) { components.add(artifactGroup); } } if (CollectionUtils.isNotEmpty(coreModules)) { updateDoc(coreModules, docu, writer, coreModule); } if (CollectionUtils.isNotEmpty(externalModules)) { updateDoc(externalModules, docu, writer, externalModule); } if (CollectionUtils.isNotEmpty(jsLibraries)) { updateDoc(jsLibraries, docu, writer, "JsLibraries"); } if (CollectionUtils.isNotEmpty(components)) { updateDoc(components, docu, writer, "Components"); } docu.close(); return new ByteArrayInputStream(os.toByteArray()); } } catch (DocumentException e) { e.printStackTrace(); throw new PhrescoException(e); } return null; }
From source file:com.photon.phresco.framework.impl.DocumentGeneratorImpl.java
License:Apache License
@Override public void generate(ApplicationInfo info, File filePath, List<ArtifactGroup> artifacts, ServiceManager serviceManager) throws PhrescoException { if (isDebugEnabled) { S_LOGGER.debug("Entering Method DocumentGeneratorImpl.generate(ProjectInfo info, File filePath)"); }/*from w w w . j a v a 2 s. c om*/ OutputStream os = null, fos = null; try { if (isDebugEnabled) { S_LOGGER.debug("generate() Filepath=" + filePath.getPath()); } String folderPath = filePath.toString() + File.separator + "docs"; File docsFolder = new File(folderPath); if (!docsFolder.exists()) { docsFolder.mkdirs(); } if (isDebugEnabled) { S_LOGGER.debug("generate() ProjectCode=" + info.getCode()); } String path = folderPath + File.separator + info.getAppDirName() + "_doc.pdf"; os = new FileOutputStream(new File(path)); com.itextpdf.text.Document docu = new com.itextpdf.text.Document(); PdfCopy pdfCopy = new PdfCopy(docu, os); docu.open(); InputStream titleSection = DocumentUtil.getTitleSection(info); DocumentUtil.addPages(titleSection, pdfCopy); String techId = info.getTechInfo().getId(); Technology technology = serviceManager.getTechnology(techId); if (StringUtils.isNotEmpty(technology.getDescription())) { InputStream stringAsPDF = DocumentUtil.getStringAsPDF(technology.getDescription()); DocumentUtil.addPages(stringAsPDF, pdfCopy); } // if (StringUtils.isNotEmpty(technology.getDescription())) { // PdfInput convertToPdf = DocConvertor.convertToPdf(technology // .getDescription()); // if (convertToPdf != null) { // DocumentUtil.addPages(convertToPdf.getInputStream(), // pdfCopy); // } // } else { // } if (CollectionUtils.isNotEmpty(artifacts)) { for (ArtifactGroup artifactGroup : artifacts) { ArtifactElement artifactDescription = serviceManager .getArtifactDescription(artifactGroup.getId()); if (artifactDescription != null) { artifactGroup.setDescription(artifactDescription.getDescription()); } } DocumentUtil.addPages(artifacts, pdfCopy); } docu.close(); //generate index.html String indexHtml = DocumentUtil.getIndexHtml(docsFolder); File indexPath = new File(docsFolder, "index.html"); fos = new FileOutputStream(indexPath); fos.write(indexHtml.getBytes()); } catch (IOException e) { e.printStackTrace(); if (isDebugEnabled) { S_LOGGER.debug("(The process cannot access the file because it is being used by another process"); } throw new PhrescoException(e); } catch (com.itextpdf.text.DocumentException e) { e.printStackTrace(); if (isDebugEnabled) { S_LOGGER.debug("(The process cannot access the file because it is being used by another process"); } throw new PhrescoException(e); } finally { Utility.closeStream(os); Utility.closeStream(fos); } }
From source file:com.photon.phresco.service.docs.impl.DocumentUtil.java
License:Apache License
/** * Adds title section./* w ww .ja va2 s . c om*/ * @param info the project info object * @return PDF input stream * @throws DocumentException */ public static InputStream getTitleSection(ProjectInfo info) throws DocumentException { if (isDebugEnabled) { S_LOGGER.debug(" Entering Method DocumentUtil.getTitleSection(ProjectInfo info)"); } if (isDebugEnabled) { S_LOGGER.debug("getTitleSection() projectCode=" + info.getCode()); } //create output stream com.itextpdf.text.Document docu = new com.itextpdf.text.Document(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfWriter.getInstance(docu, os); docu.open(); //add standard title section with supplied info object Paragraph paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_CENTER); paragraph.setFont(DocConstants.TITLE_FONT); addBlankLines(paragraph, 10); paragraph.add(info.getName()); addBlankLines(paragraph, 4); docu.add(paragraph); paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_CENTER); addBlankLines(paragraph, 10); String techName = info.getTechnology().getName(); if (info.getTechnology().getVersions() != null) { paragraph.add(techName + " - " + info.getTechnology().getVersions().get(0)); } else { paragraph.add(techName); } docu.add(paragraph); paragraph = new Paragraph(); addBlankLines(paragraph, 10); paragraph.setAlignment(Element.ALIGN_CENTER); paragraph.add(DocumentMessages.getString("Documents.version.name") + getVersion(info)); //$NON-NLS-1$ addBlankLines(paragraph, 7); docu.add(paragraph); paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_RIGHT); paragraph.setFont(DocConstants.DESC_FONT); paragraph.setFirstLineIndent(8); paragraph.add(info.getDescription()); docu.add(paragraph); docu.close(); //Create an inputstream to return. return new ByteArrayInputStream(os.toByteArray()); }
From source file:com.photon.phresco.service.docs.impl.DocumentUtil.java
License:Apache License
/** * Creates and returns PDF input stream for the supplied string. * @param string to be printed in the PDF * @return PDF input stream./*from ww w . j a v a2 s .c o m*/ * @throws DocumentException */ public static InputStream getStringAsPDF(String string) throws DocumentException { if (isDebugEnabled) { S_LOGGER.debug("Entering Method DocumentUtil.getStringAsPDF(String string)"); } com.itextpdf.text.Document docu = new com.itextpdf.text.Document(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfWriter.getInstance(docu, os); docu.open(); Paragraph paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_LEFT); paragraph.setFirstLineIndent(180); paragraph.add("\n"); //$NON-NLS-1$ paragraph.add(string); paragraph.add("\n\n"); //$NON-NLS-1$ docu.add(paragraph); docu.close(); //Create an inputstream to return. return new ByteArrayInputStream(os.toByteArray()); }
From source file:com.photon.phresco.service.docs.impl.DocumentUtil.java
License:Apache License
/** * Process tuple beans to generate Documnets for a speific entity type. * @param dependencyManager dependency manager * @param modules list of tuple beans/* w w w . j a v a 2 s .c om*/ * @param type Entity type * @return PDF input stream. * @throws PhrescoException * @throws DocumentException * @throws IOException */ public static InputStream getDocumentStream(List<ModuleGroup> modules, String moduleType) throws PhrescoException, DocumentException, IOException { if (isDebugEnabled) { S_LOGGER.debug( "Entering Method DocumentUtil.getDocumentStream(RepositoryManager repoManager,List<TupleBean> modules, EntityType type)"); } if (modules != null && !modules.isEmpty()) { com.itextpdf.text.Document docu = new com.itextpdf.text.Document(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(docu, os); docu.open(); if (moduleType.equals("Modules")) { List<ModuleGroup> coreModules = new ArrayList<ModuleGroup>(); List<ModuleGroup> externalModules = new ArrayList<ModuleGroup>(); for (ModuleGroup moduleGroup : modules) { if (moduleGroup.isCore()) { coreModules.add(moduleGroup); } if (!moduleGroup.isCore()) { externalModules.add(moduleGroup); } } if (coreModules != null && CollectionUtils.isNotEmpty(coreModules) && moduleType.equals("Modules")) { updateDoc(coreModules, docu, writer, coreModule); } if (externalModules != null && CollectionUtils.isNotEmpty(externalModules) && moduleType.equals("Modules")) { updateDoc(externalModules, docu, writer, externalModule); } } if (moduleType.equals("JsLibraries")) { updateDoc(modules, docu, writer, moduleType); } docu.close(); return new ByteArrayInputStream(os.toByteArray()); } return null; }
From source file:com.photon.phresco.service.impl.DocumentGeneratorImpl.java
License:Apache License
@Override public void generate(ProjectInfo info, File filePath) throws PhrescoException { if (isDebugEnabled) { S_LOGGER.debug("Entering Method DocumentGeneratorImpl.generate(ProjectInfo info, File filePath)"); }/* w ww . ja v a2 s . c om*/ OutputStream os = null, fos = null; try { if (isDebugEnabled) { S_LOGGER.debug("generate() Filepath=" + filePath.getPath()); } String folderPath = filePath.toString() + File.separator + "docs"; File docsFolder = new File(folderPath); if (!docsFolder.exists()) { docsFolder.mkdirs(); } if (isDebugEnabled) { S_LOGGER.debug("generate() ProjectCode=" + info.getCode()); } String path = folderPath + File.separator + info.getName() + "_doc.pdf"; os = new FileOutputStream(new File(path)); com.itextpdf.text.Document docu = new com.itextpdf.text.Document(); PdfCopy pdfCopy = new PdfCopy(docu, os); docu.open(); InputStream titleSection = DocumentUtil.getTitleSection(info); DocumentUtil.addPages(titleSection, pdfCopy); Technology technology = PhrescoServerFactory.getDbManager() .getTechnologyDoc(info.getTechnology().getId()); List<Documentation> technologyDoc = technology.getDocs(); if (technologyDoc != null) { for (Documentation documentation : technologyDoc) { if (!StringUtils.isEmpty(documentation.getUrl())) { PdfInput convertToPdf = DocConvertor.convertToPdf(documentation.getUrl()); if (convertToPdf != null) { DocumentUtil.addPages(convertToPdf.getInputStream(), pdfCopy); } } else { InputStream stringAsPDF = DocumentUtil.getStringAsPDF(documentation.getContent()); DocumentUtil.addPages(stringAsPDF, pdfCopy); } } } // Documents documentInfo = repoManager.getDocument(technology.getId(), EntityType.TECHNOLOGY); // if(documentInfo!= null){ // List<Document> docs = documentInfo.getDocument(); // for (Document document : docs) { // if(!StringUtils.isEmpty(document.getUrl())){ // PdfInput convertToPdf = DocConvertor.convertToPdf(document.getUrl()); // if(convertToPdf != null) { // DocumentUtil.addPages(convertToPdf.getInputStream(), pdfCopy); // } // } else { // InputStream stringAsPDF = DocumentUtil.getStringAsPDF(document.getContent()); // DocumentUtil.addPages(stringAsPDF, pdfCopy); // } // } // } List<ModuleGroup> tuples = technology.getModules(); DocumentUtil.addPages(tuples, pdfCopy, MODULES); List<ModuleGroup> libraries = technology.getJsLibraries(); DocumentUtil.addPages(libraries, pdfCopy, LIB); //TODO: need to do for App servers, databases if required. docu.close(); //generate index.html String indexHtml = DocumentUtil.getIndexHtml(docsFolder); File indexPath = new File(docsFolder, "index.html"); fos = new FileOutputStream(indexPath); fos.write(indexHtml.getBytes()); } catch (IOException e) { if (isDebugEnabled) { S_LOGGER.debug("(The process cannot access the file because it is being used by another process"); } throw new PhrescoException(e); } catch (com.itextpdf.text.DocumentException e) { if (isDebugEnabled) { S_LOGGER.debug("(The process cannot access the file because it is being used by another process"); } throw new PhrescoException(e); } finally { Utility.closeStream(os); Utility.closeStream(fos); } }