private static PdfStream makeType0(PdfArray domain, PdfArray range, PdfArray size, PdfNumber bitsPerSample, PdfNumber order, PdfArray encode, PdfArray decode, byte[] samples) { PdfStream stream = new PdfStream(samples); stream.put(PdfName.FunctionType, new PdfNumber(0)); stream.put(PdfName.Domain, domain); stream.put(PdfName.Range, range); stream.put(PdfName.Size, size); stream.put(PdfName.BitsPerSample, bitsPerSample); if (order != null) stream.put(PdfName.Order, order); if (encode != null) stream.put(PdfName.Encode, encode); if (decode != null) stream.put(PdfName.Decode, decode); return stream; } }
/** * Puts the value into Image XObject dictionary and associates it with the specified key. * If the key is already present, it will override the old value with the specified one. * * @param key key to insert or to override * @param value the value to associate with the specified key * @return object itself. */ public PdfImageXObject put(PdfName key, PdfObject value) { getPdfObject().put(key, value); return this; }
/** * Puts the value into Image XObject dictionary and associates it with the specified key. * If the key is already present, it will override the old value with the specified one. * * @param key key to insert or to override * @param value the value to associate with the specified key * @return object itself. */ public PdfFormXObject put(PdfName key, PdfObject value) { getPdfObject().put(key, value); return this; }
public static PdfStream getIccProfileStream(IccProfile iccProfile) { PdfStream stream = new PdfStream(iccProfile.getData()); stream.put(PdfName.N, new PdfNumber(iccProfile.getNumComponents())); switch (iccProfile.getNumComponents()) { case 1: stream.put(PdfName.Alternate, PdfName.DeviceGray); break; case 3: stream.put(PdfName.Alternate, PdfName.DeviceRGB); break; case 4: stream.put(PdfName.Alternate, PdfName.DeviceCMYK); break; default: break; } return stream; }
/** * Creates a new instance of Form XObject. * * @param bBox the form XObject’s bounding box. */ public PdfFormXObject(Rectangle bBox) { super(new PdfStream()); getPdfObject().put(PdfName.Type, PdfName.XObject); getPdfObject().put(PdfName.Subtype, PdfName.Form); if (bBox != null) { getPdfObject().put(PdfName.BBox, new PdfArray(bBox)); } }
public static PdfStream getIccProfileStream(IccProfile iccProfile, float[] range) { PdfStream stream = getIccProfileStream(iccProfile); stream.put(PdfName.Range, new PdfArray(range)); return stream; }
/** * Sets the layer this XObject belongs to. * * @param layer the layer this XObject belongs to. */ public void setLayer(IPdfOCG layer) { getPdfObject().put(PdfName.OC, layer.getIndirectReference()); }
/** * Creates a sound annotation. Sound annotations are deprecated in PDF 2.0. * @param document * @param rect * @param soundStream * @param sampleRate * @param encoding * @param channels * @param sampleSizeInBits * @throws IOException */ public PdfSoundAnnotation(PdfDocument document, Rectangle rect, InputStream soundStream, float sampleRate, PdfName encoding, int channels, int sampleSizeInBits) throws IOException { super(rect); PdfStream sound = new PdfStream(document, correctWavFile(soundStream)); sound.put(PdfName.R, new PdfNumber(sampleRate)); sound.put(PdfName.E, encoding); sound.put(PdfName.B, new PdfNumber(sampleSizeInBits)); sound.put(PdfName.C, new PdfNumber(channels)); put(PdfName.Sound, sound); }
private void applyRotation(PdfFormXObject xObject, float height, float width) { switch (rotation) { case 90: xObject.getPdfObject().put(PdfName.Matrix, new PdfArray(new float[]{0, 1, -1, 0, height, 0})); break; case 180: xObject.getPdfObject().put(PdfName.Matrix, new PdfArray(new float[]{-1, 0, 0, -1, width, height})); break; case 270: xObject.getPdfObject().put(PdfName.Matrix, new PdfArray(new float[]{0, -1, 1, 0, 0, width})); break; } }
private void applyRotation(PdfFormXObject xObject, float height, float width) { switch (rotation) { case 90: xObject.getPdfObject().put(PdfName.Matrix, new PdfArray(new float[]{0, 1, -1, 0, height, 0})); break; case 180: xObject.getPdfObject().put(PdfName.Matrix, new PdfArray(new float[]{-1, 0, 0, -1, width, height})); break; case 270: xObject.getPdfObject().put(PdfName.Matrix, new PdfArray(new float[]{0, -1, 1, 0, 0, width})); break; } }
/** * 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; }
/** * Returns files associated with XObject. * * @return associated files array. */ public PdfArray getAssociatedFiles(boolean create) { PdfArray afArray = getPdfObject().getAsArray(PdfName.AF); if (afArray == null && create) { afArray = new PdfArray(); getPdfObject().put(PdfName.AF, afArray); } return afArray; }
@Override public PdfResources getResources() { if (this.resources == null) { PdfDictionary resourcesDict = getPdfObject().getAsDictionary(PdfName.Resources); if (resourcesDict == null) { resourcesDict = new PdfDictionary(); getPdfObject().put(PdfName.Resources, resourcesDict); } this.resources = new AppearanceResources(resourcesDict); } return resources; } }
@Override public PdfResources getResources() { if (this.resources == null) { PdfDictionary resourcesDict = getPdfObject().getAsDictionary(PdfName.Resources); if (resourcesDict == null) { resourcesDict = new PdfDictionary(); getPdfObject().put(PdfName.Resources, resourcesDict); } this.resources = new AppearanceResources(resourcesDict); } return resources; } }
/** * Gets {@link PdfResources} of the Form XObject. * Note, if there is no resources, a new instance will be created. * * @return not null instance of {@link PdfResources}. */ public PdfResources getResources() { if (this.resources == null) { PdfDictionary resourcesDict = getPdfObject().getAsDictionary(PdfName.Resources); if (resourcesDict == null) { resourcesDict = new PdfDictionary(); getPdfObject().put(PdfName.Resources, resourcesDict); } this.resources = new PdfResources(resourcesDict); } return resources; }
/** * Create {@code PdfStream} based on {@code fontStreamBytes}. * * @param fontStreamBytes original font data, must be not null. * @param fontStreamLengths array to generate {@code Length*} keys, must be not null. * @return the PdfStream containing the font or {@code null}, if there is an error reading the font. * @throws PdfException Method will throw exception if {@code fontStreamBytes} is {@code null}. */ protected PdfStream getPdfFontStream(byte[] fontStreamBytes, int[] fontStreamLengths) { if (fontStreamBytes == null) { throw new PdfException(PdfException.FontEmbeddingIssue); } PdfStream fontStream = new PdfStream(fontStreamBytes); makeObjectIndirect(fontStream); for (int k = 0; k < fontStreamLengths.length; ++k) { fontStream.put(new PdfName("Length" + (k + 1)), new PdfNumber(fontStreamLengths[k])); } return fontStream; }
/** * 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; }
/** * Embed a file to a PdfDocument. * * @param doc * @param is * @param description * @param fileDisplay * @param mimeType * @param fileParameter * @param afRelationshipValue */ public static PdfFileSpec createEmbeddedFileSpec(PdfDocument doc, InputStream is, String description, String fileDisplay, PdfName mimeType, PdfDictionary fileParameter, PdfName afRelationshipValue) { PdfStream stream = new PdfStream(doc, is); PdfDictionary params = new PdfDictionary(); if (fileParameter != null) { params.mergeDifferent(fileParameter); } if (!params.containsKey(PdfName.ModDate)) { params.put(PdfName.ModDate, new PdfDate().getPdfObject()); } stream.put(PdfName.Params, params); return createEmbeddedFileSpec(doc, stream, description, fileDisplay, mimeType, afRelationshipValue); }
/** * Creates form XObject from page content. * The page shall be from the document, to which FormXObject will be added. * * @param page an instance of {@link PdfPage} */ public PdfFormXObject(PdfPage page) { this(page.getCropBox()); getPdfObject().getOutputStream().writeBytes(page.getContentBytes()); resources = new PdfResources((PdfDictionary) page.getResources().getPdfObject().clone()); getPdfObject().put(PdfName.Resources, resources.getPdfObject()); }