public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) { PdfContentByte cb = outputDevice.getCurrentPage(); PdfWriter writer = outputDevice.getWriter(); PdfAcroForm acroForm = writer.getAcroForm(); Element elem = box.getElement(); String name = getFieldName(outputDevice, elem); String value = getValue(elem); acroForm.addHiddenField(name, value); }
public ITextRenderer(float dotsPerPoint, int dotsPerPixel) { this(dotsPerPoint, dotsPerPixel, new ITextOutputDevice(dotsPerPoint)); }
public List findPagePositionsByID(CssContext c, Pattern pattern) { Map idMap = _sharedContext.getIdMap(); if (idMap == null) { return Collections.EMPTY_LIST; } List result = new ArrayList(); for (Iterator i = idMap.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Entry) i.next(); String id = (String) entry.getKey(); if (pattern.matcher(id).find()) { Box box = (Box) entry.getValue(); PagePosition pos = calcPDFPagePosition(c, id, box); if (pos != null) { result.add(pos); } } } Collections.sort(result, new Comparator() { public int compare(Object arg0, Object arg1) { PagePosition p1 = (PagePosition) arg0; PagePosition p2 = (PagePosition) arg1; return p1.getPageNo() - p2.getPageNo(); } }); return result; }
private com.lowagie.text.Rectangle checkLinkArea(RenderingContext c, Box box) { com.lowagie.text.Rectangle targetArea = calcTotalLinkArea(c, box); String key = createRectKey(targetArea); if (_linkTargetAreas.contains(key)) { return null; } _linkTargetAreas.add(key); return targetArea; }
private void paintPage(RenderingContext c, PdfWriter writer, PageBox page) { provideMetadataToPage(writer, page); page.paintBackground(c, 0, Layer.PAGED_MODE_PRINT); page.paintMarginAreas(c, 0, Layer.PAGED_MODE_PRINT); page.paintBorder(c, 0, Layer.PAGED_MODE_PRINT); Shape working = _outputDevice.getClip(); Rectangle content = page.getPrintClippingBounds(c); _outputDevice.clip(content); int top = -page.getPaintingTop() + page.getMarginBorderPadding(c, CalculatedStyle.TOP); int left = page.getMarginBorderPadding(c, CalculatedStyle.LEFT); _outputDevice.translate(left, top); _root.getLayer().paint(c); _outputDevice.translate(-left, -top); _outputDevice.setClip(working); }
private void drawPDFAsImage(PDFAsImage image, int x, int y) { URI uri = image.getURI(); PdfReader reader = null; try { reader = getReader(uri); } catch (IOException e) { throw new XRRuntimeException("Could not load " + uri + ": " + e.getMessage(), e); } PdfImportedPage page = getWriter().getImportedPage(reader, 1); AffineTransform at = AffineTransform.getTranslateInstance(x, y); at.translate(0, image.getHeightAsFloat()); at.scale(image.getWidthAsFloat(), image.getHeightAsFloat()); AffineTransform inverse = normalizeMatrix(_transform); AffineTransform flipper = AffineTransform.getScaleInstance(1, -1); inverse.concatenate(at); inverse.concatenate(flipper); double[] mx = new double[6]; inverse.getMatrix(mx); mx[0] = image.scaleWidth(); mx[3] = image.scaleHeight(); _currentPage.restoreState(); _currentPage.addTemplate(page, (float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]); _currentPage.saveState(); }
private void createAppearance( RenderingContext c, ITextOutputDevice outputDevice, BlockBox box, PdfFormField field) { PdfWriter writer = outputDevice.getWriter(); ITextFSFont font = (ITextFSFont)box.getStyle().getFSFont(c); PdfContentByte cb = writer.getDirectContent(); float width = outputDevice.getDeviceLength(getWidth()); float height = outputDevice.getDeviceLength(getHeight()); float fontSize = outputDevice.getDeviceLength(font.getSize2D()); PdfAppearance tp = cb.createAppearance(width, height); tp.setFontAndSize(font.getFontDescription().getFont(), fontSize); FSColor color = box.getStyle().getColor(); setFillColor(tp, color); field.setDefaultAppearanceString(tp); }
public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) { PdfWriter writer = outputDevice.getWriter(); Element elem = box.getElement(); Rectangle targetArea = outputDevice.createLocalTargetArea(c, box); TextField field = new TextField(writer, targetArea, getFieldName(outputDevice, elem)); String value = getValue(elem); field.setText(value); try { PdfFormField formField = field.getTextField(); createAppearance(c, outputDevice, box, formField, value); //TODO add max length back in if (isReadOnly(elem)) { formField.setFieldFlags(PdfFormField.FF_READ_ONLY); } writer.addAnnotation(formField); } catch (IOException ioe) { System.out.println(ioe); } catch (DocumentException de) { System.out.println(de); } }
public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) { PdfContentByte cb = outputDevice.getCurrentPage(); PdfWriter writer = outputDevice.getWriter(); PdfFormField field = PdfFormField.createCheckBox(writer); Element e = box.getElement(); String onValue = getValue(e); boolean checked = isChecked(e); float width = outputDevice.getDeviceLength(getWidth()); float height = outputDevice.getDeviceLength(getHeight()); FSColor color = box.getStyle().getColor(); FSColor darker = box.getEffBackgroundColor(c).darkenColor(); createAppearances(cb, field, onValue, width, height, true, color, darker); createAppearances(cb, field, onValue, width, height, false, color, darker); field.setWidget(outputDevice.createLocalTargetArea(c, box), PdfAnnotation.HIGHLIGHT_INVERT); field.setFieldName(getFieldName(outputDevice, e)); field.setBorderStyle(new PdfBorderDictionary(0.0f, 0)); field.setValueAsName(checked ? onValue : OFF_STATE); field.setAppearanceState(checked ? onValue : OFF_STATE); if (isReadOnly(e)) { field.setFieldFlags(PdfFormField.FF_READ_ONLY); } writer.addAnnotation(field); }
public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) { PdfContentByte cb = outputDevice.getCurrentPage(); PdfWriter writer = outputDevice.getWriter(); Element elm = box.getElement(); Rectangle targetArea = outputDevice.createLocalTargetArea(c, box); String onValue = getValue(elm); RadioCheckField field = new RadioCheckField(writer, targetArea, getFieldName(outputDevice, elm), onValue); field.setChecked(isChecked(elm)); field.setCheckType(RadioCheckField.TYPE_CHECK); field.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); //TODO Consider if we can get some more correct color field.setBorderColor(Color.black); field.setBorderWidth(BaseField.BORDER_WIDTH_THIN); try { PdfFormField formField = field.getCheckField(); if (isReadOnly(elm)) { formField.setFieldFlags(PdfFormField.FF_READ_ONLY); } writer.addAnnotation(formField); } catch (IOException ioe) { System.out.println(ioe); } catch (DocumentException de) { System.out.println(de); } }
if (url.getPath() != null && url.getPath().toLowerCase().endsWith(".pdf")) { PdfReader reader = _outputDevice.getReader(url); PDFAsImage image = new PDFAsImage(url); Rectangle rect = reader.getPageSizeWithRotation(1); image.setInitialWidth(rect.getWidth()*_outputDevice.getDotsPerPoint()); image.setInitialHeight(rect.getHeight()*_outputDevice.getDotsPerPoint()); resource = new ImageResource(image); } else {
public com.itextpdf.text.Rectangle createTargetArea(RenderingContext c, Box box) { PageBox current = c.getPage(); boolean inCurrentPage = box.getAbsY() > current.getTop() && box.getAbsY() < current.getBottom(); if (inCurrentPage || box.isContainedInMarginBox()) { return createLocalTargetArea(c, box); } else { Rectangle bounds = box.getContentAreaEdge(box.getAbsX(), box.getAbsY(), c); PageBox page = _root.getLayer().getPage(c, bounds.y); float bottom = getDeviceLength(page.getBottom() - (bounds.y + bounds.height) + page.getMarginBorderPadding(c, CalculatedStyle.BOTTOM)); float left = getDeviceLength(page.getMarginBorderPadding(c, CalculatedStyle.LEFT) + bounds.x); com.itextpdf.text.Rectangle result = new com.itextpdf.text.Rectangle(left, bottom, left + getDeviceLength(bounds.width), bottom + getDeviceLength(bounds.height)); return result; } }
private com.itextpdf.text.Rectangle calcTotalLinkArea(RenderingContext c, Box box) { Box current = box; while (true) { Box prev = current.getPreviousSibling(); if (prev == null || prev.getElement() != box.getElement()) { break; } current = prev; } com.itextpdf.text.Rectangle result = createLocalTargetArea(c, current, true); current = current.getNextSibling(); while (current != null && current.getElement() == box.getElement()) { result = add(result, createLocalTargetArea(c, current, true)); current = current.getNextSibling(); } return result; }
Box target = _sharedContext.getBoxById(anchor); if (target != null) { PdfDestination dest = createDestination(c, target); com.lowagie.text.Rectangle targetArea = checkLinkArea(c, box); if (targetArea == null) { return; PdfAction action = new PdfAction(uri); com.lowagie.text.Rectangle targetArea = checkLinkArea(c, box); if (targetArea == null) { return;
@Override public void paint(RenderingContext renderingContext, ITextOutputDevice outputDevice, BlockBox blockBox) { PdfContentByte cb = outputDevice.getWriter().getDirectContent(); float width = (float) (cssWidth / outputDevice.getDotsPerPoint()); float height = (float) (cssHeight / outputDevice.getDotsPerPoint()); PdfTemplate template = cb.createTemplate(width, height); Graphics2D g2d = template.createGraphics(width, height); PrintTranscoder prm = new PrintTranscoder(); TranscoderInput ti = new TranscoderInput(svg); prm.transcode(ti, null); PageFormat pg = new PageFormat(); Paper pp = new Paper(); pp.setSize(width, height); pp.setImageableArea(0, 0, width, height); pg.setPaper(pp); prm.print(g2d, pg, 0); g2d.dispose(); PageBox page = renderingContext.getPage(); float x = blockBox.getAbsX() + page.getMarginBorderPadding(renderingContext, CalculatedStyle.LEFT); float y = (page.getBottom() - (blockBox.getAbsY() + cssHeight)) + page.getMarginBorderPadding( renderingContext, CalculatedStyle.BOTTOM); x /= outputDevice.getDotsPerPoint(); y /= outputDevice.getDotsPerPoint(); cb.addTemplate(template, x, y); }
public ITextRenderer(float dotsPerPoint, int dotsPerPixel) { _dotsPerPoint = dotsPerPoint; _outputDevice = new ITextOutputDevice(_dotsPerPoint); ITextUserAgent userAgent = new ITextUserAgent(_outputDevice); _sharedContext = new SharedContext(userAgent); userAgent.setSharedContext(_sharedContext); _outputDevice.setSharedContext(_sharedContext); ITextFontResolver fontResolver = new ITextFontResolver(_sharedContext); _sharedContext.setFontResolver(fontResolver); ITextReplacedElementFactory replacedElementFactory = new ITextReplacedElementFactory(_outputDevice); _sharedContext.setReplacedElementFactory(replacedElementFactory); _sharedContext.setTextRenderer(new ITextTextRenderer()); _sharedContext.setDPI(72*_dotsPerPoint); _sharedContext.setDotsPerPixel(dotsPerPixel); _sharedContext.setPrint(true); _sharedContext.setInteractive(false); }
/** * Add PDF meta values to the target PDF document. */ private void addPdfMetaValuesToPdfDocument( ITextRenderer renderer ) { Iterator pdfNameIter = this.pdfInfoValues.keySet().iterator(); while (pdfNameIter.hasNext()) { PdfName pdfName = (PdfName) pdfNameIter.next(); PdfString pdfString = (PdfString) pdfInfoValues.get( pdfName ); XRLog.render(Level.FINEST, "pdfName=" + pdfName + ", pdfString=" + pdfString ); renderer.getOutputDevice().getWriter().getInfo().put( pdfName, pdfString ); } if ( XRLog.isLoggingEnabled() ) { XRLog.render(Level.FINEST, "added " + renderer.getOutputDevice().getWriter().getInfo().getKeys() ); } }
private void writeNamedDestinations(RenderingContext c) { Map idMap = getSharedContext().getIdMap(); if ((idMap != null) && (!idMap.isEmpty())) { PdfArray dests = new PdfArray(); dests.add(new PdfString(anchorName, PdfString.TEXT_UNICODE)); PdfDestination dest = createDestination(c, targetBox); if (dest != null) { PdfIndirectReference ref = _writer.addToBody(dest).getIndirectReference();
public com.itextpdf.text.Rectangle createLocalTargetArea(RenderingContext c, Box box) { return createLocalTargetArea(c, box, false); }
private void paintPage(RenderingContext c, PdfWriter writer, PageBox page) { provideMetadataToPage(writer, page); page.paintBackground(c, 0, Layer.PAGED_MODE_PRINT); page.paintMarginAreas(c, 0, Layer.PAGED_MODE_PRINT); page.paintBorder(c, 0, Layer.PAGED_MODE_PRINT); Shape working = _outputDevice.getClip(); Rectangle content = page.getPrintClippingBounds(c); _outputDevice.clip(content); int top = -page.getPaintingTop() + page.getMarginBorderPadding(c, CalculatedStyle.TOP); int left = page.getMarginBorderPadding(c, CalculatedStyle.LEFT); _outputDevice.translate(left, top); _root.getLayer().paint(c); _outputDevice.translate(-left, -top); _outputDevice.setClip(working); }