List of usage examples for com.lowagie.text.pdf PdfContentByte setLineWidth
public void setLineWidth(float w)
From source file:org.revager.export.PDFPageEventHelper.java
License:Open Source License
/** * Sets the marks to the PDF document./*from www. ja v a2 s . c o m*/ * * @param writer * the PDF writer * @param document * the PDF document */ private void setMarks(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); float height = PDFTools.ptToCm(document.getPageSize().getHeight()); cb.setLineWidth(0.0f); cb.moveTo(0.0f, PDFTools.cmToPt(height / 2.0f)); cb.lineTo(PDFTools.cmToPt(0.3f), PDFTools.cmToPt(height / 2.0f)); cb.moveTo(0.0f, PDFTools.cmToPt(height * 0.33f)); cb.lineTo(PDFTools.cmToPt(0.3f), PDFTools.cmToPt(height * 0.33f)); cb.moveTo(0.0f, PDFTools.cmToPt(height * 0.66f)); cb.lineTo(PDFTools.cmToPt(0.3f), PDFTools.cmToPt(height * 0.66f)); cb.stroke(); }
From source file:org.xhtmlrenderer.pdf.ITextOutputDevice.java
License:Open Source License
public void drawString(String s, float x, float y, JustificationInfo info) { if (Configuration.isTrue("xr.renderer.replace-missing-characters", false)) { s = replaceMissingCharacters(s); }/*www . j a v a 2s .c om*/ if (s.length() == 0) return; PdfContentByte cb = _currentPage; ensureFillColor(); AffineTransform at = (AffineTransform) getTransform().clone(); at.translate(x, y); AffineTransform inverse = normalizeMatrix(at); AffineTransform flipper = AffineTransform.getScaleInstance(1, -1); inverse.concatenate(flipper); inverse.scale(_dotsPerPoint, _dotsPerPoint); double[] mx = new double[6]; inverse.getMatrix(mx); cb.beginText(); // Check if bold or italic need to be emulated boolean resetMode = false; FontDescription desc = _font.getFontDescription(); float fontSize = _font.getSize2D() / _dotsPerPoint; cb.setFontAndSize(desc.getFont(), fontSize); float b = (float) mx[1]; float c = (float) mx[2]; FontSpecification fontSpec = getFontSpecification(); if (fontSpec != null) { int need = ITextFontResolver.convertWeightToInt(fontSpec.fontWeight); int have = desc.getWeight(); if (need > have) { cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); float lineWidth = fontSize * 0.04f; // 4% of font size cb.setLineWidth(lineWidth); resetMode = true; ensureStrokeColor(); } if ((fontSpec.fontStyle == IdentValue.ITALIC) && (desc.getStyle() != IdentValue.ITALIC)) { b = 0f; c = 0.21256f; } } cb.setTextMatrix((float) mx[0], b, c, (float) mx[3], (float) mx[4], (float) mx[5]); if (info == null) { cb.showText(s); } else { PdfTextArray array = makeJustificationArray(s, info); cb.showText(array); } if (resetMode) { cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); cb.setLineWidth(1); } cb.endText(); }
From source file:org.xhtmlrenderer.pdf.ITextOutputDevice.java
License:Open Source License
private void setStrokeDiff(Stroke newStroke, Stroke oldStroke) { PdfContentByte cb = _currentPage; if (newStroke == oldStroke) return;/*from w ww . j a v a 2s . c o m*/ if (!(newStroke instanceof BasicStroke)) return; BasicStroke nStroke = (BasicStroke) newStroke; boolean oldOk = (oldStroke instanceof BasicStroke); BasicStroke oStroke = null; if (oldOk) oStroke = (BasicStroke) oldStroke; if (!oldOk || nStroke.getLineWidth() != oStroke.getLineWidth()) cb.setLineWidth(nStroke.getLineWidth()); if (!oldOk || nStroke.getEndCap() != oStroke.getEndCap()) { switch (nStroke.getEndCap()) { case BasicStroke.CAP_BUTT: cb.setLineCap(0); break; case BasicStroke.CAP_SQUARE: cb.setLineCap(2); break; default: cb.setLineCap(1); } } if (!oldOk || nStroke.getLineJoin() != oStroke.getLineJoin()) { switch (nStroke.getLineJoin()) { case BasicStroke.JOIN_MITER: cb.setLineJoin(0); break; case BasicStroke.JOIN_BEVEL: cb.setLineJoin(2); break; default: cb.setLineJoin(1); } } if (!oldOk || nStroke.getMiterLimit() != oStroke.getMiterLimit()) cb.setMiterLimit(nStroke.getMiterLimit()); boolean makeDash; if (oldOk) { if (nStroke.getDashArray() != null) { if (nStroke.getDashPhase() != oStroke.getDashPhase()) { makeDash = true; } else if (!java.util.Arrays.equals(nStroke.getDashArray(), oStroke.getDashArray())) { makeDash = true; } else makeDash = false; } else if (oStroke.getDashArray() != null) { makeDash = true; } else makeDash = false; } else { makeDash = true; } if (makeDash) { float dash[] = nStroke.getDashArray(); if (dash == null) cb.setLiteral("[]0 d\n"); else { cb.setLiteral('['); int lim = dash.length; for (int k = 0; k < lim; ++k) { cb.setLiteral(dash[k]); cb.setLiteral(' '); } cb.setLiteral(']'); cb.setLiteral(nStroke.getDashPhase()); cb.setLiteral(" d\n"); } } }
From source file:oscar.eform.util.EFormPDFServlet.java
License:Open Source License
private void writeContent(Properties printCfg, Properties props, Properties measurements, float height, PdfContentByte cb) throws Exception { for (Enumeration e = printCfg.propertyNames(); e.hasMoreElements();) { StringBuilder temp = new StringBuilder(e.nextElement().toString()); String[] cfgVal = printCfg.getProperty(temp.toString()).split(" *, *"); String[] fontType = null; int fontFlags = 0; if (cfgVal[4].indexOf(";") > -1) { fontType = cfgVal[4].split(";"); if (fontType[1].trim().equals("italic")) fontFlags = Font.ITALIC; else if (fontType[1].trim().equals("bold")) fontFlags = Font.BOLD; else if (fontType[1].trim().equals("bolditalic")) fontFlags = Font.BOLDITALIC; else/*from w ww. j av a2 s . c om*/ fontFlags = Font.NORMAL; } else { fontFlags = Font.NORMAL; fontType = new String[] { cfgVal[4].trim() }; } String encoding = null; if (fontType[0].trim().equals("BaseFont.HELVETICA")) { fontType[0] = BaseFont.HELVETICA; encoding = BaseFont.CP1252; //latin1 encoding } else if (fontType[0].trim().equals("BaseFont.HELVETICA_OBLIQUE")) { fontType[0] = BaseFont.HELVETICA_OBLIQUE; encoding = BaseFont.CP1252; } else if (fontType[0].trim().equals("BaseFont.ZAPFDINGBATS")) { fontType[0] = BaseFont.ZAPFDINGBATS; encoding = BaseFont.ZAPFDINGBATS; } else { fontType[0] = BaseFont.COURIER; encoding = BaseFont.CP1252; } BaseFont bf = BaseFont.createFont(fontType[0], encoding, BaseFont.NOT_EMBEDDED); String propValue = props.getProperty(temp.toString()); //if not in regular config then check measurements if (propValue == null) { propValue = measurements.getProperty(temp.toString(), ""); } ColumnText ct = new ColumnText(cb); // write in a rectangle area if (cfgVal.length >= 9) { Font font = new Font(bf, Integer.parseInt(cfgVal[5].trim()), fontFlags); ct.setSimpleColumn(Integer.parseInt(cfgVal[1].trim()), (height - Integer.parseInt(cfgVal[2].trim())), Integer.parseInt(cfgVal[7].trim()), (height - Integer.parseInt(cfgVal[8].trim())), Integer.parseInt(cfgVal[9].trim()), (cfgVal[0].trim().equals("left") ? Element.ALIGN_LEFT : (cfgVal[0].trim().equals("right") ? Element.ALIGN_RIGHT : Element.ALIGN_CENTER))); ct.setText(new Phrase(12, propValue, font)); ct.go(); continue; } // draw line directly if (temp.toString().startsWith("__$line")) { cb.setRGBColorStrokeF(0f, 0f, 0f); cb.setLineWidth(Float.parseFloat(cfgVal[4].trim())); cb.moveTo(Float.parseFloat(cfgVal[0].trim()), Float.parseFloat(cfgVal[1].trim())); cb.lineTo(Float.parseFloat(cfgVal[2].trim()), Float.parseFloat(cfgVal[3].trim())); cb.stroke(); } else if (temp.toString().startsWith("__")) { cb.beginText(); cb.setFontAndSize(bf, Integer.parseInt(cfgVal[5].trim())); cb.showTextAligned( (cfgVal[0].trim().equals("left") ? PdfContentByte.ALIGN_LEFT : (cfgVal[0].trim().equals("right") ? PdfContentByte.ALIGN_RIGHT : PdfContentByte.ALIGN_CENTER)), (cfgVal.length >= 7 ? (cfgVal[6].trim()) : propValue), Integer.parseInt(cfgVal[1].trim()), (height - Integer.parseInt(cfgVal[2].trim())), 0); cb.endText(); } else { // write prop text cb.beginText(); cb.setFontAndSize(bf, Integer.parseInt(cfgVal[5].trim())); cb.showTextAligned( (cfgVal[0].trim().equals("left") ? PdfContentByte.ALIGN_LEFT : (cfgVal[0].trim().equals("right") ? PdfContentByte.ALIGN_RIGHT : PdfContentByte.ALIGN_CENTER)), (cfgVal.length >= 7 ? ((propValue.equals("") ? "" : cfgVal[6].trim())) : propValue), Integer.parseInt(cfgVal[1].trim()), (height - Integer.parseInt(cfgVal[2].trim())), 0); cb.endText(); } } }
From source file:oscar.eform.util.EFormPDFServlet.java
License:Open Source License
private void plotProperties(Properties tp, Properties props, List<String> xDate, List<String> yHeight, float height, PdfContentByte cb, boolean countEven) { StringBuilder temp = null;/* w w w . j av a 2 s . co m*/ String tempValue = null; String className = null; String[] tempYcoords; int origX = 0; int origY = 0; Properties args = new Properties(); for (Enumeration e = tp.propertyNames(); e.hasMoreElements();) { temp = new StringBuilder(e.nextElement().toString()); tempValue = tp.getProperty(temp.toString()).trim(); if (temp.toString().equals("__finalEDB")) args.setProperty(temp.toString(), props.getProperty(tempValue)); else if (temp.toString().equals("__xDateScale")) args.setProperty(temp.toString(), props.getProperty(tempValue)); else if (temp.toString().equals("__dateFormat")) args.setProperty(temp.toString(), tempValue); else if (temp.toString().equals("__nMaxPixX")) args.setProperty(temp.toString(), tempValue); else if (temp.toString().equals("__nMaxPixY")) args.setProperty(temp.toString(), tempValue); else if (temp.toString().equals("__fStartX")) args.setProperty(temp.toString(), tempValue); else if (temp.toString().equals("__fEndX")) args.setProperty(temp.toString(), tempValue); else if (temp.toString().equals("__fStartY")) args.setProperty(temp.toString(), tempValue); else if (temp.toString().equals("__fEndY")) args.setProperty(temp.toString(), tempValue); else if (temp.toString().equals("__origX")) origX = Integer.parseInt(tempValue); else if (temp.toString().equals("__origY")) origY = Integer.parseInt(tempValue); else if (temp.toString().equals("__className")) className = tempValue; else { MiscUtils.getLogger() .debug("Adding xDate " + temp.toString() + " VAL: " + props.getProperty(temp.toString())); MiscUtils.getLogger() .debug("Adding yHeight " + tempValue + " VAL: " + props.getProperty(tempValue)); xDate.add(props.getProperty(temp.toString())); yHeight.add(props.getProperty(tempValue)); } } // end for read in from config file FrmPdfGraphic pdfGraph = FrmGraphicFactory.create(className); pdfGraph.init(args); Properties gProp = pdfGraph.getGraphicXYProp(xDate, yHeight); //draw the pic cb.setLineWidth(1.5f); if (countEven) { cb.setRGBColorStrokeF(0f, 0f, 255f); } else { cb.setRGBColorStrokeF(255f, 0f, 0f); } for (Enumeration e = gProp.propertyNames(); e.hasMoreElements();) { temp = new StringBuilder(e.nextElement().toString()); tempValue = gProp.getProperty(temp.toString(), ""); if (tempValue.equals("")) { continue; } tempYcoords = tempValue.split(","); for (int idx = 0; idx < tempYcoords.length; ++idx) { tempValue = tempYcoords[idx]; cb.circle((origX + Float.parseFloat(temp.toString())), (height - origY + Float.parseFloat(tempValue)), 1.5f); cb.stroke(); } } }
From source file:questions.importpages.NameCards.java
public static void createSheet(int p) throws DocumentException, IOException { Rectangle rect = new Rectangle(PageSize.A4); Document document = new Document(rect); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(SHEET[p - 1])); document.open();//ww w. j av a 2 s. co m PdfContentByte canvas = writer.getDirectContentUnder(); PdfReader reader = new PdfReader(NameCard.CARD); PdfImportedPage front = writer.getImportedPage(reader, p); float x = rect.getWidth() / 2 - front.getWidth(); float y = (rect.getHeight() - (front.getHeight() * 5)) / 2; canvas.setLineWidth(0.5f); canvas.moveTo(x, y - 15); canvas.lineTo(x, y); canvas.lineTo(x - 15, y); canvas.moveTo(x + front.getWidth(), y - 15); canvas.lineTo(x + front.getWidth(), y); canvas.moveTo(x + front.getWidth() * 2, y - 15); canvas.lineTo(x + front.getWidth() * 2, y); canvas.lineTo(x + front.getWidth() * 2 + 15, y); canvas.stroke(); for (int i = 0; i < 5; i++) { for (int j = 0; j < 2; j++) { canvas.addTemplate(front, x, y); x += front.getWidth(); } x = rect.getWidth() / 2 - front.getWidth(); y += front.getHeight(); canvas.moveTo(x, y); canvas.lineTo(x - 15, y); canvas.moveTo(x + front.getWidth() * 2, y); canvas.lineTo(x + front.getWidth() * 2 + 15, y); canvas.stroke(); } canvas.moveTo(x, y + 15); canvas.lineTo(x, y); canvas.moveTo(x + front.getWidth(), y + 15); canvas.lineTo(x + front.getWidth(), y); canvas.moveTo(x + front.getWidth() * 2, y + 15); canvas.lineTo(x + front.getWidth() * 2, y); canvas.stroke(); document.close(); }
From source file:uk.ac.bbsrc.tgac.miso.core.data.decorator.itext.ITextProjectDecorator.java
License:Open Source License
public void buildReport() throws DocumentException { report = new Document(); PdfWriter writer = PdfWriter.getInstance(report, stream); report.open();/*from w w w . ja v a2s. c o m*/ report.add(new Paragraph("Project Summary")); PdfContentByte cb = writer.getDirectContent(); cb.setLineWidth(2.0f); // Make a bit thicker than 1.0 default cb.setGrayStroke(0.9f); // 1 = black, 0 = white float x = 72f; float y = 200f; cb.moveTo(x, y); cb.lineTo(x + 72f * 6, y); cb.stroke(); report.add(new Paragraph(project.getAlias())); report.add(new Paragraph(project.getDescription())); PdfPTable t = new PdfPTable(1); t.setHorizontalAlignment(Element.ALIGN_CENTER); t.setWidthPercentage(100f); // this would be the 100 from setHorizontalLine t.setSpacingAfter(5f); t.setSpacingBefore(0f); t.getDefaultCell().setUseVariableBorders(true); t.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); t.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); t.getDefaultCell().setBorder(Rectangle.BOTTOM); // This generates the line t.getDefaultCell().setBorderWidth(1f); // this would be the 1 from setHorizontalLine t.getDefaultCell().setPadding(0); t.addCell(""); report.add(t); x = 72f; y = 100f; cb.moveTo(x, y); cb.lineTo(x + 72f * 6, y); cb.stroke(); if (project.getSamples().size() > 0) { report.add(new Paragraph("Samples")); for (Sample sample : project.getSamples()) { Paragraph sPara = new Paragraph(sample.getAlias(), FontFactory.getFont("Helvetica", 12, Font.BOLD)); sPara.setIndentationLeft(20); report.add(sPara); report.add(new Paragraph(sample.getDescription())); } } report.close(); }