List of usage examples for com.itextpdf.text.pdf PdfReader getStreamBytes
public static byte[] getStreamBytes(final PRStream stream) throws IOException
From source file:de.rub.dez6a3.jpdfsigner.control.JPodPDFViewer.java
License:Open Source License
@Override public ArrayList getAttachments() throws IOException { ArrayList files = new ArrayList(); PdfReader reader = new PdfReader(conf.getPDFFile()); PdfDictionary root = reader.getCatalog(); PdfDictionary documentnames = root.getAsDict(PdfName.NAMES); PdfDictionary embeddedfiles = documentnames.getAsDict(PdfName.EMBEDDEDFILES); PdfArray filespecs = embeddedfiles.getAsArray(PdfName.NAMES); PdfDictionary filespec;/* w w w .j av a2s. c o m*/ PdfDictionary refs; for (int i = 0; i < filespecs.size();) { filespecs.getAsName(i++); filespec = filespecs.getAsDict(i++); refs = filespec.getAsDict(PdfName.EF); Iterator it = refs.getKeys().iterator(); while (it.hasNext()) { PdfName key = (PdfName) it.next(); if (key.toString().equals("/F")) { String filename = "-"; String desc = "-"; int size = -1; String moddate = "-"; String compsize = "-"; PdfObject pdfobj = null; try { filename = filespec.getAsString(key).toString(); } catch (Exception e) { log.warn("Cannot load attachment-name - " + e.getMessage()); } try { desc = filespec.getAsString(PdfName.DESC).toString(); } catch (Exception e) { log.warn("Cannot load attachment-description - " + e.getMessage()); } byte[] attBytes = null; try { PRStream stream = (PRStream) PdfReader.getPdfObject(refs.getAsIndirectObject(key)); attBytes = PdfReader.getStreamBytes(stream); size = attBytes.length; } catch (Exception e) { log.warn("Cannot load attachment-size - " + e.getMessage()); } try { pdfobj = PdfReader.getPdfObject(refs.getAsIndirectObject(key)); } catch (Exception e) { log.warn("Cannot load attachment-pdfobject - " + e.getMessage()); } Hashtable fileData = new Hashtable(); fileData.put(ATTACHMENT_FILENAME_STRING, filename); //filename fileData.put(ATTACHMENT_DESCRIPTION_STRING, desc); //Description fileData.put(ATTACHMENT_SIZE_INT, size); //size fileData.put(ATTACHMENT_BYTES_ARR, attBytes); //bytes files.add(fileData); } } } return files; }
From source file:de.rub.dez6a3.jpdfsigner.view.GUIAttachmentBar.java
License:Open Source License
public void loadAttachments() { tfield = new RSyntaxTextArea(); labelDesc = new JLabel(); tfield.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML); tfield.setFont(new Font("Arial", Font.PLAIN, 13)); tfield.setTextAntiAliasHint("VALUE_TEXT_ANTIALIAS_ON"); tfield.setHyperlinksEnabled(true);//from w w w .j a va 2 s . c om tfield.setEditable(false); allIcons = new ArrayList<AttachmentsBGLayer>(); JScrollPane scroller = new JScrollPane(); scroller.setBackground(new Color(0, 0, 0, 0)); scroller.setOpaque(false); scroller.setBorder(null); scroller.setViewportBorder(null); JPanel content = new JPanel() { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; GradientPaint gp = new GradientPaint(getWidth() / 3, 0, LAFProperties.getInstance().getBluePanelColorE(), getWidth(), 0, LAFProperties.getInstance().getBluePanelColorW()); g2.setPaint(gp); g2.fillRect(0, 0, getWidth(), getHeight()); g2.setColor(LAFProperties.getInstance().getBluePanelHighlightColor()); Polygon hl = new Polygon(); hl.addPoint(200, 0); hl.addPoint(getWidth(), 0); hl.addPoint(getWidth(), getHeight()); hl.addPoint(135, getHeight()); g2.fillPolygon(hl); g2.setColor(LAFProperties.getInstance().getBorderColor()); g2.drawLine(0, 0, getWidth(), 0); g2.setColor(LAFProperties.getInstance().getBluePanelBorderHighlightColor()); g2.drawLine(0, 1, getWidth(), 1); super.paint(g); } }; Dimension contentDim = new Dimension(50, 110); content.setPreferredSize(contentDim); content.setSize(contentDim); content.setOpaque(false); content.setBackground(new Color(0, 0, 0, 0)); content.setLayout(new FlowLayout(FlowLayout.CENTER)); scroller.setViewportView(content); content.setAlignmentX(0); setLayout(new BorderLayout()); add(scroller, BorderLayout.CENTER); try { ArrayList<Hashtable> attachments = pdfHandler.getAttachments(); attachmentCount = attachments.size(); int col = 0; int count = 0; //um zu entscheidne wenn der letzte separator angefgt wird (ganz unten soll keiner hin) for (Hashtable currAttachment : attachments) { PRStream stream = (PRStream) currAttachment.get("pdfobject"); byte[] contentBytes = PdfReader.getStreamBytes(stream); String stringAttach = new String(contentBytes); if (currAttachment.get("filename").equals("out.xml") && attachmentCount == 1) { String parsedXML = ""; try { parsedXML = parseXML(stringAttach); signable = true; } catch (Exception e) { parsedXML = stringAttach; } tfield.setText(parsedXML); labelDesc.setText(currAttachment.get("description").toString()); } JLabel separator = new JLabel(new ImageIcon( getClass().getResource("/de/rub/dez6a3/jpdfsigner/resources/images/seperator.png"))); Image scaled = new ImageIcon( getClass().getResource("/de/rub/dez6a3/jpdfsigner/resources/images/attachment.png")) .getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH); AttachmentsBGLayer panel = new AttachmentsBGLayer(new ImageIcon(scaled), currAttachment.get("filename").toString(), currAttachment.get("description").toString(), stringAttach); allIcons.add(panel); content.add(panel); col++; count++; if (count < attachments.size()) { content.add(separator); } col++; } } catch (IOException ex) { } }
From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java
License:Open Source License
/** * Tead a JBIG2 image and give a BufferedImage * @param image//from w w w .j a va 2 s. c o m * @return The BufferedImage obj */ public static BufferedImage readJBIG2(PdfImageObject image) { BufferedImage buffImg = null; PdfDictionary dic = image.getDictionary(); PdfDictionary decodedic = dic.getAsDict(PdfName.DECODEPARMS); PdfStream globalStream = decodedic.getAsStream(PdfName.JBIG2GLOBALS); try { byte[] byteArrayGlobal = PdfReader.getStreamBytes((PRStream) globalStream); InputStream in = new ByteArrayInputStream(image.getImageAsBytes()); ImageInputStream stream = ImageIO.createImageInputStream(in); InputStream inG = new ByteArrayInputStream(byteArrayGlobal); ImageInputStream streamG = ImageIO.createImageInputStream(inG); JBIG2ImageReader reader = new JBIG2ImageReader(new JBIG2ImageReaderSpi()); reader.setInput(stream); JBIG2Globals globals = reader.processGlobals(streamG); reader.setGlobals(globals); ImageReadParam param = reader.getDefaultReadParam(); buffImg = reader.read(0, param); in.close(); inG.close(); } catch (Exception e) { logger.error("Exception", e); PdfTrickMessages.append("ERROR", Consts.SENDLOG_MSG); } return buffImg; }
From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java
License:Open Source License
/** * Read a png image with if all other method fails * @param ref/*from w w w .j a v a2 s .c o m*/ * @param resultFile * @return The BufferedImage obj * @throws IOException * @throws ImageReadException */ public static BufferedImage readIndexedPNG(int ref, String resultFile) throws IOException, ImageReadException { PdfReader reader = new PdfReader(resultFile); PRStream stream = (PRStream) reader.getPdfObject(ref); PdfDictionary dic = stream; byte[] content = PdfReader.getStreamBytesRaw(stream); int width = dic.getAsNumber(PdfName.WIDTH).intValue(); int height = dic.getAsNumber(PdfName.HEIGHT).intValue(); int pngBitDepth = dic.getAsNumber(PdfName.BITSPERCOMPONENT).intValue(); PdfObject colorspace = dic.getDirectObject(PdfName.COLORSPACE); PdfArray decode = dic.getAsArray(PdfName.DECODE); PdfArray carray = (PdfArray) colorspace; PdfObject id2 = carray.getDirectObject(3); byte[] palette = null; if (id2 instanceof PdfString) { palette = ((PdfString) id2).getBytes(); } else if (id2 instanceof PRStream) { palette = PdfReader.getStreamBytes(((PRStream) id2)); } Map<PdfName, FilterHandlers.FilterHandler> handlers = new HashMap<PdfName, FilterHandlers.FilterHandler>( FilterHandlers.getDefaultFilterHandlers()); byte[] imageBytes = PdfReader.decodeBytes(content, dic, handlers); int stride = (width * pngBitDepth + 7) / 8; ByteArrayOutputStream ms = new ByteArrayOutputStream(); PngWriter png = new PngWriter(ms); if (decode != null) { if (pngBitDepth == 1) { // if the decode array is 1,0, then we need to invert the image if (decode.getAsNumber(0).intValue() == 1 && decode.getAsNumber(1).intValue() == 0) { int len = imageBytes.length; for (int t = 0; t < len; ++t) { imageBytes[t] ^= 0xff; } } else { // if the decode array is 0,1, do nothing. It's possible that the array could be 0,0 or 1,1 - but that would be silly, so we'll just ignore that case } } else { // todo: add decode transformation for other depths } } int pngColorType = 0; png.writeHeader(width, height, pngBitDepth, pngColorType); if (palette != null) { png.writePalette(palette); } png.writeData(imageBytes, stride); png.writeEnd(); imageBytes = ms.toByteArray(); InputStream in = new ByteArrayInputStream(imageBytes); ImageInputStream ima_stream = ImageIO.createImageInputStream(in); BufferedImage buffImg = null; BufferedImage buffPic = ImageIO.read(ima_stream); // check if image contains a mask image ... experimental for this type of image BufferedImage buffMask = null; PRStream maskStream = (PRStream) dic.getAsStream(PdfName.SMASK); if (maskStream != null) { PdfImageObject maskImage = new PdfImageObject(maskStream); buffMask = maskImage.getBufferedImage(); Image img = PdfTrickUtils.TransformGrayToTransparency(buffMask); buffImg = PdfTrickUtils.ApplyTransparency(buffPic, img); } else { buffImg = buffPic; } reader.close(); ms.close(); in.close(); return buffImg; }
From source file:org.sejda.impl.itext5.component.PdfUnpacker.java
License:Open Source License
private File copyToTemporaryFile(PRStream prs) throws TaskIOException { File tmpFile = createTemporaryBuffer(); LOG.debug("Created output temporary buffer {}", tmpFile); ByteArrayInputStream inputStream = null; try {/* w ww.j a v a 2 s .c o m*/ inputStream = new ByteArrayInputStream(PdfReader.getStreamBytes(prs)); FileUtils.copyInputStreamToFile(inputStream, tmpFile); LOG.debug("Attachment unpacked to temporary buffer"); } catch (IOException e) { throw new TaskIOException("Unable to copy attachment to temporary file.", e); } finally { IOUtils.closeQuietly(inputStream); } return tmpFile; }