List of usage examples for com.lowagie.text Paragraph setSpacingBefore
public void setSpacingBefore(float spacing)
From source file:com.concursive.connect.web.modules.wiki.utils.WikiPDFUtils.java
License:Open Source License
private static boolean parseContent(WikiPDFContext context, Wiki wiki, String content, Document document, PdfPCell cell, Connection db, ArrayList<Integer> wikiListTodo, ArrayList<Integer> wikiListDone, float cellWidth) throws Exception { LOG.debug("PARSING CONTENT: " + content); // Parse the wiki page int lastIndent = 0; boolean preTag = false; boolean pre = false; boolean code = false; boolean header = true; try {//from w w w . j av a 2 s. c o m BufferedReader in = new BufferedReader(new StringReader(content)); String line = null; ArrayList unorderedParents = null; List thisList = null; Paragraph codeParagraph = null; while ((line = in.readLine()) != null) { // Tables if (line.startsWith("|")) { // @todo Close all ordered lists, unordered lists, and paragraphs // Parse the table line = parseTable(context, wiki, line, document, db, wikiListTodo, wikiListDone, in); if (line == null) { continue; } } // Forms if (line.startsWith("[{form")) { // @todo close any lists or paragraphs // parseForm operates over all the lines that make up the form, // it will have to look forward so it returns an unparsed line parseForm(context, db, in, line, document, cell); continue; } // Handle code blocks // @todo chunk the content similar to WikiToHTMLUtils otherwise inaccurate if (line.startsWith("<pre>") || line.startsWith("<code>")) { if (!code && !pre) { if (line.startsWith("<pre>")) { preTag = true; pre = true; } else if (line.startsWith("<code>")) { code = true; } codeParagraph = new Paragraph("", codeFont); codeParagraph.setSpacingBefore(10); if (pre && line.length() > ("<pre>").length()) { int endOfLine = line.length(); if (line.endsWith("</pre>")) { endOfLine = line.indexOf("</pre>"); } // This line has some extra content that needs to be added codeParagraph.add(new Chunk( line.substring(line.indexOf("<pre>") + 5, endOfLine) + Chunk.NEWLINE)); } if (code && line.length() > ("<code>").length()) { int endOfLine = line.length(); if (line.endsWith("</code>")) { endOfLine = line.indexOf("</code>"); } // This line has some extra content that needs to be added codeParagraph.add(new Chunk( line.substring(line.indexOf("<code>") + 6, endOfLine) + Chunk.NEWLINE)); } // See if this is a single line block if (preTag && line.endsWith("</pre>")) { // This is a single line block, so finish processing it } else if (code && line.endsWith("</code>")) { // This is a single line block, so finish processing it } else { // There are more lines to process, so do that continue; } } } if (line.startsWith("</code>") || line.endsWith("</code>")) { if (code) { code = false; if (line.indexOf("</code>") > 0 && !line.startsWith("<code>")) { // This line has some extra content that needs to be added codeParagraph .add(new Chunk(line.substring(0, line.indexOf("</code>")) + Chunk.NEWLINE)); } // Draw the final content PdfPTable codeTable = new PdfPTable(1); codeTable.setWidthPercentage(100); codeTable.setSpacingBefore(10); PdfPCell codeCell = new PdfPCell(codeParagraph); codeCell.setPadding(20); codeCell.setBorderColor(new Color(100, 100, 100)); codeCell.setBackgroundColor(new Color(200, 200, 200)); // codeCell.setNoWrap(true); codeTable.addCell(codeCell); LOG.debug("document.add(codeTable)"); document.add(codeTable); continue; } } if (line.startsWith("</pre>") || line.endsWith("</pre>")) { if (pre) { preTag = false; pre = false; if (line.indexOf("</pre>") > 0 && !line.startsWith("<pre>")) { // This line has some extra content that needs to be added codeParagraph.add(new Chunk(line.substring(0, line.indexOf("</pre>")) + Chunk.NEWLINE)); } // Draw the final content PdfPTable codeTable = new PdfPTable(1); codeTable.setWidthPercentage(100); codeTable.setSpacingBefore(10); PdfPCell codeCell = new PdfPCell(codeParagraph); codeCell.setPadding(20); codeCell.setBorderColor(new Color(100, 100, 100)); codeCell.setBackgroundColor(new Color(200, 200, 200)); // codeCell.setNoWrap(true); codeTable.addCell(codeCell); LOG.debug("document.add(codeTable)"); document.add(codeTable); continue; } } if (code || preTag) { // Append the chunk codeParagraph.add(new Chunk(line + Chunk.NEWLINE)); continue; } // Section if (line.startsWith("=") && line.endsWith("=")) { // @todo close any open lists or paragraphs int hCount = parseHCount(line, "="); if (hCount > 6) { hCount = 6; } String section = line.substring(line.indexOf("=") + hCount, line.lastIndexOf("=") - hCount + 1); header = true; context.foundHeader(hCount); String headerAnchor = null; // Store the h2's with anchors for table of contents or index if (hCount == 2) { headerAnchor = StringUtils.toHtmlValue(section).replace(" ", "_"); context.getHeaderAnchors().put(headerAnchor, section); } if (hCount == 3) { Paragraph title = new Paragraph(section.trim(), section2Font); title.setSpacingBefore(10); if (cell != null) { LOG.debug("phrase.add(title)"); cell.addElement(title); } else { LOG.debug("document.add(title)"); document.add(title); } } else if (hCount > 3) { Paragraph title = new Paragraph(section.trim(), section3Font); title.setSpacingBefore(10); if (cell != null) { LOG.debug("phrase.add(title)"); cell.addElement(title); } else { LOG.debug("document.add(title)"); document.add(title); } } else { Paragraph title = new Paragraph(section.trim(), sectionFont); title.setSpacingBefore(10); if (cell != null) { LOG.debug("phrase.add(title)"); cell.addElement(title); } else { LOG.debug("document.add(title)"); document.add(title); } } continue; } if (header) { header = false; if (line.trim().equals("")) { // remove the extra space a user may leave after a header continue; } } // Determine if this is a bulleted list if (line.startsWith("*") || line.startsWith("#")) { // Initialize the list array if (unorderedParents == null) { unorderedParents = new ArrayList(); // if (phrase != null) { // LOG.debug("phrase.add(new Paragraph(Chunk.NEWLINE))"); // phrase.add(new Paragraph(Chunk.NEWLINE)); // } else { // LOG.debug("document.add(new Paragraph(Chunk.NEWLINE))"); // document.add(new Paragraph(Chunk.NEWLINE)); // } } // Get the indent level boolean ol = line.startsWith("#"); int hCount = WikiPDFUtils.parseHCount(line, ol ? "#" : "*"); // Determine a shift in the tree if (lastIndent == 0) { if (ol) { thisList = new List(ol, 20); } else { thisList = new List(ol, 10); thisList.setListSymbol( new Chunk("\u2022", FontFactory.getFont(FontFactory.HELVETICA, 12))); } thisList.setIndentationLeft(36); thisList.setIndentationRight(36); unorderedParents.add(thisList); } else { if (hCount > lastIndent) { if (ol) { thisList = new List(ol, 20); } else { thisList = new List(ol, 10); thisList.setListSymbol( new Chunk("\u2022", FontFactory.getFont(FontFactory.HELVETICA, 12))); } thisList.setIndentationLeft(36); thisList.setIndentationRight(36); ((List) unorderedParents.get(unorderedParents.size() - 1)).add(thisList); unorderedParents.add(thisList); } else if (hCount < lastIndent) { unorderedParents.remove(unorderedParents.size() - 1); thisList = (List) unorderedParents.get(unorderedParents.size() - 1); } } lastIndent = hCount; // Append the item... Paragraph thisItem = new Paragraph(); parseLine(context, line.substring(hCount).trim(), thisItem, db, wikiListTodo, cellWidth, cell); thisList.add(new ListItem(thisItem)); continue; } // List is finished, so append it to the document before working on // other paragraphs if (unorderedParents != null) { if (cell != null) { LOG.debug("phrase.add((List) unorderedParents.get(0))"); cell.addElement((List) unorderedParents.get(0)); } else { LOG.debug("document.add((List) unorderedParents.get(0))"); document.add((List) unorderedParents.get(0)); } unorderedParents = null; thisList = null; lastIndent = 0; } // Otherwise a simple paragraph Paragraph paragraph = new Paragraph(); parseLine(context, line, paragraph, db, wikiListTodo, cellWidth, cell); if (cell != null) { LOG.debug("phrase.add(paragraph)"); if (cell.getHorizontalAlignment() == Element.ALIGN_CENTER) { paragraph.setAlignment(Element.ALIGN_CENTER); } paragraph.setSpacingBefore(5); cell.addElement(paragraph); } else { LOG.debug("document.add(paragraph)"); paragraph.setSpacingBefore(5); document.add(paragraph); } } // Cleanup now that the lines are finished if (pre || code) { PdfPTable codeTable = new PdfPTable(1); codeTable.setWidthPercentage(100); codeTable.setSpacingBefore(10); PdfPCell codeCell = new PdfPCell(codeParagraph); codeCell.setPadding(20); codeCell.setBorderColor(new Color(100, 100, 100)); codeCell.setBackgroundColor(new Color(200, 200, 200)); // codeCell.setNoWrap(true); codeTable.addCell(codeCell); LOG.debug("document.add(codeTable)"); document.add(codeTable); } if (unorderedParents != null) { if (cell != null) { LOG.debug("phrase.add((List) unorderedParents.get(0))"); cell.addElement((List) unorderedParents.get(0)); } else { LOG.debug("document.add((List) unorderedParents.get(0))"); document.add((List) unorderedParents.get(0)); } } in.close(); } catch (Exception e) { LOG.error("parseContent", e); } return true; }
From source file:com.qcadoo.mes.genealogies.print.GenealogyForProductView.java
License:Open Source License
private void addTables(final Document document, final Entity entity, final Locale locale) throws DocumentException { List<String> orderHeader = new ArrayList<String>(); orderHeader.add(translationService.translate("orders.order.number.label", locale)); orderHeader.add(translationService.translate("orders.order.name.label", locale)); orderHeader.add(translationService.translate("orders.order.dateFrom.label", locale)); Paragraph productTitle = new Paragraph(new Phrase( translationService.translate("genealogies.genealogyForProduct.report.paragrah.product", locale), FontUtils.getDejavuBold11Light())); productTitle.setSpacingBefore(20); document.add(productTitle);//from w ww . j ava 2 s .co m PdfPTable headerData = pdfHelper.createPanelTable(3); headerData.setSpacingBefore(7); Entity product = entity.getBelongsToField(ORDER_FIELD).getBelongsToField(PRODUCT_FIELD); pdfHelper.addTableCellAsOneColumnTable(headerData, translationService.translate("basic.product.number.label", locale), product.getField(NUMBER_FIELD)); pdfHelper.addTableCellAsOneColumnTable(headerData, translationService.translate("basic.product.name.label", locale), product.getField(NAME_FIELD)); pdfHelper.addTableCellAsOneColumnTable(headerData, translationService.translate("genealogies.genealogy.batch.label", locale), entity.getField(BATCH_FIELD)); document.add(headerData); Paragraph orderTitle = new Paragraph(new Phrase( translationService.translate("genealogies.genealogyForProduct.report.paragrah.order", locale), FontUtils.getDejavuBold11Light())); orderTitle.setSpacingBefore(20); document.add(orderTitle); List<Entity> orders = getOrders(entity); Collections.sort(orders, new EntityNumberComparator()); addOrderSeries(document, orders, orderHeader); if (pluginManager.isPluginEnabled(GENEALOGIES_FOR_COMPONENTS_PLUGIN)) { addComponentSeries(document, orders, locale); } }
From source file:com.qcadoo.mes.genealogiesForComponents.GenealogyForComponentView.java
License:Open Source License
private void addTables(final Document document, final Entity entity, final Locale locale) throws DocumentException { List<String> orderHeader = new ArrayList<String>(); orderHeader.add(translationService.translate("orders.order.number.label", locale)); orderHeader.add(translationService.translate("orders.order.name.label", locale)); orderHeader.add(translationService.translate("orders.order.product.label", locale)); orderHeader.add(translationService/* w ww . ja va 2 s.co m*/ .translate("genealogiesForComponents.genealogyForComponent.report.productBatch", locale)); Paragraph productTitle = new Paragraph(new Phrase( translationService.translate( "genealogiesForComponents.genealogyForComponent.report.paragrah.product", locale), FontUtils.getDejavuBold11Light())); productTitle.setSpacingBefore(20); document.add(productTitle); PdfPTable headerData = pdfHelper.createPanelTable(3); headerData.setSpacingBefore(7); Entity product = entity.getBelongsToField("productInComponent").getBelongsToField("productInComponent") .getBelongsToField("product"); pdfHelper.addTableCellAsOneColumnTable(headerData, translationService.translate("basic.product.number.label", locale), product.getField("number")); pdfHelper.addTableCellAsOneColumnTable(headerData, translationService.translate("basic.product.name.label", locale), product.getField("name")); pdfHelper.addTableCellAsOneColumnTable(headerData, translationService.translate("genealogiesForComponents.productInBatch.batch.label", locale), entity.getField(BATCH_FIELD)); document.add(headerData); Paragraph orderTitle = new Paragraph( new Phrase( translationService.translate( "genealogiesForComponents.genealogyForComponent.report.paragrah.order", locale), FontUtils.getDejavuBold11Light())); orderTitle.setSpacingBefore(20); document.add(orderTitle); addOrderSeries(document, entity, orderHeader); }
From source file:com.qcadoo.mes.qualityControls.print.QualityControlsReportService.java
License:Open Source License
public final void addQualityControlReportHeader(final Document document, final Map<String, Object> model, final Locale locale) throws DocumentException { if (!model.containsKey(ENTITIES)) { Paragraph firstParagraphTitle = new Paragraph(new Phrase( translationService.translate("qualityControls.qualityControl.report.paragrah", locale), FontUtils.getDejavuBold11Light())); firstParagraphTitle.add(new Phrase(" " + model.get("dateFrom") + " - " + model.get("dateTo"), FontUtils.getDejavuBold11Light())); firstParagraphTitle.setSpacingBefore(20); document.add(firstParagraphTitle); }/*w w w . j a v a 2 s . co m*/ Paragraph secondParagraphTitle = new Paragraph( new Phrase(translationService.translate("qualityControls.qualityControl.report.paragrah2", locale), FontUtils.getDejavuBold11Light())); secondParagraphTitle.setSpacingBefore(20); document.add(secondParagraphTitle); }
From source file:com.qcadoo.report.api.pdf.elements.Headers.java
License:Open Source License
private static Paragraph paragraph(final String text, final Font font, final float marginTop, final float marginBottom) { Paragraph paragraph = new Paragraph(text, font); paragraph.setSpacingBefore(marginTop); paragraph.setSpacingAfter(marginBottom); return paragraph; }
From source file:de.jdufner.sudoku.generator.pdf.PdfPrinterImpl.java
License:Open Source License
private void writeFrontpage(String name, Document document, List<PdfSolution> solutions) throws DocumentException { document.open();// w ww .j av a2 s. c om Paragraph p = new Paragraph(name); p.setAlignment(Element.ALIGN_CENTER); p.setSpacingBefore(20f); p.setSpacingAfter(20f); document.add(p); PdfPTable table = new PdfPTable(17); table.setWidthPercentage(100); int[] width = { 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; table.setWidths(width); PdfPCell cell; table.addCell(buildHeaderCell("Name", 90, true, false)); table.addCell(buildHeaderCell("Schwierigkeitsgrad", 90, false, false)); table.addCell(buildHeaderCell("Besetzte Zellen", 90, false, false)); table.addCell(buildHeaderCell("Simple", 90, false, false)); table.addCell(buildHeaderCell("Hidden Single", 90, false, false)); table.addCell(buildHeaderCell("Naked Pair", 90, false, false)); table.addCell(buildHeaderCell("Naked Triple", 90, false, false)); table.addCell(buildHeaderCell("Naked Quad", 90, false, false)); table.addCell(buildHeaderCell("Hidden Pair", 90, false, false)); table.addCell(buildHeaderCell("Hidden Triple", 90, false, false)); table.addCell(buildHeaderCell("Hidden Quad", 90, false, false)); table.addCell(buildHeaderCell("Intersection Removal", 90, false, false)); table.addCell(buildHeaderCell("Y-Wing", 90, false, false)); table.addCell(buildHeaderCell("X-Wing", 90, false, false)); table.addCell(buildHeaderCell("Jellyfish", 90, false, false)); table.addCell(buildHeaderCell("Swordfish", 90, false, false)); table.addCell(buildHeaderCell("Backtracking", 90, false, true)); boolean even = false; for (PdfSolution solution : solutions) { table.addCell(buildBodyNumberCell(solution.getId(), even, true, false)); table.addCell(buildBodyTextCell(solution.getLevel().toString(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getFixed(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategySimple(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyHiddenSingle(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyNakedPair(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyNakedTriple(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyNakedQuad(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyHiddenPair(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyHiddenTriple(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyHiddenQuad(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyIntersectionRemoval(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyYwing(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyXwing(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyJellyfish(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategySwordfish(), even, false, false)); table.addCell(buildBodyNumberCell(solution.getStrategyBacktracking(), even, false, true)); even = (even ? false : true); } document.add(table); document.close(); }
From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.FastPdfMapper.java
License:Open Source License
@Override protected void visitPicture(CTPicture picture, Float offsetX, org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.STRelFromH.Enum relativeFromH, Float offsetY,//from w w w . j a v a2 s.c o m org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.STRelFromV.Enum relativeFromV, STWrapText.Enum wrapText, IITextContainer pdfParentContainer) throws Exception { CTPositiveSize2D ext = picture.getSpPr().getXfrm().getExt(); long x = ext.getCx(); long y = ext.getCy(); byte[] pictureData = super.getPictureBytes(picture); if (pictureData != null) { try { Image img = Image.getInstance(pictureData); img.scaleAbsolute(emu2points(x), emu2points(y)); IITextContainer parentOfParentContainer = pdfParentContainer.getITextContainer(); if (parentOfParentContainer != null && parentOfParentContainer instanceof PdfPCell) { parentOfParentContainer.addElement(img); } else { float chunkOffsetX = 0; if (offsetX != null) { if (STRelFromH.CHARACTER.equals(relativeFromH)) { chunkOffsetX = offsetX; } else if (STRelFromH.COLUMN.equals(relativeFromH)) { chunkOffsetX = offsetX; } else if (STRelFromH.INSIDE_MARGIN.equals(relativeFromH)) { chunkOffsetX = offsetX; } else if (STRelFromH.LEFT_MARGIN.equals(relativeFromH)) { chunkOffsetX = offsetX; } else if (STRelFromH.MARGIN.equals(relativeFromH)) { chunkOffsetX = pdfDocument.left() + offsetX; } else if (STRelFromH.OUTSIDE_MARGIN.equals(relativeFromH)) { chunkOffsetX = offsetX; } else if (STRelFromH.PAGE.equals(relativeFromH)) { chunkOffsetX = offsetX - pdfDocument.left(); } } float chunkOffsetY = 0; boolean useExtendedImage = false; if (STRelFromV.PARAGRAPH.equals(relativeFromV)) { useExtendedImage = true; } if (useExtendedImage) { ExtendedImage extImg = new ExtendedImage(img, -offsetY); if (STRelFromV.PARAGRAPH.equals(relativeFromV)) { chunkOffsetY = -extImg.getScaledHeight(); } Chunk chunk = new Chunk(extImg, chunkOffsetX, chunkOffsetY, false); pdfParentContainer.addElement(chunk); } /* * float chunkOffsetY = 0; if ( wrapText != null ) { chunkOffsetY = -img.getScaledHeight(); } * boolean useExtendedImage = offsetY != null; // if ( STRelFromV.PARAGRAPH.equals( relativeFromV ) * ) // { // useExtendedImage = true; // } // if ( useExtendedImage ) { float imgY = -offsetY; if ( * pdfHeader != null ) { float headerY = pdfHeader.getY() != null ? pdfHeader.getY() : 0; imgY += - * img.getScaledHeight() + headerY; } ExtendedImage extImg = new ExtendedImage( img, imgY ); // if ( * STRelFromV.PARAGRAPH.equals( relativeFromV ) ) // { // chunkOffsetY = -extImg.getScaledHeight(); * // } Chunk chunk = new Chunk( extImg, chunkOffsetX, chunkOffsetY, false ); * pdfParentContainer.addElement( chunk ); } */ else { if (pdfParentContainer instanceof Paragraph) { // I don't know why but we need add some spacing before in the paragraph // otherwise the image cut the text of the below paragraph (see FormattingTests JUnit)? Paragraph paragraph = (Paragraph) pdfParentContainer; paragraph.setSpacingBefore(paragraph.getSpacingBefore() + 5f); } pdfParentContainer.addElement(new Chunk(img, chunkOffsetX, chunkOffsetY, false)); } } } catch (Exception e) { LOGGER.severe(e.getMessage()); } } }
From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper.java
License:Open Source License
@Override protected void visitPicture(CTPicture picture, Float offsetX, org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.STRelFromH.Enum relativeFromH, Float offsetY,//from w w w . ja v a 2 s . c o m org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.STRelFromV.Enum relativeFromV, STWrapText.Enum wrapText, IITextContainer pdfParentContainer) throws Exception { CTPositiveSize2D ext = picture.getSpPr().getXfrm().getExt(); long x = ext.getCx(); long y = ext.getCy(); XWPFPictureData pictureData = super.getPictureData(picture); if (pictureData != null) { try { Image img = Image.getInstance(pictureData.getData()); img.scaleAbsolute(emu2points(x), emu2points(y)); IITextContainer parentOfParentContainer = pdfParentContainer.getITextContainer(); if (parentOfParentContainer != null && parentOfParentContainer instanceof PdfPCell) { parentOfParentContainer.addElement(img); } else { float chunkOffsetX = 0; if (offsetX != null) { if (STRelFromH.CHARACTER.equals(relativeFromH)) { chunkOffsetX = offsetX; } else if (STRelFromH.COLUMN.equals(relativeFromH)) { chunkOffsetX = offsetX; } else if (STRelFromH.INSIDE_MARGIN.equals(relativeFromH)) { chunkOffsetX = offsetX; } else if (STRelFromH.LEFT_MARGIN.equals(relativeFromH)) { chunkOffsetX = offsetX; } else if (STRelFromH.MARGIN.equals(relativeFromH)) { chunkOffsetX = pdfDocument.left() + offsetX; } else if (STRelFromH.OUTSIDE_MARGIN.equals(relativeFromH)) { chunkOffsetX = offsetX; } else if (STRelFromH.PAGE.equals(relativeFromH)) { chunkOffsetX = offsetX - pdfDocument.left(); } } float chunkOffsetY = 0; boolean useExtendedImage = false; if (STRelFromV.PARAGRAPH.equals(relativeFromV)) { useExtendedImage = true; } if (useExtendedImage) { ExtendedImage extImg = new ExtendedImage(img, -offsetY); if (STRelFromV.PARAGRAPH.equals(relativeFromV)) { chunkOffsetY = -extImg.getScaledHeight(); } Chunk chunk = new Chunk(extImg, chunkOffsetX, chunkOffsetY, false); pdfParentContainer.addElement(chunk); } /* * float chunkOffsetY = 0; if ( wrapText != null ) { * chunkOffsetY = -img.getScaledHeight(); } boolean * useExtendedImage = offsetY != null; // if ( * STRelFromV.PARAGRAPH.equals( relativeFromV ) ) // { // * useExtendedImage = true; // } // if ( useExtendedImage ) * { float imgY = -offsetY; if ( pdfHeader != null ) { float * headerY = pdfHeader.getY() != null ? pdfHeader.getY() : * 0; imgY += - img.getScaledHeight() + headerY; } * ExtendedImage extImg = new ExtendedImage( img, imgY ); // * if ( STRelFromV.PARAGRAPH.equals( relativeFromV ) ) // { * // chunkOffsetY = -extImg.getScaledHeight(); // } Chunk * chunk = new Chunk( extImg, chunkOffsetX, chunkOffsetY, * false ); pdfParentContainer.addElement( chunk ); } */ else { if (pdfParentContainer instanceof Paragraph) { // I don't know why but we need add some spacing // before in the paragraph // otherwise the image cut the text of the below // paragraph (see FormattingTests JUnit)? Paragraph paragraph = (Paragraph) pdfParentContainer; paragraph.setSpacingBefore(paragraph.getSpacingBefore() + 5f); } pdfParentContainer.addElement(new Chunk(img, chunkOffsetX, chunkOffsetY, false)); } } } catch (Exception e) { LOGGER.severe(e.getMessage()); } } }
From source file:fr.paris.lutece.plugins.directory.modules.pdfproducer.utils.PDFUtils.java
License:Open Source License
/** * method to create PDF/*w w w. j a v a 2 s. c om*/ * @param adminUser The admin user * @param locale The locale * @param strNameFile PDF name * @param out OutputStream * @param nIdRecord the id record * @param listIdEntryConfig list of config id entry * @param bExtractNotFilledField if true, extract empty fields, false */ public static void doCreateDocumentPDF(AdminUser adminUser, Locale locale, String strNameFile, OutputStream out, int nIdRecord, List<Integer> listIdEntryConfig, Boolean bExtractNotFilledField) { Document document = new Document(PageSize.A4); Plugin plugin = PluginService.getPlugin(DirectoryPlugin.PLUGIN_NAME); EntryFilter filter; Record record = RecordHome.findByPrimaryKey(nIdRecord, plugin); filter = new EntryFilter(); filter.setIdDirectory(record.getDirectory().getIdDirectory()); filter.setIsGroup(EntryFilter.FILTER_TRUE); List<IEntry> listEntry = DirectoryUtils.getFormEntries(record.getDirectory().getIdDirectory(), plugin, adminUser); int nIdDirectory = record.getDirectory().getIdDirectory(); Directory directory = DirectoryHome.findByPrimaryKey(nIdDirectory, plugin); try { PdfWriter.getInstance(document, out); } catch (DocumentException e) { AppLogService.error(e); } document.open(); if (record.getDateCreation() != null) { SimpleDateFormat monthDayYearformatter = new SimpleDateFormat( AppPropertiesService.getProperty(PROPERTY_POLICE_FORMAT_DATE)); Font fontDate = new Font( DirectoryUtils.convertStringToInt(AppPropertiesService.getProperty(PROPERTY_POLICE_NAME)), DirectoryUtils.convertStringToInt(AppPropertiesService.getProperty(PROPERTY_POLICE_SIZE_DATE)), DirectoryUtils .convertStringToInt(AppPropertiesService.getProperty(PROPERTY_POLICE_STYLE_DATE))); Paragraph paragraphDate = new Paragraph( new Phrase(monthDayYearformatter.format(record.getDateCreation()).toString(), fontDate)); paragraphDate.setAlignment(DirectoryUtils .convertStringToInt(AppPropertiesService.getProperty(PROPERTY_POLICE_ALIGN_DATE))); try { document.add(paragraphDate); } catch (DocumentException e) { AppLogService.error(e); } } Image image; try { image = Image.getInstance(ImageIO.read(new File(AppPathService .getAbsolutePathFromRelativePath(AppPropertiesService.getProperty(PROPERTY_IMAGE_URL)))), null); image.setAlignment( DirectoryUtils.convertStringToInt(AppPropertiesService.getProperty(PROPERTY_IMAGE_ALIGN))); float fitWidth; float fitHeight; try { fitWidth = Float.parseFloat(AppPropertiesService.getProperty(PROPERTY_IMAGE_FITWIDTH)); fitHeight = Float.parseFloat(AppPropertiesService.getProperty(PROPERTY_IMAGE_FITHEIGHT)); } catch (NumberFormatException e) { fitWidth = 100f; fitHeight = 100f; } image.scaleToFit(fitWidth, fitHeight); try { document.add(image); } catch (DocumentException e) { AppLogService.error(e); } } catch (BadElementException e) { AppLogService.error(e); } catch (MalformedURLException e) { AppLogService.error(e); } catch (IOException e) { AppLogService.error(e); } directory.getTitle(); Font fontTitle = new Font( DirectoryUtils.convertStringToInt(AppPropertiesService.getProperty(PROPERTY_POLICE_NAME)), DirectoryUtils .convertStringToInt(AppPropertiesService.getProperty(PROPERTY_POLICE_SIZE_TITLE_DIRECTORY)), DirectoryUtils.convertStringToInt( AppPropertiesService.getProperty(PROPERTY_POLICE_STYLE_TITLE_DIRECTORY))); fontTitle.isUnderlined(); Paragraph paragraphHeader = new Paragraph(new Phrase(directory.getTitle(), fontTitle)); paragraphHeader.setAlignment(Element.ALIGN_CENTER); paragraphHeader.setSpacingBefore(DirectoryUtils.convertStringToInt( AppPropertiesService.getProperty(PROPERTY_POLICE_SPACING_BEFORE_TITLE_DIRECTORY))); paragraphHeader.setSpacingAfter(DirectoryUtils.convertStringToInt( AppPropertiesService.getProperty(PROPERTY_POLICE_SPACING_AFTER_TITLE_DIRECTORY))); try { document.add(paragraphHeader); } catch (DocumentException e) { AppLogService.error(e); } builderPDFWithEntry(document, plugin, nIdRecord, listEntry, listIdEntryConfig, locale, bExtractNotFilledField); document.close(); }
From source file:fr.paris.lutece.plugins.directory.modules.pdfproducer.utils.PDFUtils.java
License:Open Source License
/** * method to builder PDF with directory entry * @param document document pdf/* ww w . java 2 s . c o m*/ * @param plugin plugin * @param nIdRecord id record * @param listEntry list of entry * @param listIdEntryConfig list of config id entry * @param locale the locale * @param bExtractNotFilledField if true, extract empty fields, false */ private static void builderPDFWithEntry(Document document, Plugin plugin, int nIdRecord, List<IEntry> listEntry, List<Integer> listIdEntryConfig, Locale locale, Boolean bExtractNotFilledField) { Map<String, List<RecordField>> mapIdEntryListRecordField = DirectoryUtils .getMapIdEntryListRecordField(listEntry, nIdRecord, plugin); for (IEntry entry : listEntry) { if (entry.getEntryType().getGroup() && (listIdEntryConfig.isEmpty() || listIdEntryConfig.contains(Integer.valueOf(entry.getIdEntry())))) { Font fontEntryTitleGroup = new Font( DirectoryUtils.convertStringToInt(AppPropertiesService.getProperty(PROPERTY_POLICE_NAME)), DirectoryUtils.convertStringToInt( AppPropertiesService.getProperty(PROPERTY_POLICE_SIZE_ENTRY_GROUP)), DirectoryUtils.convertStringToInt( AppPropertiesService.getProperty(PROPERTY_POLICE_STYLE_ENTRY_GROUP))); Paragraph paragraphTitleGroup = new Paragraph(new Phrase(entry.getTitle(), fontEntryTitleGroup)); paragraphTitleGroup.setAlignment(Element.ALIGN_LEFT); paragraphTitleGroup.setIndentationLeft(DirectoryUtils.convertStringToInt( AppPropertiesService.getProperty(PROPERTY_POLICE_MARGIN_LEFT_ENTRY_GROUP))); paragraphTitleGroup.setSpacingBefore(DirectoryUtils.convertStringToInt( AppPropertiesService.getProperty(PROPERTY_POLICE_SPACING_BEFORE_ENTRY_GROUP))); paragraphTitleGroup.setSpacingAfter(DirectoryUtils.convertStringToInt( AppPropertiesService.getProperty(PROPERTY_POLICE_SPACING_AFTER_ENTRY_GROUP))); try { document.add(paragraphTitleGroup); } catch (DocumentException e) { AppLogService.error(e); } if (entry.getChildren() != null) { for (IEntry child : entry.getChildren()) { if (listIdEntryConfig.isEmpty() || listIdEntryConfig.contains(Integer.valueOf(child.getIdEntry()))) { try { builFieldsInPDF(mapIdEntryListRecordField.get(Integer.toString(child.getIdEntry())), document, child, locale, bExtractNotFilledField); } catch (DocumentException e) { AppLogService.error(e); } } } } } else { if (listIdEntryConfig.isEmpty() || listIdEntryConfig.contains(Integer.valueOf(entry.getIdEntry()))) { try { builFieldsInPDF(mapIdEntryListRecordField.get(Integer.toString(entry.getIdEntry())), document, entry, locale, bExtractNotFilledField); } catch (DocumentException e) { AppLogService.error(e); } } } } }