List of usage examples for com.lowagie.text Document rightMargin
public float rightMargin()
From source file:biblivre3.administration.reports.BaseBiblivreReport.java
License:Open Source License
@Override public void onEndPage(PdfWriter writer, Document document) { try {/*from w ww .j a va 2 s. c o m*/ Rectangle page = document.getPageSize(); PdfPTable head = new PdfPTable(1); PdfPCell cell = new PdfPCell(new Paragraph(this.getText("REPORTS_HEADER"))); cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER); cell.setBorder(Rectangle.BOTTOM); head.addCell(cell); head.setTotalWidth((page.width() / 2) - document.leftMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), page.height() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent()); PdfPTable date = new PdfPTable(1); PdfPCell dateCell = new PdfPCell(new Paragraph(dateFormat.format(generationDate))); dateCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); dateCell.setVerticalAlignment(PdfPCell.ALIGN_CENTER); dateCell.setBorder(Rectangle.BOTTOM); date.addCell(dateCell); date.setTotalWidth((page.width() / 2) - document.rightMargin()); date.writeSelectedRows(0, -1, (page.width() / 2), page.height() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent()); PdfPTable foot = new PdfPTable(1); Chunk pageNumber = new Chunk(String.valueOf(document.getPageNumber())); pageNumber.setFont(footerFont); cell = new PdfPCell(new Paragraph(pageNumber)); cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER); cell.setBorder(Rectangle.TOP); foot.addCell(cell); foot.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin()); foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent()); } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:br.org.acessobrasil.silvinha.util.HeaderAndFooter.java
License:Open Source License
/** * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) *//*from w ww.j a v a2s.c om*/ public void onEndPage(PdfWriter writer, Document document) { try { Rectangle page = document.getPageSize(); PdfPTable head = new PdfPTable(1); PdfPCell cell = new PdfPCell(new Paragraph(GERAL.RELATORIOS_ASES)); cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER); cell.setBorder(Rectangle.BOTTOM); head.addCell(cell); head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent()); PdfPTable foot = new PdfPTable(1); cell = new PdfPCell(new Paragraph(String.valueOf(document.getPageNumber()))); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER); cell.setBorder(Rectangle.TOP); foot.addCell(cell); foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent()); } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:ch.gpb.elexis.cst.view.CstResultPart.java
License:Open Source License
private void makeActions(final Control viewer) { actionScreenshot = new Action() { public void run() { if (profile == null) { SWTHelper.alert("No profile", "Ohne Profil kann kein Resultat erzeugt werden"); return; }/* w w w . j a v a 2 s . c o m*/ GC gc = null; Image image = null; try { String latestPath = CoreHub.userCfg.get(CstPreference.CST_IDENTIFIER_LATESTPATH, null); if (latestPath == null) { latestPath = System.getProperty("user.home"); } FileDialog fd = new FileDialog(baseComposite.getShell(), SWT.SAVE); fd.setText("Save"); fd.setFilterPath(latestPath); String[] filterExt = { "*.png", "*.*" }; fd.setFilterExtensions(filterExt); fd.setFileName(CstService.generateFilename(patient)); String selected = fd.open(); if (selected == null) { return; } File selFile = new File(selected); CoreHub.userCfg.set(CstPreference.CST_IDENTIFIER_LATESTPATH, selFile.getParentFile().getAbsolutePath()); //if (profile.getAnzeigeTyp().toLowerCase().equals("effektiv")) { if (profile.getAnzeigeTyp().toLowerCase().equals(CstProfile.ANZEIGETYP_EFFEKTIV)) { if (profile.getAusgabeRichtung()) { image = new Image(viewer.getDisplay(), 1123, viewer.getBounds().height); } else { image = new Image(viewer.getDisplay(), 794, viewer.getBounds().height); } } else { image = new Image(viewer.getDisplay(), 794, viewer.getBounds().height); } ImageLoader loader = new ImageLoader(); gc = new GC(image); viewer.print(gc); gc.dispose(); loader.data = new ImageData[] { image.getImageData() }; loader.save(selected, SWT.IMAGE_PNG); } catch (Exception e) { log.error("Error saving png: " + e.toString()); showMessage("Error while saving PNG", e.getMessage()); } finally { if (image != null) { image.dispose(); } if (gc != null) { gc.dispose(); } } } }; actionScreenshot.setText(Messages.Cst_Text_Save_as_png); actionScreenshot.setToolTipText(Messages.Cst_Text_Save_as_png); actionScreenshot.setImageDescriptor( PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_ELEMENT)); actionScreenshot.setImageDescriptor(Activator.getImageDescriptor(Activator.IMG_PNG_PATH)); // TODO: die pdf ausgabe ist eine ziemliche Baustelle - berarbeiten actionPdf = new Action() { public void run() { ////////////////////////// if (profile == null) { SWTHelper.alert("No profile", "Ohne Profil kann kein Resultat erzeugt werden"); return; } GC gc = null; Image image = null; try { String latestPath = CoreHub.userCfg.get(CstPreference.CST_IDENTIFIER_LATESTPATH, null); if (latestPath == null) { latestPath = System.getProperty("user.home"); } FileDialog fd = new FileDialog(baseComposite.getShell(), SWT.SAVE); fd.setText("Save"); fd.setFilterPath(latestPath); String[] filterExt = { "*.pdf", "*.*" }; fd.setFilterExtensions(filterExt); fd.setFileName(CstService.generateFilename(patient)); String selected = fd.open(); if (selected == null) { return; } File selFile = new File(selected); CoreHub.userCfg.set(CstPreference.CST_IDENTIFIER_LATESTPATH, selFile.getParentFile().getAbsolutePath()); int printHeigth = 1123; int printWidth = 794; if (profile.getAusgabeRichtung()) { printHeigth = 794; printWidth = 1123; } // get the image from the viewport image = new Image(viewer.getDisplay(), printWidth, viewer.getBounds().height); ImageLoader loader = new ImageLoader(); gc = new GC(image); viewer.print(gc); gc.dispose(); // prepare title data //Date date = new Date(); //SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm"); Patient patient = Patient.load(profile.getKontaktId()); //String sTitle = "Gemeinschaftspraxis Brunnmatt Dr. Beat Knzi "; String sTitle; sTitle = profile.getOutputHeader() == null ? "No header configured!" : profile.getOutputHeader(); if (sTitle == null || sTitle.length() == 0) { sTitle = "No header configured!"; } sTitle = sTitle + " Datum: " + CstService.getReadableDateAndTime(); // get option (paging to A4/ in one piece) int pdfOutputOption = 0; boolean onePage = true; PdfOptionsDialog dialog = new PdfOptionsDialog(baseComposite.getShell()); dialog.create(); if (dialog.open() == Window.OK) { pdfOutputOption = dialog.getPdfOutputOption(); if (pdfOutputOption == PdfOptionsDialog.OPTION_ONE_PAGE) { onePage = true; } else { onePage = false; } } float docHeight = viewer.getBounds().height; docHeight = docHeight / 7.5f; float fontSize = 12f; if (docHeight < 360f) { docHeight = 360f; } BufferedImage bimage = ImageUtils.convertToAWT(image.getImageData()); // create an Itextt Image from AWT BufferedImage com.lowagie.text.Image itextImage = null; java.awt.Image awtImage = null; try { awtImage = Toolkit.getDefaultToolkit().createImage(bimage.getSource()); itextImage = com.lowagie.text.Image.getInstance(awtImage, null); } catch (Exception e) { log.error("Error on image loading: " + e.toString()); e.printStackTrace(); } // only for debugging //loader.data = new ImageData[] { image.getImageData() }; //loader.save("C:\\Users\\daniel\\tmp\\debug.png", SWT.IMAGE_PNG); Rectangle pagesize = new Rectangle(595f, itextImage.getHeight() * 0.75f); // is it a4 quer? if (profile.getAusgabeRichtung()) { pagesize = new Rectangle(842f, itextImage.getHeight() * 0.75f); } //System.out.println("pagesize: " + pagesize.toString()); Document document; if (onePage) { document = new Document(pagesize); } else { document = new Document(PageSize.A4); if (profile.getAusgabeRichtung()) { document = new Document(PageSize.A4.rotate()); } } document.addCreationDate(); try { // creation of the different writers PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(selected)); // various fonts BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252", true); BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true); BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252", true); com.lowagie.text.Font fontHelv12 = new com.lowagie.text.Font(bf_helv, fontSize); com.lowagie.text.Font fontTimes = new com.lowagie.text.Font(bf_times, fontSize); fontTimes.setSize(fontSize); fontTimes.setStyle(com.lowagie.text.Font.ITALIC); Chunk chunkHeader = new Chunk(sTitle, FontFactory.getFont(FontFactory.HELVETICA, fontSize, com.lowagie.text.Font.NORMAL, new java.awt.Color(255, 0, 0))); com.lowagie.text.Phrase phraseHeader = new com.lowagie.text.Phrase(chunkHeader); // headers and footers must be added before the document // is opened Chunk chunkFooter = new Chunk("Seite: ", FontFactory.getFont(FontFactory.HELVETICA, fontSize, com.lowagie.text.Font.BOLD, new java.awt.Color(0, 0, 0))); Phrase phraseFooter = new Phrase(chunkFooter); phraseFooter.setFont(fontHelv12); HeaderFooter footer = new HeaderFooter(phraseFooter, true); footer.setBorder(Rectangle.NO_BORDER); footer.setAlignment(Element.ALIGN_CENTER); document.setFooter(footer); Phrase headerPhrase = new Phrase(sTitle); headerPhrase.setFont(fontTimes); HeaderFooter header = new HeaderFooter(phraseHeader, false); header.setBorder(Rectangle.BOTTOM); header.setBorderWidth(0.5f); header.setAlignment(Element.ALIGN_LEFT); document.setHeader(header); document.open(); //System.out.println("itext image w: " + itextImage.getWidth() + " h:" + itextImage.getHeight()); if (onePage) { int scale = 66; itextImage.scalePercent(scale); document.add(itextImage); } else { BufferedImage[] imageChunks = ImageUtils.splitImageByHeigth(bimage, printHeigth); for (int i = 0; i < imageChunks.length; i++) { com.lowagie.text.Image itextImage2 = com.lowagie.text.Image.getInstance( Toolkit.getDefaultToolkit().createImage(imageChunks[i].getSource()), null); // width becomes typically 523 (595 - 72) for a4Hoch or 770 (842 - 72) for A4Quer float imgWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin(); float imgHeigth = itextImage.getHeight() * 0.75f; itextImage2.setAbsolutePosition(30, 20); int scale = 66; itextImage2.scalePercent(scale); document.add(itextImage2); document.newPage(); } } // we're done! document.close(); /////////////////////////////// } catch (Exception ex) { log.error(ex.getMessage()); showMessage("Error while generating PDF", ex.getMessage()); } } finally { if (image != null) { image.dispose(); } if (gc != null) { gc.dispose(); } /* image.dispose(); gc.dispose(); */ } } }; actionPdf.setText(Messages.Cst_Text_Save_as_pdf); actionPdf.setToolTipText(Messages.Cst_Text_Save_as_pdf); /* * actionPdf.setImageDescriptor(PlatformUI.getWorkbench() .getSharedImages() * .getImageDescriptor(ISharedImages.IMG_OBJ_FILE)); */ actionPdf.setImageDescriptor(Activator.getImageDescriptor(Activator.IMG_PDF_PATH)); }
From source file:com.aryjr.nheengatu.pdf.PDFPageBreak.java
License:Open Source License
public PDFPageBreak(final PdfWriter writer, final Document document, final Tag headFirstPageTag, final Tag footFirstPageTag, final Tag headTag, final Tag footTag) throws DocumentException { this.headFirstPageTag = headFirstPageTag; this.footFirstPageTag = footFirstPageTag; this.headTag = headTag; this.footTag = footTag; final Rectangle page = document.getPageSize(); if (headTag != null) { final PDFTable pdft = createTable(writer, document, headTag); pdft.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); headHeight = pdft.getTotalHeight(); }/*from www . jav a2 s . co m*/ if (footTag != null) { final PDFTable pdft = createTable(writer, document, footTag); pdft.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); footHeight = pdft.getTotalHeight(); } if (headFirstPageTag != null) { final PDFTable pdft = createTable(writer, document, headFirstPageTag); pdft.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); headFirstPageHeight = pdft.getTotalHeight(); } if (footFirstPageTag != null) { final PDFTable pdft = createTable(writer, document, footFirstPageTag); pdft.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); footFirstPageHeight = pdft.getTotalHeight(); } topMargin = document.topMargin(); bottomMargin = document.bottomMargin(); document.setMargins(document.leftMargin(), document.rightMargin(), topMargin + headFirstPageHeight, bottomMargin + footFirstPageHeight); }
From source file:com.aryjr.nheengatu.pdf.PDFPageBreak.java
License:Open Source License
@Override public void onEndPage(final PdfWriter writer, final Document document) { try {//from w w w .ja v a2 s. c o m final Rectangle page = document.getPageSize(); PDFTable head = null; PDFTable foot = null; if (writer.getPageNumber() == 1) { if (headFirstPageTag != null) head = createTable(writer, document, headFirstPageTag); if (footFirstPageTag != null) foot = createTable(writer, document, footFirstPageTag); } else { if (headTag != null) head = createTable(writer, document, headTag); if (footTag != null) foot = createTable(writer, document, footTag); } if (head != null) { head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - topMargin, writer.getDirectContent()); } if (foot != null) { foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); foot.writeSelectedRows(0, -1, document.leftMargin(), bottomMargin + foot.getTotalHeight(), writer.getDirectContent()); } } catch (final Exception e) { throw new ExceptionConverter(e); } document.setMargins(document.leftMargin(), document.rightMargin(), topMargin + headHeight, bottomMargin + footHeight); }
From source file:com.krawler.spring.exportFuctionality.ExportinvController.java
License:Open Source License
private static void addHeaderFooter(Document document, PdfWriter writer) throws DocumentException, ServiceException { fontSmallRegular.setColor(Color.BLACK); java.util.Date dt = new java.util.Date(); String date = "yyyy-MM-dd"; java.text.SimpleDateFormat dtf = new java.text.SimpleDateFormat(date); String DateStr = dtf.format(dt); PdfPTable footer = new PdfPTable(1); PdfPCell footerSeparator = new PdfPCell(new Phrase("")); footerSeparator.setBorder(PdfPCell.BOX); footerSeparator.setPadding(0);/* w w w . ja v a2 s . com*/ footerSeparator.setColspan(3); footer.addCell(footerSeparator); String PageDate = DateStr; PdfPCell pagerDateCell = new PdfPCell(new Phrase("Generated Date : " + PageDate, fontSmallRegular)); pagerDateCell.setBorder(0); pagerDateCell.setHorizontalAlignment(PdfCell.ALIGN_CENTER); footer.addCell(pagerDateCell); // -------- footer end ----------- try { Rectangle page = document.getPageSize(); footer.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); footer.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent()); } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:CPS.Core.TODOLists.PDFExporter.java
License:Open Source License
private Document prepareDocument(String filename, final String title, final String author, final String creator, final Rectangle pageSize) { System.out.println("DEBUG(PDFExporter): Creating document: " + filename); Document d = new Document(); d.setPageSize(pageSize);// w w w . jav a2 s . c o m // TODO alter page orientation? maybe useful for seed order worksheet d.addTitle(title); d.addAuthor(author); // d.addSubject( ); // d.addKeywords( ); d.addCreator(creator); // left, right, top, bottom - scale in points (~72 points/inch) d.setMargins(35, 35, 35, 44); try { PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(filename)); // add header and footer writer.setPageEvent(new PdfPageEventHelper() { public void onEndPage(PdfWriter writer, Document document) { try { Rectangle page = document.getPageSize(); PdfPTable head = new PdfPTable(3); head.getDefaultCell().setBorderWidth(0); head.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT); head.addCell(new Phrase(author, fontHeadFootItal)); head.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER); head.addCell(new Phrase(title, fontHeadFootReg)); head.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); head.addCell(""); head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent()); PdfPTable foot = new PdfPTable(3); foot.getDefaultCell().setBorderWidth(0); foot.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT); foot.addCell(new Phrase(creator, fontHeadFootItal)); foot.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER); foot.addCell(""); foot.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); foot.addCell(new Phrase("Page " + document.getPageNumber(), fontHeadFootReg)); foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent()); } catch (Exception e) { throw new ExceptionConverter(e); } } }); } catch (Exception e) { e.printStackTrace(); } return d; }
From source file:cz.incad.kramerius.rest.api.k5.client.pdf.PDFResource.java
License:Open Source License
private static StreamingOutput streamingOutput(final File file, final String format) { return new StreamingOutput() { public void write(OutputStream output) throws IOException, WebApplicationException { try { Rectangle formatRect = formatRect(format); Document document = new Document(formatRect); PdfWriter.getInstance(document, output); document.open();//from ww w . j av a 2s . c o m Image image = Image.getInstance(file.toURI().toURL()); image.scaleToFit( document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin(), document.getPageSize().getHeight() - document.topMargin() - document.bottomMargin()); document.add(image); document.close(); } catch (Exception e) { throw new WebApplicationException(e); } finally { if (file != null) file.delete(); } } }; }
From source file:jdbreport.model.io.pdf.itext2.PdfWriter.java
License:Apache License
private int saveSheet(Document document, int listCount, ReportModel model) throws DocumentException, IOException, SaveReportException { model.updatePages(0);//from w w w . j a va2s .c o m ReportPage pageFormat = model.getReportPage(); Paper paper = pageFormat.getPaper(); Rectangle pageSize = new Rectangle(Math.round((float) paper.getWidth()), Math.round((float) paper.getHeight())); if (pageFormat.getOrientation() == ReportPage.LANDSCAPE) { pageSize = pageSize.rotate(); } document.setPageSize(pageSize); document.setMargins(Math.round((float) pageFormat.getLeftMargin(Units.PT)), Math.round((float) pageFormat.getRightMargin(Units.PT)), Math.round((float) pageFormat.getTopMargin(Units.PT)), Math.round((float) pageFormat.getBottomMargin(Units.PT))); Rectangle viewPageSize = new Rectangle( document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin(), document.getPageSize().getHeight() - document.topMargin() - document.bottomMargin()); if (listCount > 0) { document.newPage(); } listCount++; //float columnMargin = (float) Units.PT.setXPixels(model.getColumnModel().getColumnMargin()); ReportPrintable printable = new ReportPrintable(model); int pageCount = printable.calcCountPage(model.getReportPage()); Map<Integer, PageClip> clips = printable.getPageClips(); int leftCol; int topRow; int rightCol; int bottomRow; for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) { PageClip pageClip = clips.get(pageIndex); if (pageClip == null) break; leftCol = pageClip.getLeftCol(); rightCol = pageClip.getRightCol(); topRow = pageClip.getTopRow(); bottomRow = pageClip.getBottomRow(); int columnCount = rightCol - leftCol; float[] widths = new float[columnCount + 1]; widths[0] = 1; for (int c = 0; c < columnCount; c++) { ReportColumn column = (ReportColumn) model.getColumnModel().getColumn(leftCol + c); widths[c + 1] = (float) Math.round(column.getNativeWidth()); } PdfPTable table = createPdfTable(columnCount + 1, widths, viewPageSize); fillTable(model, leftCol, topRow, rightCol, bottomRow, table); document.add(table); if (pageIndex < clips.size()) { document.newPage(); } } return listCount; }
From source file:org.activityinfo.server.report.renderer.itext.ItextChartRenderer.java
License:Open Source License
@Override public void render(DocWriter writer, Document doc, PivotChartReportElement element) throws DocumentException { doc.add(ThemeHelper.elementTitle(element.getTitle())); ItextRendererHelper.addFilterDescription(doc, element.getContent().getFilterDescriptions()); ItextRendererHelper.addDateFilterDescription(doc, element.getFilter().getDateRange()); if (element.getContent().getData().isEmpty()) { Paragraph para = new Paragraph(I18N.CONSTANTS.noData()); para.setFont(new Font(Font.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0))); doc.add(para);//from ww w . java2 s . c o m } else { float width = doc.getPageSize().getWidth() - doc.rightMargin() - doc.leftMargin(); float height = (doc.getPageSize().getHeight() - doc.topMargin() - doc.bottomMargin()) / 3f; renderImage(writer, doc, element, width, height); } }