List of usage examples for com.itextpdf.text.pdf PdfDictionary getAsDict
public PdfDictionary getAsDict(final PdfName key)
PdfObject
as a PdfDictionary
, resolving indirect references. From source file:com.cib.statementstamper.windows.StatementStamperMainWindow.java
License:Open Source License
protected ByteArrayOutputStream doStamper(ByteArrayOutputStream baos) throws IOException, DocumentException { map.clear();/* ww w.jav a 2s . c o m*/ ByteArrayOutputStream baosFinal = new ByteArrayOutputStream(); PdfReader reader = new PdfReader(baos.toByteArray()); PdfContentStreamProcessor processor = new PdfContentStreamProcessor(this); for (int i = 1; i <= reader.getNumberOfPages(); i++) { actualPage = i; PdfDictionary pageDic = reader.getPageN(i); PdfDictionary resourcesDic = pageDic.getAsDict(PdfName.RESOURCES); processor.processContent(ContentByteUtils.getContentBytesForPage(reader, i), resourcesDic); } Document newDocument = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(newDocument, baosFinal); newDocument.open(); PdfContentByte canvas = writer.getDirectContent(); // Font myFont = FontFactory.getFont(FontFactory.COURIER, 7, Font.BOLD); Iterator<Entry<Integer, Map<Float, StringBuffer>>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<Integer, Map<Float, StringBuffer>> pairs = (Map.Entry<Integer, Map<Float, StringBuffer>>) it .next(); Iterator<Entry<Float, StringBuffer>> iter = pairs.getValue().entrySet().iterator(); while (iter.hasNext()) { Map.Entry<Float, StringBuffer> actualEntry = iter.next(); canvas.beginText(); canvas.setFontAndSize(myFontBase, 7); canvas.showTextAligned(Element.ALIGN_LEFT, actualEntry.getValue().toString(), 25, actualEntry.getKey() + 60, 0); canvas.endText(); } newDocument.newPage(); } newDocument.close(); reader = new PdfReader(baosFinal.toByteArray()); PdfReaderContentParser parser = new PdfReaderContentParser(reader); TextMarginFinder finder; for (int i = 1; i <= reader.getNumberOfPages(); i++) { finder = parser.processContent(i, new TextMarginFinder()); if (finder.getLly() <= 68) { getWindow().showNotification("Hiba", "A(z) " + i + ".ik oldalon tl sok a szveg !!!", Notification.TYPE_ERROR_MESSAGE); return null; } } reader = new PdfReader(baosFinal.toByteArray()); PdfStamper stamper = new PdfStamper(reader, baosFinal); int n = reader.getNumberOfPages(); for (int i = 1; i <= n; i++) { PdfContentByte overContent = stamper.getOverContent(i); overContent.addImage(logo, 131, 0, 0, 32, 44, 775); getFooterTable(i, n).writeSelectedRows(0, -1, 27, 68, stamper.getOverContent(i)); // ez a jo getIspLogoTable(i, n).writeSelectedRows(0, -1, 425, 45, stamper.getOverContent(i)); } stamper.close(); reader.close(); return baosFinal; }
From source file:com.poet.ar.remover.AnnotationRemover.java
/** * remove content that matches keywords/*from w w w. j a v a 2 s . c o m*/ * * @param page * @return count of removed content */ private static int doRemoveContent(PdfDictionary page) { // all contents in page i PdfArray contentArray = page.getAsArray(PdfName.CONTENTS); PdfDictionary resources = page.getAsDict(PdfName.RESOURCES); List<Integer> willRemovedIx = new ArrayList<Integer>(); if (contentArray != null) { PdfStream stream = null; for (int i = 0; i < contentArray.size(); i++) { stream = contentArray.getAsStream(i); PRStream pr = (PRStream) stream; // TODO // FIXME: 2016/1/27 0027 java.lang.ClassCastException: com.itextpdf.text.pdf.PdfArray cannot be cast to com.itextpdf.text.pdf.PdfLiteral // get display text // String text = StreamContentExtractor.extractFromPdfStream(stream, resources); // // if (keywords.contains(text)) { // willRemovedIx.add(i); // } try { String text = StreamContentExtractor.extractFromPdfStream(stream, resources); if (keywords.contains(text)) { willRemovedIx.add(i); } } catch (Exception ex) { } } int i = 0; for (Integer ix : willRemovedIx) { contentArray.remove(ix - i++); } } return willRemovedIx.size(); }
From source file:com.poet.ar.remover.AnnotationRemover.java
/** * remove annotation that matches keywords * * @param page/*w w w . jav a 2 s.c om*/ * @return count of removed annotations */ private static int doRemoveAnnotation(PdfDictionary page) { // all annotations in page i PdfArray annoArray = page.getAsArray(PdfName.ANNOTS); List<Integer> willRemovedIx = new ArrayList<Integer>(); if (annoArray != null) { PdfDictionary annotation = null; PdfDictionary a = null; PdfString uri = null; for (int i = 0; i < annoArray.size(); i++) { annotation = annoArray.getAsDict(i); if (annotation == null) { continue; } a = annotation.getAsDict(PdfName.A); if (a == null) { continue; } uri = a.getAsString(PdfName.URI); if (uri == null) { continue; } String uriStr = uri.toString().trim(); if (keywords.contains(uriStr)) { willRemovedIx.add(i); } } int i = 0; for (Integer ix : willRemovedIx) { annoArray.remove(ix - i++); } } return willRemovedIx.size(); }
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. ja va 2 s . co 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:io.konik.carriage.itext.ITextInvoiceExtractor.java
License:Open Source License
private static PdfDictionary getValidEf(PdfDictionary fileSpec) { if (fileSpec.contains(EF)) { return fileSpec.getAsDict(EF); }/*from w w w .j av a 2 s .co m*/ throw new InvoiceExtractionError("Pdf catalog does not contain Valid EF Entry"); }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
private void cleanUpPage(int pageNum, List<PdfCleanUpLocation> cleanUpLocations) throws IOException, DocumentException { if (cleanUpLocations.size() == 0) { return;// w ww . j a va 2 s . c o m } PdfReader pdfReader = pdfStamper.getReader(); PdfDictionary page = pdfReader.getPageN(pageNum); PdfContentByte canvas = pdfStamper.getUnderContent(pageNum); byte[] pageContentInput = ContentByteUtils.getContentBytesForPage(pdfReader, pageNum); page.remove(PdfName.CONTENTS); canvas.saveState(); PdfCleanUpRegionFilter filter = createFilter(cleanUpLocations); PdfCleanUpRenderListener pdfCleanUpRenderListener = new PdfCleanUpRenderListener(pdfStamper, filter); pdfCleanUpRenderListener.registerNewContext(pdfReader.getPageResources(page), canvas); PdfContentStreamProcessor contentProcessor = new PdfContentStreamProcessor(pdfCleanUpRenderListener); PdfCleanUpContentOperator.populateOperators(contentProcessor, pdfCleanUpRenderListener); contentProcessor.processContent(pageContentInput, page.getAsDict(PdfName.RESOURCES)); pdfCleanUpRenderListener.popContext(); canvas.restoreState(); colorCleanedLocations(canvas, cleanUpLocations); if (redactAnnotIndirRefs != null) { // if it isn't null, then we are in "extract locations from redact annots" mode deleteRedactAnnots(pageNum); } }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
private PdfName generateNameForXObj(PdfDictionary pageDict) { PdfDictionary resourcesDict = pageDict.getAsDict(PdfName.RESOURCES); PdfDictionary xobjDict = resourcesDict.getAsDict(PdfName.XOBJECT); if (xobjDict != null) { for (PdfName xobjName : xobjDict.getKeys()) { int xobjNum = getXObjNum(xobjName); if (currentXObjNum <= xobjNum) { currentXObjNum = xobjNum + 1; }//from ww w . j ava2 s.co m } } return new PdfName(XOBJ_NAME_PREFIX + currentXObjNum++); }
From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java
License:Open Source License
/** * Tead a JBIG2 image and give a BufferedImage * @param image//from www .ja 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.roda.common.certification.PDFSignatureUtils.java
private static StringBuilder getExtractionInformation(AcroFields fields, ArrayList<?> names, Path outputContents, String filename) throws IOException { StringBuilder sb = new StringBuilder(); for (int i = 0; i < names.size(); i++) { String name = (String) names.get(i); Item item = fields.getFieldItem(name); PdfDictionary widget = item.getWidget(0); PdfDictionary infoDictionary = widget.getAsDict(PdfName.V); sb.append("<signature>\n"); try {//from w w w . ja v a 2 s . co m PdfPKCS7 pk = fields.verifySignature(name); sb = addElementToExtractionResult(sb, "name", name); sb = addElementToExtractionResult(sb, "sign-name", pk.getSignName()); sb = addElementToExtractionResult(sb, "version", Integer.toString(pk.getVersion())); sb = addElementToExtractionResult(sb, "reason", pk.getReason()); sb = addElementToExtractionResult(sb, "location", pk.getLocation()); SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); if (pk.getTimeStampDate() != null) { String timestamp = formatter.format(pk.getTimeStampDate().getTime()); sb = addElementToExtractionResult(sb, "timestamp-time", timestamp); } if (pk.getSignDate() != null) { String sign = formatter.format(pk.getSignDate().getTime()); sb = addElementToExtractionResult(sb, "sign-time", sign); } sb = addElementToExtractionResult(sb, "digest-algorithm", pk.getDigestAlgorithm()); sb = addElementToExtractionResult(sb, "hash-algorithm", pk.getHashAlgorithm()); sb = addElementToExtractionResult(sb, "covers-whole-document", Boolean.toString(fields.signatureCoversWholeDocument(name))); sb = addElementToExtractionResult(sb, "ft", widget.get(PdfName.FT).toString()); if (infoDictionary.contains(PdfName.CONTACTINFO)) sb = addElementToExtractionResult(sb, "contact-info", infoDictionary.getAsString(PdfName.CONTACTINFO).toString()); if (infoDictionary.contains(PdfName.FILTER)) sb = addElementToExtractionResult(sb, "filter", infoDictionary.get(PdfName.FILTER).toString()); if (infoDictionary.contains(PdfName.SUBFILTER)) sb = addElementToExtractionResult(sb, "subfilter", infoDictionary.get(PdfName.SUBFILTER).toString()); if (infoDictionary.contains(PdfName.LOCK)) sb = addElementToExtractionResult(sb, "lock", "true"); if (infoDictionary.contains(PdfName.CONTENTS)) { PdfString elementName = infoDictionary.getAsString(PdfName.CONTENTS); Files.write(outputContents, elementName.toUnicodeString().getBytes()); sb = addElementToExtractionResult(sb, "contents", filename + ".pkcs7"); } } catch (NoSuchFieldError e) { LOGGER.warn("DS information extraction did not execute properly"); } sb.append("</signature>"); } return sb; }
From source file:org.roda.core.plugins.plugins.characterization.PDFSignatureUtils.java
private static StringBuilder getExtractionInformation(AcroFields fields, ArrayList<?> names, Path outputContents, String filename) throws IOException { StringBuilder sb = new StringBuilder(); for (int i = 0; i < names.size(); i++) { String name = (String) names.get(i); Item item = fields.getFieldItem(name); PdfDictionary widget = item.getWidget(0); PdfDictionary infoDictionary = widget.getAsDict(PdfName.V); sb.append("<signature>\n"); try {// ww w . j av a2 s. c o m PdfPKCS7 pk = fields.verifySignature(name); sb = addElementToExtractionResult(sb, "name", name); sb = addElementToExtractionResult(sb, "sign-name", pk.getSignName()); sb = addElementToExtractionResult(sb, "version", Integer.toString(pk.getVersion())); sb = addElementToExtractionResult(sb, "reason", pk.getReason()); sb = addElementToExtractionResult(sb, "location", pk.getLocation()); SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); if (pk.getTimeStampDate() != null) { String timestamp = formatter.format(pk.getTimeStampDate().getTime()); sb = addElementToExtractionResult(sb, "timestamp-time", timestamp); } if (pk.getSignDate() != null) { String sign = formatter.format(pk.getSignDate().getTime()); sb = addElementToExtractionResult(sb, "sign-time", sign); } sb = addElementToExtractionResult(sb, "digest-algorithm", pk.getDigestAlgorithm()); sb = addElementToExtractionResult(sb, "hash-algorithm", pk.getHashAlgorithm()); sb = addElementToExtractionResult(sb, "covers-whole-document", Boolean.toString(fields.signatureCoversWholeDocument(name))); sb = addElementToExtractionResult(sb, "ft", widget.get(PdfName.FT).toString()); if (infoDictionary.contains(PdfName.CONTACTINFO)) sb = addElementToExtractionResult(sb, "contact-info", infoDictionary.getAsString(PdfName.CONTACTINFO).toString()); if (infoDictionary.contains(PdfName.FILTER)) sb = addElementToExtractionResult(sb, "filter", infoDictionary.get(PdfName.FILTER).toString()); if (infoDictionary.contains(PdfName.SUBFILTER)) sb = addElementToExtractionResult(sb, "subfilter", infoDictionary.get(PdfName.SUBFILTER).toString()); if (infoDictionary.contains(PdfName.LOCK)) sb = addElementToExtractionResult(sb, "lock", "true"); if (infoDictionary.contains(PdfName.CONTENTS)) { PdfString elementName = infoDictionary.getAsString(PdfName.CONTENTS); Files.write(outputContents, elementName.toUnicodeString().getBytes()); sb = addElementToExtractionResult(sb, "contents", filename + ".pkcs7"); } } catch (NoSuchFieldError e) { LOGGER.warn("DS information extraction did not execute properly"); } sb.append("</signature>"); } return sb; }