List of usage examples for com.itextpdf.text Document topMargin
public float topMargin()
From source file:com.masscustsoft.service.ToPdf.java
License:Open Source License
private Image getImage(Map row) throws Exception { Image image = null;// w ww . j a va2 s .c o m String url = (String) row.get("url"); if (url != null) { image = Image.getInstance(url); } String externalId = (String) row.get("externalId"); if (externalId != null) { File tmp = File.createTempFile("tmp", ".image"); InputStream is = getFs().getResource(externalId); FileOutputStream os = new FileOutputStream(tmp); StreamUtil.copyStream(is, os, 0); is.close(); os.close(); image = Image.getInstance(tmp.toURL()); ThreadHelper.postponeDelete(tmp); } String img = (String) row.get("image"); if (img != null) { byte[] bytes = LightStr.getHexContent(img); image = Image.getInstance(bytes); } if (image != null) { MapUtil.setIfFloat(row, "spacingAfter", image); MapUtil.setIfFloat(row, "spacingBefore", image); MapUtil.setIfFloat(row, "scalePercent", image); MapUtil.setIfFloat(row, "scaleDegree", image); Float fitPer = MapUtil.getFloat(row, "fitPercent", null); if (fitPer != null) { Document doc = this.getDoc(); image.scaleToFit( (doc.getPageSize().getWidth() - doc.leftMargin() - doc.rightMargin()) * fitPer / 100f, (doc.getPageSize().getHeight() - doc.topMargin() - doc.bottomMargin()) * fitPer / 100f); } Float fitWidth = MapUtil.getFloat(row, "fitWidth", null); if (fitWidth != null) { Float fitHeight = MapUtil.getFloat(row, "fitHeight", fitWidth); image.scaleToFit(fitWidth, fitHeight); } image.setAlignment(getAlignment(row, "alignment")); } return image; }
From source file:com.vectorprint.report.itext.EventHelper.java
License:Open Source License
/** * prints debug info when property debug is true, calls renderHeader and renderFooter and * {@link Advanced#draw(com.itextpdf.text.Rectangle, java.lang.String) } with {@link Document#getPageSize() } * and null for {@link DefaultStylerFactory#PAGESTYLERS}. * * @param writer/*w w w . j av a 2s . c o m*/ * @param document */ @Override public final void onEndPage(PdfWriter writer, Document document) { super.onEndPage(writer, document); sanitize(writer); try { if (failuresHereAfter || debugHereAfter) { PdfContentByte bg = writer.getDirectContentUnder(); Rectangle rect = writer.getPageSize(); rect.setBackgroundColor(itextHelper .fromColor(getSettings().getColorProperty(new Color(240, 240, 240), "legendbackground"))); bg.rectangle(rect); bg.closePathFillStroke(); } else { for (Advanced a : doForAllPages) { try { if (a.shouldDraw(null)) { a.draw(document.getPageSize(), null); } } catch (VectorPrintException ex) { throw new VectorPrintRuntimeException(ex); } } } if (!debugHereAfter && getSettings().getBooleanProperty(false, DEBUG)) { PdfContentByte canvas = writer.getDirectContent(); Rectangle rect = new Rectangle(document.leftMargin(), document.bottomMargin(), document.right() - document.rightMargin(), document.top() - document.topMargin()); DebugHelper.debugRect(canvas, rect, new float[] { 10, 2 }, 0.3f, getSettings(), stylerFactory.getLayerManager()); } renderHeader(writer, document); maxTagForGenericTagOnPage = ((DefaultElementProducer) elementProducer).getAdvancedTag(); if (getSettings().getBooleanProperty(Boolean.FALSE, ReportConstants.PRINTFOOTER)) { renderFooter(writer, document); } else { log.warning("not printing footer, if you want page footers set " + ReportConstants.PRINTFOOTER + " to true"); } maxTagForGenericTagOnPage = Integer.MAX_VALUE; } catch (VectorPrintException | DocumentException | InstantiationException | IllegalAccessException e) { throw new VectorPrintRuntimeException("failed to create the report header or footer: ", e); } }
From source file:eu.aniketos.wp1.ststool.report.pdfgenerator.ReportContentFactory.java
License:Open Source License
/******************************************************************************************/ private void buildAppendixAChapter(Chapter c, Document d) { c.setTitle(getChapterTitleParagraph("Appendix A")); c.setNumberDepth(0);/*ww w . j a v a2 s. c o m*/ c.setTriggerNewPage(true); float width = d.getPageSize().getWidth() - d.leftMargin() - d.rightMargin(); float height = d.getPageSize().getHeight() - d.topMargin() - d.bottomMargin() - 70; if (needSocialDiagramInAppendixA()) { Image socialImage = rsp.generateScrenshot(ViewsManager.SOCIAL_VIEW); socialImage.setAlignment(Element.ALIGN_CENTER); if (socialImage.getPlainWidth() > socialImage.getPlainHeight()) { fitImageInArea(socialImage, height, width, true); socialImage.setRotationDegrees(90); } else { fitImageInArea(socialImage, width, height, true); } c.add(decorateImage(socialImage, ftc.getFigure(FigureConstant.SOCIAL_DIAGRAM) + " - Social View for the " + getProjectName() + " project")); } if (needInformationDiagramInAppendixA()) { Image informationImage = rsp.generateScrenshot(ViewsManager.RESOURCE_VIEW); informationImage.setAlignment(Element.ALIGN_CENTER); if (informationImage.getPlainWidth() > informationImage.getPlainHeight()) { fitImageInArea(informationImage, height, width, true); informationImage.setRotationDegrees(90); } else { fitImageInArea(informationImage, width, height, true); } // c.add(informationImage); c.add(decorateImage(informationImage, ftc.getFigure(FigureConstant.INFORMATION_DIAGRAM) + " - Information View for the " + getProjectName() + " project")); } if (needInformationDiagramInAppendixA()) { Image authorisationImage = rsp.generateScrenshot(ViewsManager.AUTHORIZATION_VIEW); authorisationImage.setAlignment(Element.ALIGN_CENTER); if (authorisationImage.getPlainWidth() > authorisationImage.getPlainHeight()) { fitImageInArea(authorisationImage, height, width, true); authorisationImage.setRotationDegrees(90); } else { fitImageInArea(authorisationImage, width, height, true); } // c.add(authorisationImage); c.add(decorateImage(authorisationImage, ftc.getFigure(FigureConstant.AUTH_DIAGRAM) + " - Authorisation View for the " + getProjectName() + " project")); } c.setComplete(true); }
From source file:net.digitstar.vanadio.AbstractReportBase.java
License:Apache License
protected Rect<Float> customizeMargins(Document document) { return new Rect<Float>(document.topMargin(), document.topMargin(), document.rightMargin(), document.bottomMargin()) {/*from www.j a va 2 s. c o m*/ @Override protected Float zero() { return 0.0f; } }; }
From source file:org.dspace.disseminate.CitationDocument.java
/** * Attempts to add a Logo to the document from the given resource. Returns * true on success and false on failure. * * @param doc The document to add the logo to. (Added to the top right * corner of the first page.//from w ww.jav a 2 s .c o m * @param writer The writer associated with the given Document. * @param res The resource/path to the logo file. This file can be any of * the following formats: * GIF, PNG, JPEG, PDF * * @return Succesfully added logo to document. */ private boolean addLogoToDocument(Document doc, PdfWriter writer, String res) { boolean ret = false; try { //First we try to get the logo as if it is a Java Resource URL logoURL = this.getClass().getResource(res); log.debug(res + " -> " + logoURL.toString()); if (logoURL == null) { logoURL = new URL(res); } if (logoURL != null) { String mtype = URLConnection.guessContentTypeFromStream(logoURL.openStream()); if (mtype == null) { mtype = URLConnection.guessContentTypeFromName(res); } log.debug("Determined MIMETYPE of logo: " + mtype); if (PDF_MIMES.contains(mtype)) { //Handle pdf logos. PdfReader reader = new PdfReader(logoURL); PdfImportedPage logoPage = writer.getImportedPage(reader, 1); Image logo = Image.getInstance(logoPage); float x = doc.getPageSize().getWidth() - doc.rightMargin() - logo.getScaledWidth(); float y = doc.getPageSize().getHeight() - doc.topMargin() - logo.getScaledHeight(); logo.setAbsolutePosition(x, y); doc.add(logo); ret = true; } else if (RASTER_MIMES.contains(mtype)) { //Use iText's Image class Image logo = Image.getInstance(logoURL); //Determine the position of the logo (upper-right corner) and //place it there. float x = doc.getPageSize().getWidth() - doc.rightMargin() - logo.getScaledWidth(); float y = doc.getPageSize().getHeight() - doc.topMargin() - logo.getScaledHeight(); logo.setAbsolutePosition(x, y); writer.getDirectContent().addImage(logo); ret = true; } else if (SVG_MIMES.contains(mtype)) { //Handle SVG Logos log.error("SVG Logos are not supported yet."); } else { //Cannot use other mimetypes log.debug("Logo MIMETYPE is not supported."); } } else { log.debug("Could not create URL to Logo resource: " + res); } } catch (Exception e) { log.error("Could not add logo (" + res + ") to cited document: " + e.getMessage()); ret = false; } return ret; }
From source file:org.ganttproject.impex.htmlpdf.itext.ThemeImpl.java
License:GNU General Public License
private void writeColontitle(String topLeft, String topRight, String bottomLeft, String bottomRight) { final Document document = myDoc; final PdfWriter writer = myWriter; Rectangle page = document.getPageSize(); PdfPTable colontitleTable = createColontitleTable(topLeft, topRight, bottomLeft, bottomRight); colontitleTable.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + colontitleTable.getTotalHeight(), writer.getDirectContent());/*from www .j a v a 2 s . co m*/ }
From source file:org.sharegov.cirm.utils.PDFViewReport.java
License:Apache License
public void generateReport(OutputStream out, List<Long> boids) { if (DBG)//w w w . j a va 2s . c om System.out.println("Start: PDFgenerateReport: for nrOfBoids: " + boids.size()); Document doc = null; try { Date date = new Date(); setTime(formatDate(date, true, false)); doc = new Document(PageSize.A4); doc.setMargins(9, 9, doc.topMargin(), doc.bottomMargin()); PdfWriter writer = PdfWriter.getInstance(doc, out); writer.setPageEvent(new TableHeader()); doc.open(); //addMetaData(doc); LegacyEmulator le = new LegacyEmulator(); int i = 1; for (Long boid : boids) { ThreadLocalStopwatch.getWatch().time("PDFViewReport loading " + i + " of " + boids.size()); Json data = le.findServiceCaseOntology(boid).toJSON(); addContent(doc, data); i++; } } catch (DocumentException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { doc.close(); } if (DBG) System.out.println("Done: PDFgenerateReport: for nrOfBoids: " + boids.size()); }
From source file:org.sharegov.cirm.utils.PDFViewReport.java
License:Apache License
/** * Generates the HET Rebate letter based on the given input parameters * @param out : OutputStream to which the letter is written to * @param applicant : ServiceCaseActor of type Applicant * @param hasCaseNumber : Service Request's Case Number * @param isEnglish : A Flag denoting the language of the letter */// w ww . ja v a 2 s. co m public void generateHETRebateLetter(OutputStream out, Json applicant, String hasCaseNumber, boolean isEnglish) { if (DBG) System.out.println("Start : generateHETRebateLetter"); Document doc = null; try { doc = new Document(PageSize.A4); doc.setMargins(9, 9, doc.topMargin(), doc.bottomMargin()); PdfWriter writer = PdfWriter.getInstance(doc, out); doc.open(); addWASDContent(doc, applicant, hasCaseNumber, isEnglish); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { doc.close(); if (DBG) System.out.println("End : generateHETRebateLetter"); } }
From source file:org.sharegov.cirm.utils.PDFViewReport.java
License:Apache License
public void errorReport(OutputStream out) { if (DBG)/*from w ww . j a va 2 s.co m*/ System.out.println("Start: PDF errorReport"); try { Date date = new Date(); setTime(formatDate(date, true, false)); Document doc = new Document(PageSize.A4); doc.setMargins(9, 9, doc.topMargin(), doc.bottomMargin()); PdfWriter writer = PdfWriter.getInstance(doc, out); TableHeader th = new TableHeader(); writer.setPageEvent(th); doc.open(); Chapter chapter = new Chapter(new Paragraph(), 1); chapter.setNumberDepth(0); addSection(chapter).add(new Phrase(" ")); addSection(chapter).add(new Phrase(" ")); addSection(chapter).add(new Phrase(genericErrorMsg)); doc.add(chapter); doc.close(); } catch (DocumentException e) { e.printStackTrace(); throw new RuntimeException(e); } if (DBG) System.out.println("Done: PDF errorReport"); }
From source file:storehausimport.SaveToPdf.java
public boolean save(String inputFilePath, String outputFilePath) throws Exception { if (outputFilePath.isEmpty()) { throw new Exception("Trkst parametrs outputFilePath"); }//from w w w.j av a 2 s.c om if (inputFilePath.isEmpty()) { throw new Exception("Trkst parametrs inputFilePath"); } File inputFile = new File(inputFilePath); Document pdfDoc = new Document(); try { PdfWriter writer = PdfWriter.getInstance(pdfDoc, new FileOutputStream(outputFilePath)); pdfDoc.open(); pdfDoc.setMarginMirroring(true); pdfDoc.setMargins(36, 72, 108, 180); pdfDoc.topMargin(); BaseFont helvetica = BaseFont.createFont("Helvetica", BaseFont.CP1257, BaseFont.NOT_EMBEDDED); Font normal_font = new Font(helvetica, 10, Font.NORMAL); Font bold_font = new Font(); bold_font.setStyle(Font.BOLD); bold_font.setSize(10); pdfDoc.add(new Paragraph("\n")); if (inputFile.exists()) { iStream = new FileInputStream(inputFile); in = new DataInputStream(iStream); is = new InputStreamReader(in); br = new BufferedReader(is); String strLine; while ((strLine = br.readLine()) != null) { Paragraph para = new Paragraph(strLine + "\n", normal_font); para.setAlignment(Element.ALIGN_LEFT); pdfDoc.add(para); } } else { pdfDoc.close(); throw new Exception("Trkst parametrs inputFilePath"); } pdfDoc.close(); } catch (Exception ex) { throw new Exception(ex); } return true; }