/** * Gets decoded stream bytes. * * @return byte[] */ public byte[] getBytes() { return getBytes(true); }
/** * Creates a Type3Glyph canvas with a non-empty Content Stream. * * @param pdfStream {@code PdfStream} from existed document. * @param document document to which {@code PdfStream} belongs. */ Type3Glyph(PdfStream pdfStream, PdfDocument document) { super(pdfStream, null, document); if (pdfStream.getBytes() != null) { fillBBFromBytes(pdfStream.getBytes()); } }
/** * Gets decoded bytes of a certain stream of a page content. * * @param index index of stream inside Content. * @return byte array. * @throws PdfException in case of any {@link IOException}. */ public byte[] getStreamBytes(int index) { return getContentStream(index).getBytes(); }
public byte[] getDocumentBytes() { return getPdfObject().getBytes(); }
private void initXfaForm(PdfObject xfa) throws IOException, ParserConfigurationException, SAXException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); if (xfa.isArray()) { PdfArray ar = (PdfArray) xfa; for (int k = 1; k < ar.size(); k += 2) { PdfObject ob = ar.get(k); if (ob instanceof PdfStream) { byte[] b = ((PdfStream) ob).getBytes(); bout.write(b); } } } else if (xfa instanceof PdfStream) { byte[] b = ((PdfStream) xfa).getBytes(); bout.write(b); } bout.close(); initXfaForm(new ByteArrayInputStream(bout.toByteArray())); }
private void initXfaForm(PdfObject xfa) throws IOException, ParserConfigurationException, SAXException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); if (xfa.isArray()) { PdfArray ar = (PdfArray) xfa; for (int k = 1; k < ar.size(); k += 2) { PdfObject ob = ar.get(k); if (ob instanceof PdfStream) { byte[] b = ((PdfStream) ob).getBytes(); bout.write(b); } } } else if (xfa instanceof PdfStream) { byte[] b = ((PdfStream) xfa).getBytes(); bout.write(b); } bout.close(); initXfaForm(new ByteArrayInputStream(bout.toByteArray())); }
private void setCheckerOutputIntent(PdfDictionary outputIntent) { if (outputIntent != null) { PdfStream destOutputProfile = outputIntent.getAsStream(PdfName.DestOutputProfile); if (destOutputProfile != null) { String intentCS = IccProfile.getIccColorSpaceName(destOutputProfile.getBytes()); this.pdfAOutputIntentColorSpace = intentCS; } } } }
private void setCheckerOutputIntent(PdfDictionary outputIntent) { if (outputIntent != null) { PdfStream destOutputProfile = outputIntent.getAsStream(PdfName.DestOutputProfile); if (destOutputProfile != null) { String intentCS = IccProfile.getIccColorSpaceName(destOutputProfile.getBytes()); this.pdfAOutputIntentColorSpace = intentCS; } } } }
/** * Writes (part of) the XFA resource to an OutputStream. * If key is {@code null}, the complete resource is written; * if key refers to an individual package, this package only is * written to the OutputStream. * * @param os the OutputStream to which the XML is written. * @throws IOException usual exception when there's a problem writing to an OutputStream */ @SuppressWarnings("unchecked") public void writeTo(OutputStream os) throws IOException { Enumeration<TreeNode> children = this.children(); FormTreeNode node; PdfStream stream; while (children.hasMoreElements()) { node = (FormTreeNode) children.nextElement(); stream = (PdfStream) node.getCorrespondingPdfObjectNode().getPdfObject(); os.write(stream.getBytes()); } os.flush(); os.close(); }
@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); } }
/** * Gets a list of X509CRL objects from a Document Security Store. * @return a list of CRLs * @throws GeneralSecurityException * @throws IOException */ public List<X509CRL> getCRLsFromDSS() throws GeneralSecurityException, IOException { List<X509CRL> crls = new ArrayList<>(); if (dss == null) return crls; PdfArray crlarray = dss.getAsArray(PdfName.CRLs); if (crlarray == null) return crls; for (int i = 0; i < crlarray.size(); i++) { PdfStream stream = crlarray.getAsStream(i); crls.add((X509CRL) SignUtils.parseCrlFromStream(new ByteArrayInputStream(stream.getBytes()))); } return crls; }
/** * Gets a list of X509CRL objects from a Document Security Store. * @return a list of CRLs * @throws GeneralSecurityException * @throws IOException */ public List<X509CRL> getCRLsFromDSS() throws GeneralSecurityException, IOException { List<X509CRL> crls = new ArrayList<>(); if (dss == null) return crls; PdfArray crlarray = dss.getAsArray(PdfName.CRLs); if (crlarray == null) return crls; for (int i = 0; i < crlarray.size(); i++) { PdfStream stream = crlarray.getAsStream(i); crls.add((X509CRL) SignUtils.parseCrlFromStream(new ByteArrayInputStream(stream.getBytes()))); } return crls; }
static CMapToUnicode processToUnicode(PdfObject toUnicode) { CMapToUnicode cMapToUnicode = null; if (toUnicode instanceof PdfStream) { try { byte[] uniBytes = ((PdfStream) toUnicode).getBytes(); ICMapLocation lb = new CMapLocationFromBytes(uniBytes); cMapToUnicode = new CMapToUnicode(); CMapParser.parseCid("", cMapToUnicode, lb); } catch (Exception e) { Logger logger = LoggerFactory.getLogger(CMapToUnicode.class); logger.error(LogMessageConstant.UNKNOWN_ERROR_WHILE_PROCESSING_CMAP); cMapToUnicode = CMapToUnicode.EmptyCMapToUnicodeMap; } } else if (PdfName.IdentityH.equals(toUnicode)) { cMapToUnicode = CMapToUnicode.getIdentity(); } return cMapToUnicode; }
/** * Gets the current value contained in the form field. * * @return the current value, as a {@link String} */ public String getValueAsString() { PdfObject value = getPdfObject().get(PdfName.V); if (value == null) { return ""; } else if (value instanceof PdfStream) { return new String(((PdfStream) value).getBytes()); } else if (value instanceof PdfName) { return ((PdfName) value).getValue(); } else if (value instanceof PdfString) { return ((PdfString) value).toUnicodeString(); } else { return ""; } }
/** * Gets the current value contained in the form field. * * @return the current value, as a {@link String} */ public String getValueAsString() { PdfObject value = getPdfObject().get(PdfName.V); if (value == null) { return ""; } else if (value instanceof PdfStream) { return new String(((PdfStream) value).getBytes()); } else if (value instanceof PdfName) { return ((PdfName) value).getValue(); } else if (value instanceof PdfString) { return ((PdfString) value).toUnicodeString(); } else { return ""; } }
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; }
private static CMapEncoding createCMap(PdfObject cmap, String uniMap) { if (cmap.isStream()) { PdfStream cmapStream = (PdfStream) cmap; byte[] cmapBytes = cmapStream.getBytes(); return new CMapEncoding(cmapStream.getAsName(PdfName.CMapName).getValue(), cmapBytes); } else { String cmapName = ((PdfName) cmap).getValue(); if (PdfEncodings.IDENTITY_H.equals(cmapName) || PdfEncodings.IDENTITY_V.equals(cmapName)) { return new CMapEncoding(cmapName); } else { return new CMapEncoding(cmapName, uniMap); } } }
/** * Generate default appearance, /DA key. * * @param font preferred font. If {@link #getFont()} is not null, it will be used instead. * @param fontSize preferred font size. If {@link PdfFormField#fontSize} is valid, * it will be used instead. * @return generated string */ protected String generateDefaultAppearanceString(PdfFont font, float fontSize, Color color, PdfResources res) { if (this.fontSize >= 0) { fontSize = this.fontSize; } if (this.font != null) { font = this.font; } PdfStream stream = new PdfStream(); PdfCanvas canvas = new PdfCanvas(stream, res, getDocument()); canvas.setFontAndSize(font, fontSize); if (color != null) canvas.setColor(color, true); return new String(stream.getBytes()); }
/** * 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); }
/** * @deprecated use {@link #drawPdfA2CheckAppearance(float, float, String, int)} instead. */ @Deprecated protected void drawPdfA1CheckAppearance(float width, float height, String selectedValue, int checkType) { 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); this.checkType = checkType; drawBorder(canvas, xObject, width, height); drawPdfACheckBox(canvas, width, height, !"Off".equals(selectedValue)); xObject.getPdfObject().getOutputStream().writeBytes(stream.getBytes()); PdfDictionary normalAppearance = new PdfDictionary(); normalAppearance.put(new PdfName(selectedValue), xObject.getPdfObject()); PdfDictionary mk = new PdfDictionary(); mk.put(PdfName.CA, new PdfString(text)); PdfWidgetAnnotation widget = getWidgets().get(0); widget.put(PdfName.MK, mk); widget.setNormalAppearance(normalAppearance); }