/** * Writes corresponding amount of bytes from a given int * * @param bytes a source of bytes, must be >= 0 * @param size expected amount of bytes */ void write(int bytes, int size) throws IOException { //safe convert to long, despite sign. write(bytes & 0xFFFFFFFFL, size); }
/** * Manually start a Marked Content sequence with properties. Used primarily for Tagged PDF * * @param tag the type of content that will be contained * @param properties the properties of the content, including Marked Content ID. If null, the PDF marker is BMC, else it is BDC * @return current canvas */ public PdfCanvas beginMarkedContent(PdfName tag, PdfDictionary properties) { mcDepth++; PdfOutputStream out = contentStream.getOutputStream().write(tag).writeSpace(); if (properties == null) { out.writeBytes(BMC); } else if (properties.getIndirectReference() == null) { out.write(properties).writeSpace().writeBytes(BDC); } else { out.write(resources.addProperties(properties)).writeSpace().writeBytes(BDC); } return this; }
/** * Sets font and size (PDF Tf operator). * * @param font The font * @param size The font size. * @return The edited canvas. */ public PdfCanvas setFontAndSize(PdfFont font, float size) { currentGs.setFontSize(size); PdfName fontName = resources.addFont(document, font); currentGs.setFont(font); contentStream.getOutputStream() .write(fontName) .writeSpace() .writeFloat(size).writeSpace() .writeBytes(Tf); return this; }
private void write(PdfArray pdfArray) { writeByte('['); for (int i = 0; i < pdfArray.size(); i++) { PdfObject value = pdfArray.get(i, false); PdfIndirectReference indirectReference; if ((indirectReference = value.getIndirectReference()) != null) { write(indirectReference); } else { write(value); } if (i < pdfArray.size() - 1) writeSpace(); } writeByte(']'); }
/** * Paints a shading object and adds it to the resources of this canvas * * @param shading * @return current canvas. */ public PdfCanvas paintShading(PdfShading shading) { PdfName shadingName = resources.addShading(shading); contentStream.getOutputStream().write((PdfObject) shadingName).writeSpace().writeBytes(sh); return this; }
/** * Writes corresponding amount of bytes from a given long * * @param bytes a source of bytes, must be >= 0 * @param size expected amount of bytes */ void write(long bytes, int size) throws IOException { assert bytes >= 0; while (--size >= 0) { write((byte) (bytes >> 8 * size & 0xff)); } }
/** * Sets the ExtGState dictionary for the current graphics state * * @param extGState a dictionary that maps resource names to graphics state parameter dictionaries * @return current canvas. */ public PdfCanvas setExtGState(PdfExtGState extGState) { if (!extGState.isFlushed()) currentGs.updateFromExtGState(extGState, document); PdfName name = resources.addExtGState(extGState); contentStream.getOutputStream().write(name).writeSpace().writeBytes(gs); document.checkIsoConformance(currentGs, IsoKey.EXTENDED_GRAPHICS_STATE); return this; }
private PdfCanvas addImage(PdfXObject xObject, float a, float b, float c, float d, float e, float f) { saveState(); concatMatrix(a, b, c, d, e, f); PdfName name = resources.addImage(xObject.getPdfObject()); contentStream.getOutputStream().write(name).writeSpace().writeBytes(Do); restoreState(); return this; }
/** * Adds {@code PdfImageXObject} to canvas. * * @param image the {@code PdfImageXObject} object * @param a an element of the transformation matrix * @param b an element of the transformation matrix * @param c an element of the transformation matrix * @param d an element of the transformation matrix * @param e an element of the transformation matrix * @param f an element of the transformation matrix * @return canvas a reference to this object. */ private PdfCanvas addImage(PdfImageXObject image, float a, float b, float c, float d, float e, float f) { saveState(); concatMatrix(a, b, c, d, e, f); PdfName name = resources.addImage(image); contentStream.getOutputStream().write(name).writeSpace().writeBytes(Do); restoreState(); return this; }
/** * Write a slice of the passed byte array to the underlying stream * * @param b byte array to slice and write. * @param off starting index of the slice. * @param len length of the slice. * @throws java.io.IOException */ @Override public void write(byte[] b, int off, int len) throws java.io.IOException { super.write(b, off, len); if (duplicateStream != null) { duplicateStream.write(b, off, len); } }
/** * Set the rendering intent. possible values are: PdfName.AbsoluteColorimetric, * PdfName.RelativeColorimetric, PdfName.Saturation, PdfName.Perceptual. * * @param renderingIntent a PdfName containing a color metric * @return current canvas. */ public PdfCanvas setRenderingIntent(PdfName renderingIntent) { document.checkIsoConformance(renderingIntent, IsoKey.RENDERING_INTENT); if (renderingIntent.equals(currentGs.getRenderingIntent())) return this; currentGs.setRenderingIntent(renderingIntent); contentStream.getOutputStream() .write(renderingIntent).writeSpace() .writeBytes(ri); return this; }
/** * This method is invoked while deserialization */ private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { in.defaultReadObject(); if (outputStream == null && duplicateContentBuffer != null) { outputStream = new ByteArrayOutputStream(); write(duplicateContentBuffer); duplicateContentBuffer = null; } }
/** * Adds {@code PdfFormXObject} to canvas. * * @param form the {@code PdfImageXObject} object * @param a an element of the transformation matrix * @param b an element of the transformation matrix * @param c an element of the transformation matrix * @param d an element of the transformation matrix * @param e an element of the transformation matrix * @param f an element of the transformation matrix * @return current canvas. */ private PdfCanvas addForm(PdfFormXObject form, float a, float b, float c, float d, float e, float f) { saveState(); concatMatrix(a, b, c, d, e, f); PdfName name = resources.addForm(form); contentStream.getOutputStream().write(name).writeSpace().writeBytes(Do); restoreState(); return this; }
@Override protected void copyContent(PdfObject from, PdfDocument document) { super.copyContent(from, document); PdfStream stream = (PdfStream) from; assert inputStream == null : "Try to copy the PdfStream that has been just created."; byte[] bytes = stream.getBytes(false); try { outputStream.write(bytes); } catch (IOException ioe) { throw new PdfException(PdfException.CannotCopyObjectContent, ioe, stream); } }
/** * Returns the resources needed for the object that was used to create * this PdfResourceCounter. If you pass a Map with resources that were * already used by other objects, these objects will not be taken into * account. * * @param res The resources that can be excluded when counting the bytes. * @return The number of bytes needed for an object. */ public long getLength(Map<Integer, PdfObject> res) { long length = 0; for (int ref : resources.keySet()) { if (res != null && res.containsKey(ref)) { continue; } PdfOutputStream os = new PdfOutputStream(new IdelOutputStream()); os.write(resources.get(ref).clone()); length += os.getCurrentPos(); } return length; } }
/** * Sets the XMP Metadata. * * @param xmpMetadata the {@code byte[]} of XMP Metadata to set. * @return this {@link PdfPage} instance. * @throws IOException in case of writing error. */ public PdfPage setXmpMetadata(byte[] xmpMetadata) throws IOException { PdfStream xmp = (PdfStream) new PdfStream().makeIndirect(getDocument()); xmp.getOutputStream().write(xmpMetadata); xmp.put(PdfName.Type, PdfName.Metadata); xmp.put(PdfName.Subtype, PdfName.XML); put(PdfName.Metadata, xmp); return this; }
/** * Copies page as FormXObject to the specified document. * * @param toDocument a document to copy to. * @return copied {@link PdfFormXObject} object. */ public PdfFormXObject copyAsFormXObject(PdfDocument toDocument) throws IOException { PdfFormXObject xObject = new PdfFormXObject(getCropBox()); List<PdfName> excludedKeys = new ArrayList<>(Arrays.asList(PdfName.MediaBox, PdfName.CropBox, PdfName.Contents) ); excludedKeys.addAll(this.excludedKeys); PdfDictionary dictionary = getPdfObject().copyTo(toDocument, excludedKeys, true); xObject.getPdfObject().getOutputStream().write(getContentBytes()); xObject.getPdfObject().mergeDifferent(dictionary); //Copy inherited resources if (!xObject.getPdfObject().containsKey(PdfName.Resources)) { PdfObject copyResource = getResources().getPdfObject().copyTo(toDocument, true); xObject.getPdfObject().put(PdfName.Resources, copyResource); } return xObject; }