private PdfStream ensureStreamDataIsReadyToBeProcessed(PdfStream stream) { if (!stream.isFlushed()) { if (stream.getOutputStream() == null || stream.containsKey(PdfName.Filter)) { try { stream.setData(stream.getBytes()); } catch (Exception ex) { // ignore } } } return stream; }
@Override protected void checkImage(PdfStream image, PdfDictionary currentColorSpaces) { PdfColorSpace colorSpace = null; if (isAlreadyChecked(image)) { colorSpace = checkedObjectsColorspace.get(image); checkColorSpace(colorSpace, currentColorSpaces, true, null); return; } PdfObject colorSpaceObj = image.get(PdfName.ColorSpace); if (colorSpaceObj != null) { colorSpace = PdfColorSpace.makeColorSpace(colorSpaceObj); checkColorSpace(colorSpace, currentColorSpaces, true, null); checkedObjectsColorspace.put(image, colorSpace); } if (image.containsKey(PdfName.Alternates)) { throw new PdfAConformanceException(PdfAConformanceException.AN_IMAGE_DICTIONARY_SHALL_NOT_CONTAIN_ALTERNATES_KEY); } if (image.containsKey(PdfName.OPI)) { throw new PdfAConformanceException(PdfAConformanceException.AN_IMAGE_DICTIONARY_SHALL_NOT_CONTAIN_OPI_KEY); } if (image.containsKey(PdfName.Interpolate) && (boolean) image.getAsBool(PdfName.Interpolate)) { throw new PdfAConformanceException(PdfAConformanceException.THE_VALUE_OF_INTERPOLATE_KEY_SHALL_BE_FALSE); } checkRenderingIntent(image.getAsName(PdfName.Intent)); if (image.containsKey(PdfName.SMask) && !PdfName.None.equals(image.getAsName(PdfName.SMask))) { throw new PdfAConformanceException(PdfAConformanceException.THE_SMASK_KEY_IS_NOT_ALLOWED_IN_XOBJECTS); } }
/** * Creates a Type3Glyph canvas with a new Content Stream. * * @param pdfDocument the document that this canvas is created for */ Type3Glyph(PdfDocument pdfDocument, float wx, float llx, float lly, float urx, float ury, boolean isColor) { super((PdfStream)new PdfStream().makeIndirect(pdfDocument), null, pdfDocument); writeMetrics(wx, llx, lly, urx, ury, isColor); }
/** * Draws the appearance of a radio button with a specified value. * * @param width the width of the radio button to draw * @param height the height of the radio button to draw * @param value the value of the button * @deprecated Please, use {@link #drawRadioAppearance(float, float, String)} instead. */ @Deprecated protected void drawPdfA1RadioAppearance(float width, float height, String value) { PdfStream stream = (PdfStream) new PdfStream().makeIndirect(getDocument()); PdfCanvas canvas = new PdfCanvas(stream, new PdfResources(), getDocument()); Rectangle rect = new Rectangle(0, 0, width, height); PdfFormXObject xObject = new PdfFormXObject(rect); drawBorder(canvas, xObject, width, height); drawRadioField(canvas, rect.getWidth(), rect.getHeight(), !"Off".equals(value)); PdfDictionary normalAppearance = new PdfDictionary(); normalAppearance.put(new PdfName(value), xObject.getPdfObject()); PdfWidgetAnnotation widget = getWidgets().get(0); xObject.getPdfObject().getOutputStream().writeBytes(stream.getBytes()); widget.setNormalAppearance(normalAppearance); }
@Override protected void checkPdfStream(PdfStream stream) { if (stream.containsKey(PdfName.F) || stream.containsKey(PdfName.FFilter) || stream.containsKey(PdfName.FDecodeParams)) { throw new PdfAConformanceException(PdfAConformanceException.STREAM_OBJECT_DICTIONARY_SHALL_NOT_CONTAIN_THE_F_FFILTER_OR_FDECODEPARAMS_KEYS); PdfObject filter = stream.get(PdfName.Filter); if (filter instanceof PdfName) { if (filter.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); if (filter.equals(PdfName.Crypt)) { PdfDictionary decodeParams = stream.getAsDictionary(PdfName.DecodeParms); if (decodeParams != null) { PdfName cryptFilterName = decodeParams.getAsName(PdfName.Name); throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); if (f.equals(PdfName.Crypt)) { PdfArray decodeParams = stream.getAsArray(PdfName.DecodeParms); if (decodeParams != null && i < decodeParams.size()) { PdfDictionary decodeParam = decodeParams.getAsDictionary(i);
private void write(PdfStream pdfStream) { try { boolean userDefinedCompression = pdfStream.getCompressionLevel() != CompressionConstants.UNDEFINED_COMPRESSION; if (!userDefinedCompression) { int defaultCompressionLevel = document != null ? document.getWriter().getCompressionLevel() : CompressionConstants.DEFAULT_COMPRESSION; pdfStream.setCompressionLevel(defaultCompressionLevel); boolean toCompress = pdfStream.getCompressionLevel() != CompressionConstants.NO_COMPRESSION; boolean allowCompression = !pdfStream.containsKey(PdfName.Filter) && isNotMetadataPdfStream(pdfStream); if (pdfStream.getInputStream() != null) { java.io.OutputStream fout = this; DeflaterOutputStream def = null; fout = def = new DeflaterOutputStream(fout, pdfStream.getCompressionLevel(), 0x8000); byte[] buf = new byte[4192]; while (true) { int n = pdfStream.getInputStream().read(buf); if (n <= 0) break; PdfNumber length = pdfStream.getAsNumber(PdfName.Length); length.setValue((int) (getCurrentPos() - beginStreamContent)); pdfStream.updateLength(length.intValue()); writeBytes(PdfOutputStream.endstream); } else {
/** * Gets decoded stream bytes. * * @return byte[] */ public byte[] getBytes() { return getBytes(true); }
@Override protected void checkFormXObject(PdfStream form) { if (isAlreadyChecked(form)) return; if (form.containsKey(PdfName.OPI)) { throw new PdfAConformanceException(PdfAConformanceException.A_FORM_XOBJECT_DICTIONARY_SHALL_NOT_CONTAIN_OPI_KEY); } if (form.containsKey(PdfName.PS)) { throw new PdfAConformanceException(PdfAConformanceException.A_FORM_XOBJECT_DICTIONARY_SHALL_NOT_CONTAIN_PS_KEY); } if (PdfName.PS.equals(form.getAsName(PdfName.Subtype2))) { throw new PdfAConformanceException(PdfAConformanceException.A_FORM_XOBJECT_DICTIONARY_SHALL_NOT_CONTAIN_SUBTYPE2_KEY_WITH_A_VALUE_OF_PS); } if (form.containsKey(PdfName.SMask) && !PdfName.None.equals(form.getAsName(PdfName.SMask))) { throw new PdfAConformanceException(PdfAConformanceException.THE_SMASK_KEY_IS_NOT_ALLOWED_IN_XOBJECTS); } if (form.containsKey(PdfName.Group) && PdfName.Transparency.equals(form.getAsDictionary(PdfName.Group).getAsName(PdfName.S))) { throw new PdfAConformanceException(PdfAConformanceException.A_GROUP_OBJECT_WITH_AN_S_KEY_WITH_A_VALUE_OF_TRANSPARENCY_SHALL_NOT_BE_INCLUDED_IN_A_FORM_XOBJECT); } checkResources(form.getAsDictionary(PdfName.Resources)); }
stream = new PdfStream(image.getData()); String filter = image.getFilter(); if (filter != null && filter.equals("JPXDecode") && image.getColorSpace() <= 0) { stream.setCompressionLevel(CompressionConstants.NO_COMPRESSION); image.setBpc(0); stream.put(PdfName.Type, PdfName.XObject); stream.put(PdfName.Subtype, PdfName.Image); PdfDictionary decodeParms = createDictionaryFromMap(stream, image.getDecodeParms()); if (decodeParms != null) { stream.put(PdfName.DecodeParms, decodeParms); stream.put(PdfName.ColorSpace, colorSpace); stream.put(PdfName.BitsPerComponent, new PdfNumber(image.getBpc())); stream.put(PdfName.Filter, new PdfName(image.getFilter())); stream.remove(PdfName.ColorSpace); stream.putAll(additional); iccBasedColorSpace.add(PdfName.ICCBased); iccBasedColorSpace.add(iccProfileStream); PdfObject colorSpaceObject = stream.get(PdfName.ColorSpace); boolean iccProfileShouldBeApplied = true; if (colorSpaceObject != null) { iccProfileShouldBeApplied = false;
/** * Embed a file to a PdfDocument. * * @param doc PdfDocument to add the file to * @param fileStore byte[] containing the file * @param description file description * @param fileDisplay actual file name stored in the pdf * @param mimeType mime-type of the file * @param fileParameter Pdfdictionary containing fil parameters * @param afRelationshipValue AFRelationship key value, @see AFRelationshipValue. If <CODE>null</CODE>, @see AFRelationshipValue.Unspecified will be added. * @return PdfFileSpec containing the file specification of the file as Pdfobject */ public static PdfFileSpec createEmbeddedFileSpec(PdfDocument doc, byte[] fileStore, String description, String fileDisplay, PdfName mimeType, PdfDictionary fileParameter, PdfName afRelationshipValue) { PdfStream stream = (PdfStream)new PdfStream(fileStore).makeIndirect(doc); PdfDictionary params = new PdfDictionary(); if (fileParameter != null) { params.mergeDifferent(fileParameter); } if (!params.containsKey(PdfName.ModDate)) { params.put(PdfName.ModDate, new PdfDate().getPdfObject()); } if (fileStore != null) { params.put(PdfName.Size, new PdfNumber(stream.getBytes().length)); } stream.put(PdfName.Params, params); return createEmbeddedFileSpec(doc, stream, description, fileDisplay, mimeType, afRelationshipValue); }
/** * 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; }
private byte[] decodeTiffAndPngBytes(byte[] imageBytes) throws IOException { PdfObject colorspace = getPdfObject().get(PdfName.ColorSpace); prepareAndFindColorspace(colorspace); java.io.ByteArrayOutputStream ms = new java.io.ByteArrayOutputStream(); throw new com.itextpdf.io.IOException(com.itextpdf.io.IOException.ColorSpaceIsNotSupported).setMessageParams(tyca.toString()); PdfStream pr = (PdfStream) ca.get(1); int n = pr.getAsNumber(PdfName.N).intValue(); if (n != 4) { throw new com.itextpdf.io.IOException(com.itextpdf.io.IOException.NValueIsNotSupported).setMessageParams(n); icc = pr.getBytes(); } else if (!PdfName.DeviceCMYK.equals(colorspace)) { throw new com.itextpdf.io.IOException(com.itextpdf.io.IOException.ColorSpaceIsNotSupported).setMessageParams(colorspace.toString()); } else { PngWriter png = new PngWriter(ms); PdfArray decode = getPdfObject().getAsArray(PdfName.Decode); if (decode != null) { if (pngBitDepth == 1) {
PdfDictionary vri = new PdfDictionary(); for (byte[] b : validated.get(vkey).crls) { PdfStream ps = new PdfStream(b); ps.setCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION); ps.makeIndirect(document); crl.add(ps); crls.add(ps); PdfStream ps = new PdfStream(b); ps.setCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION); ocsp.add(ps); ocsps.add(ps); PdfStream ps = new PdfStream(b); ps.setCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION); ps.makeIndirect(document); cert.add(ps); certs.add(ps);
PdfStream xObjStream = (PdfStream) xObject; PdfObject subtype = null; boolean isFlushed = xObjStream.isFlushed(); if (!isFlushed) { subtype = xObjStream.get(PdfName.Subtype); PdfStream pStream = (PdfStream) p; if (!isAlreadyChecked(pStream)) { checkResources(pStream.getAsDictionary(PdfName.Resources));
PdfStream tStream = new PdfStream(serializeDocument(form.templateNode)); tStream.setCompressionLevel(document.getWriter().getCompressionLevel()); ar.set(t, tStream); PdfStream dStream = new PdfStream(serializeDocument(form.datasetsNode)); dStream.setCompressionLevel(document.getWriter().getCompressionLevel()); ar.set(d, dStream); ar.setModified(); PdfStream stream = new PdfStream(serializeDocument(form.domDocument)); stream.setCompressionLevel(document.getWriter().getCompressionLevel()); stream.flush(); acroForm.put(PdfName.XFA, stream); acroForm.setModified();
public void handleXObject(PdfCanvasProcessor processor, Stack<CanvasTag> canvasTagHierarchy, PdfStream xObjectStream, PdfName xObjectName) { PdfDictionary resourcesDic = xObjectStream.getAsDictionary(PdfName.Resources); PdfResources resources; if (resourcesDic == null) { resources = processor.getResources(); } else { resources = new PdfResources(resourcesDic); } // we read the content bytes up here so if it fails we don't leave the graphics state stack corrupted // this is probably not necessary (if we fail on this, probably the entire content stream processing // operation should be rejected byte[] contentBytes; contentBytes = xObjectStream.getBytes(); final PdfArray matrix = xObjectStream.getAsArray(PdfName.Matrix); new PushGraphicsStateOperator().invoke(processor, null, null); if (matrix != null) { float a = matrix.getAsNumber(0).floatValue(); float b = matrix.getAsNumber(1).floatValue(); float c = matrix.getAsNumber(2).floatValue(); float d = matrix.getAsNumber(3).floatValue(); float e = matrix.getAsNumber(4).floatValue(); float f = matrix.getAsNumber(5).floatValue(); Matrix formMatrix = new Matrix(a, b, c, d, e, f); processor.getGraphicsState().updateCtm(formMatrix); } processor.processContent(contentBytes, resources); new PopGraphicsStateOperator().invoke(processor, null, null); } }
@Override protected void checkPdfStream(PdfStream stream) { if (stream.containsKey(PdfName.F) || stream.containsKey(PdfName.FFilter) || stream.containsKey(PdfName.FDecodeParams)) { throw new PdfAConformanceException(PdfAConformanceException.STREAM_OBJECT_DICTIONARY_SHALL_NOT_CONTAIN_THE_F_FFILTER_OR_FDECODEPARAMS_KEYS); } PdfObject filter = stream.get(PdfName.Filter); if (filter instanceof PdfName) { if (filter.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } else if (filter instanceof PdfArray) { for (PdfObject f : ((PdfArray) filter)) { if (f.equals(PdfName.LZWDecode)) throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } } }
/** * To manually flush a {@code PdfObject} behind this wrapper, you have to ensure * that this object is added to the document, i.e. it has an indirect reference. * Basically this means that before flushing you need to explicitly call {@link #makeIndirect(PdfDocument)}. * For example: wrapperInstance.makeIndirect(document).flush(); * Note that not every wrapper require this, only those that have such warning in documentation. */ @Override public void flush() { resources = null; if (getPdfObject().get(PdfName.BBox) == null) { throw new PdfException(PdfException.FormXObjectMustHaveBbox); } super.flush(); }
/** * Gets separation color names of trap network appearance, {@link PdfName#SeparationColorNames} key. * * @return an {@link PdfArray} of names identifying the colorants. */ public PdfArray getSeparationColorNames() { return getPdfObject().getAsArray(PdfName.SeparationColorNames); }