/** * Returns the stream. * {@inheritDoc} */ @Override public final COSStream getCOSObject() { return stream.getCOSObject(); }
/** * Returns the stream. * @return The stream for this object. */ @Override public COSDictionary getCOSObject() { if (functionStream != null) { return functionStream.getCOSObject(); } else { return functionDictionary; } }
/** * Sets the metadata stream that is associated with this color space. * @param metadata the new metadata stream */ public void setMetadata(COSStream metadata) { stream.getCOSObject().setItem(COSName.METADATA, metadata); }
/** * Returns the metadata stream for this object, or null if there is no metadata stream. * @return the metadata stream, or null if there is none */ public COSStream getMetadata() { return (COSStream)stream.getCOSObject().getDictionaryObject(COSName.METADATA); }
/** * Sets the number of color components. * @param n the number of color components */ // TODO it's probably not safe to use this @Deprecated public void setNumberOfComponents(int n) { numberOfComponents = n; stream.getCOSObject().setInt(COSName.N, n); }
@Override public int getNumberOfComponents() { if (numberOfComponents < 0) { numberOfComponents = stream.getCOSObject().getInt(COSName.N); } return numberOfComponents; }
/** * Creates a new XObject from the given stream and subtype. * @param stream The stream to read. * @param subtype */ protected PDXObject(PDStream stream, COSName subtype) { this.stream = stream; // could be used for writing: stream.getCOSObject().setName(COSName.TYPE, COSName.XOBJECT.getName()); stream.getCOSObject().setName(COSName.SUBTYPE, subtype.getName()); }
@Override public boolean isEmpty() { return getStream().getCOSObject().getLength() == 0; }
/** * Sets the list of alternateColorSpace color spaces. * * @param list the list of color space objects */ public void setAlternateColorSpaces(List<PDColorSpace> list) { COSArray altArray = null; if(list != null) { altArray = COSArrayList.converterToCOSArray(list); } stream.getCOSObject().setItem(COSName.ALTERNATE, altArray); }
/** * Creates a new XObject of the given subtype for writing. * @param document The document in which to create the XObject. * @param subtype The subtype of the new XObject. */ protected PDXObject(PDDocument document, COSName subtype) { stream = new PDStream(document); stream.getCOSObject().setName(COSName.TYPE, COSName.XOBJECT.getName()); stream.getCOSObject().setName(COSName.SUBTYPE, subtype.getName()); }
/** * Constructor. * * @param function The function stream. * */ public PDFunction( COSBase function ) { if (function instanceof COSStream) { functionStream = new PDStream( (COSStream)function ); functionStream.getCOSObject().setItem( COSName.TYPE, COSName.FUNCTION ); } else if (function instanceof COSDictionary) { functionDictionary = (COSDictionary)function; } }
private PDStream configureOutputProfile(PDDocument doc, InputStream colorProfile) throws IOException { ICC_Profile icc = ICC_Profile.getInstance(colorProfile); PDStream stream = new PDStream(doc, new ByteArrayInputStream(icc.getData()), COSName.FLATE_DECODE); stream.getCOSObject().setInt(COSName.N, icc.getNumComponents()); return stream; } }
/** * Creates an Image XObject with the given stream as its contents and current color spaces. This * constructor is for internal PDFBox use and is not for PDF generation. Users who want to * create images should look at {@link #createFromFileByExtension(File, PDDocument) }. * * @param stream the XObject stream to read * @param resources the current resources * @throws java.io.IOException if there is an error creating the XObject. */ public PDImageXObject(PDStream stream, PDResources resources) throws IOException { super(stream, COSName.IMAGE); this.resources = resources; List<COSName> filters = stream.getFilters(); if (filters != null && !filters.isEmpty() && COSName.JPX_DECODE.equals(filters.get(filters.size()-1))) { try (COSInputStream is = stream.createInputStream()) { DecodeResult decodeResult = is.getDecodeResult(); stream.getCOSObject().addAll(decodeResult.getParameters()); this.colorSpace = decodeResult.getJPXColorSpace(); } } }
/** * Returns the range for a certain component number. * This will never return null. * If it is not present then the range 0..1 will be returned. * @param n the component number to get the range for * @return the range for this component */ public PDRange getRangeForComponent(int n) { COSArray rangeArray = (COSArray) stream.getCOSObject().getDictionaryObject(COSName.RANGE); if (rangeArray == null || rangeArray.size() < getNumberOfComponents() * 2) { return new PDRange(); // 0..1 } return new PDRange(rangeArray, n); }
@Override public PDStream extractFontFile(PDFontDescriptor fontDescriptor) { PDStream ff2 = fontDescriptor.getFontFile2(); if (ff2 != null) { // Stream validation should be done by the StreamValidateHelper. Process font specific check COSStream stream = ff2.getCOSObject(); if (stream == null) { this.fContainer.push(new ValidationError(ERROR_FONTS_FONT_FILEX_INVALID, fontDescriptor.getFontName() + ": The FontFile is missing")); this.fContainer.notEmbedded(); } } checkCIDSet(fontDescriptor); return ff2; }
public final void buildFontFile2(InputStream ttfStream) throws IOException { PDStream stream = new PDStream(document, ttfStream, COSName.FLATE_DECODE); // as the stream was closed within the PDStream constructor, we have to recreate it try (InputStream input = stream.createInputStream()) { ttf = new TTFParser().parseEmbedded(input); if (!isEmbeddingPermitted(ttf)) { throw new IOException("This font does not permit embedding"); } if (fontDescriptor == null) { fontDescriptor = createFontDescriptor(ttf); } } stream.getCOSObject().setLong(COSName.LENGTH1, ttf.getOriginalDataSize()); fontDescriptor.setFontFile2(stream); }
@Override public PDStream extractFontFile(PDFontDescriptor fontDescriptor) { PDStream ff3 = fontDescriptor.getFontFile3(); if (ff3 != null) { // Stream validation should be done by the StreamValidateHelper. Process font specific check COSStream stream = ff3.getCOSObject(); if (stream == null) { this.fContainer.push(new ValidationError(ERROR_FONTS_FONT_FILEX_INVALID, fontDescriptor.getFontName() + ": The FontFile is missing")); this.fContainer.notEmbedded(); } else { // Length1/2/3 aren't mandatory for this type of font // But the Subtype is a mandatory field with specific values String st = stream.getNameAsString(COSName.SUBTYPE); if (!(FONT_DICTIONARY_VALUE_TYPE0C.equals(st) || FONT_DICTIONARY_VALUE_TYPE1C.equals(st))) { this.fContainer.push(new ValidationError(ERROR_FONTS_FONT_FILEX_INVALID, fontDescriptor.getFontName() + ": invalid /Subtype /" + st + " in /FontFile3 stream")); } checkCIDSet(fontDescriptor); } } return ff3; }
/** * Sets the range for this color space. * @param range the new range for the a component * @param n the component to set the range for */ public void setRangeForComponent(PDRange range, int n) { COSArray rangeArray = (COSArray) stream.getCOSObject().getDictionaryObject(COSName.RANGE); if (rangeArray == null) { rangeArray = new COSArray(); stream.getCOSObject().setItem(COSName.RANGE, rangeArray); } // extend range array with default values if needed while (rangeArray.size() < (n + 1) * 2) { rangeArray.add(new COSFloat(0)); rangeArray.add(new COSFloat(1)); } rangeArray.set(n*2, new COSFloat(range.getMin())); rangeArray.set(n*2+1, new COSFloat(range.getMax())); }
/** * Creates a new TrueType font for embedding. */ TrueTypeEmbedder(PDDocument document, COSDictionary dict, TrueTypeFont ttf, boolean embedSubset) throws IOException { this.document = document; this.embedSubset = embedSubset; this.ttf = ttf; fontDescriptor = createFontDescriptor(ttf); if (!isEmbeddingPermitted(ttf)) { throw new IOException("This font does not permit embedding"); } if (!embedSubset) { // full embedding PDStream stream = new PDStream(document, ttf.getOriginalData(), COSName.FLATE_DECODE); stream.getCOSObject().setLong(COSName.LENGTH1, ttf.getOriginalDataSize()); fontDescriptor.setFontFile2(stream); } dict.setName(COSName.BASE_FONT, ttf.getName()); // choose a Unicode "cmap" cmap = ttf.getUnicodeCmap(); cmapLookup = ttf.getUnicodeCmapLookup(); }
@Override public PDStream extractFontFile(PDFontDescriptor fontDescriptor) { PDStream fontFile = fontDescriptor.getFontFile2(); COSStream stream = (fontFile == null ? null : fontFile.getCOSObject()); if (stream == null) { this.fContainer.push(new ValidationError(ERROR_FONTS_FONT_FILEX_INVALID, fontDescriptor.getFontName() + ": The FontFile2 is missing")); this.fContainer.notEmbedded(); return null; } if (stream.getInt(COSName.LENGTH1) <= 0) { this.fContainer.push(new ValidationError(ERROR_FONTS_FONT_FILEX_INVALID, fontDescriptor.getFontName() + ": The FontFile entry /Length1 is invalid")); return null; } return fontFile; }