protected PdfStream(java.io.OutputStream outputStream) { this.outputStream = new PdfOutputStream(outputStream); this.compressionLevel = CompressionConstants.UNDEFINED_COMPRESSION; setState(MUST_BE_INDIRECT); }
/** * 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; }
/** * Close the writer and underlying streams. * * @throws IOException */ @Override public void close() throws IOException { try { super.close(); } finally { try { if (duplicateStream != null) { duplicateStream.close(); } } catch (Exception ex) { Logger logger = LoggerFactory.getLogger(PdfWriter.class); logger.error("Closing of the duplicatedStream failed.", ex); } } }
/** * Moves text by shifting text line matrix (PDF Td operator). * * @param x x coordinate. * @param y y coordinate. * @return current canvas. */ public PdfCanvas moveText(double x, double y) { contentStream.getOutputStream() .writeDouble(x) .writeSpace() .writeDouble(y).writeSpace() .writeBytes(Td); return this; }
private void addToPropertiesAndBeginLayer(IPdfOCG layer) { PdfName name = resources.addProperties(layer.getPdfObject()); contentStream.getOutputStream().write(PdfName.OC).writeSpace() .write(name).writeSpace().writeBytes(BDC).writeNewLine(); }
private byte[] getDebugBytes() throws IOException { if (duplicateStream != null) { duplicateStream.flush(); return ((ByteArrayOutputStream) (duplicateStream.getOutputStream())).toByteArray(); } else { return null; } }
boolean allowCompression = !pdfStream.containsKey(PdfName.Filter) && isNotMetadataPdfStream(pdfStream); updateCompressionFilter(pdfStream); fout = def = new DeflaterOutputStream(fout, pdfStream.getCompressionLevel(), 0x8000); this.write((PdfDictionary) pdfStream); writeBytes(PdfOutputStream.stream); long beginStreamContent = getCurrentPos(); byte[] buf = new byte[4192]; while (true) { length.setValue((int) (getCurrentPos() - beginStreamContent)); pdfStream.updateLength(length.intValue()); writeBytes(PdfOutputStream.endstream); } else { bytes = decodeFlateBytes(pdfStream, bytes); pdfStream.getOutputStream().write(bytes); if (toCompress && !containsFlateFilter(pdfStream) && (allowCompression || userDefinedCompression)) { // compress updateCompressionFilter(pdfStream); byteArrayStream = new ByteArrayOutputStream(); DeflaterOutputStream zip = new DeflaterOutputStream(byteArrayStream, pdfStream.getCompressionLevel()); if (pdfStream instanceof PdfObjectStream) { PdfObjectStream objectStream = (PdfObjectStream) pdfStream; ((ByteArrayOutputStream) objectStream.getIndexStream().getOutputStream()).writeTo(zip); ((ByteArrayOutputStream) objectStream.getOutputStream().getOutputStream()).writeTo(zip); } else {
/** * Sets the word spacing parameter. * * @param wordSpacing a parameter * @return current canvas. */ public PdfCanvas setWordSpacing(float wordSpacing) { currentGs.setWordSpacing(wordSpacing); contentStream.getOutputStream() .writeFloat(wordSpacing).writeSpace() .writeBytes(Tw); return this; }
/** * Moves to the start of the next line. * * @return current canvas. */ public PdfCanvas newlineText() { contentStream.getOutputStream() .writeBytes(TStar); return this; }
/** * Changes the value of the <VAR>line dash pattern</VAR>. * <br> * The line dash pattern controls the pattern of dashes and gaps used to stroke paths. * It is specified by an <I>array</I> and a <I>phase</I>. The array specifies the length * of the alternating dashes and gaps. The phase specifies the distance into the dash * pattern to start the dash. * * @param phase the value of the phase * @param unitsOn the number of units that must be 'on' * @param unitsOff the number of units that must be 'off' * @return current canvas. */ public PdfCanvas setLineDash(float unitsOn, float unitsOff, float phase) { currentGs.setDashPattern(getDashPatternArray(new float[]{unitsOn, unitsOff}, phase)); contentStream.getOutputStream().writeByte('[').writeFloat(unitsOn).writeSpace() .writeFloat(unitsOff).writeByte(']').writeSpace() .writeFloat(phase).writeSpace() .writeBytes(d); return this; }
PdfIndirectReference reference = xrefTable.get(i); if (reference.isFree()) { xrefStream.getOutputStream().write(0); xrefStream.getOutputStream().write(reference.getOffset(), offsetSize); xrefStream.getOutputStream().write(reference.getGenNumber(), 2); } else if (reference.getObjStreamNumber() == 0) { xrefStream.getOutputStream().write(1); xrefStream.getOutputStream().write(reference.getOffset(), offsetSize); xrefStream.getOutputStream().write(reference.getGenNumber(), 2); } else { xrefStream.getOutputStream().write(2); xrefStream.getOutputStream().write(reference.getObjStreamNumber(), offsetSize); xrefStream.getOutputStream().write(reference.getIndex(), 2); int first = (int) sections.get(k); int len = (int) sections.get(k + 1); writer.writeInteger(first).writeSpace().writeInteger(len).writeByte((byte) '\n'); for (int i = first; i < first + len; i++) { PdfIndirectReference reference = xrefTable.get(i); writer.writeString(off.substring(off.length() - 10, off.length())).writeSpace(). writeString(gen.substring(gen.length() - 5, gen.length())).writeSpace(); if (reference.isFree()) { writer.writeBytes(freeXRefEntry); writeLong(startxref). writeString("\n%%EOF\n"); xref = null; freeReferencesLinkedList.clear();
range[range.length - 1] = bout.length - range[range.length - 2]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); PdfOutputStream os = new PdfOutputStream(bos); os.write('['); for (int k = 0; k < range.length; ++k) { os.writeLong(range[k]).write(' '); os.write(']'); System.arraycopy(bos.toByteArray(), 0, bout, (int) byteRangePosition, (int) bos.size()); } else { range[range.length - 1] = len - range[range.length - 2]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); PdfOutputStream os = new PdfOutputStream(bos); os.write('['); for (int k = 0; k < range.length; ++k) { os.writeLong(range[k]).write(' '); os.write(']'); raf.seek(byteRangePosition); raf.write(bos.toByteArray(), 0, (int) bos.size());
/** * Adds object to the object stream. * * @param object object to add. */ public void addObject(PdfObject object) { if (size.intValue() == MAX_OBJ_STREAM_SIZE) { throw new PdfException(PdfException.PdfObjectStreamReachMaxSize); } PdfOutputStream outputStream = getOutputStream(); indexStream.writeInteger(object.getIndirectReference().getObjNumber()). writeSpace(). writeLong(outputStream.getCurrentPos()). writeSpace(); outputStream.write(object); object.getIndirectReference().setObjStreamNumber(getIndirectReference().getObjNumber()); object.getIndirectReference().setIndex(size.intValue()); outputStream.writeSpace(); size.increment(); getAsNumber(PdfName.First).setValue(indexStream.getCurrentPos()); }
/** * Sets text rendering mode. * * @param textRenderingMode text rendering mode @see PdfCanvasConstants. * @return current canvas. */ public PdfCanvas setTextRenderingMode(int textRenderingMode) { currentGs.setTextRenderingMode(textRenderingMode); contentStream.getOutputStream() .writeInteger(textRenderingMode).writeSpace() .writeBytes(Tr); 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(PdfIndirectReference indirectReference) { if (document != null && !indirectReference.getDocument().equals(document)) { throw new PdfException(PdfException.PdfIndirectObjectBelongsToOtherPdfDocument); } if (indirectReference.isFree()) { Logger logger = LoggerFactory.getLogger(PdfOutputStream.class); logger.error(LogMessageConstant.FLUSHED_OBJECT_CONTAINS_FREE_REFERENCE); write(PdfNull.PDF_NULL); } else if (indirectReference.getRefersTo() == null) { Logger logger = LoggerFactory.getLogger(PdfOutputStream.class); logger.error(LogMessageConstant.FLUSHED_OBJECT_CONTAINS_REFERENCE_WHICH_NOT_REFER_TO_ANY_OBJECT); write(PdfNull.PDF_NULL); } else if (indirectReference.getGenNumber() == 0) { writeInteger(indirectReference.getObjNumber()). writeBytes(endIndirectWithZeroGenNr); } else { writeInteger(indirectReference.getObjNumber()). writeSpace(). writeInteger(indirectReference.getGenNumber()). writeBytes(endIndirect); } }
outputStream = new PdfOutputStream(new ByteArrayOutputStream()); throw new PdfException(PdfException.CannotReadAStreamInOrderToAppendNewBytes, ex); outputStream.assignBytes(oldBytes, oldBytes.length); outputStream.writeBytes(bytes); outputStream.assignBytes(bytes, bytes.length); } else { outputStream.reset();
/** * 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); }
/** * 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; } }
throw new PdfException(PdfException.DocumentMustBePreClosed); ByteArrayOutputStream bous = new ByteArrayOutputStream(); PdfOutputStream os = new PdfOutputStream(bous); throw new IllegalArgumentException("The key didn't reserve space in preclose"); bous.reset(); os.write(obj); if (bous.size() > lit.getBytesCount()) throw new IllegalArgumentException("The key is too big");