List of usage examples for org.apache.pdfbox.cos COSName getName
public String getName()
From source file:PrintImageLocations.java
License:Apache License
protected void processOperator(PDFOperator operator, List arguments) throws IOException { String operation = operator.getOperation(); if (INVOKE_OPERATOR.equals(operation)) { COSName objectName = (COSName) arguments.get(0); Map<String, PDXObject> xobjects = getResources().getXObjects(); PDXObject xobject = (PDXObject) xobjects.get(objectName.getName()); if (xobject instanceof PDXObjectImage) { PDXObjectImage image = (PDXObjectImage) xobject; PDPage page = getCurrentPage(); int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double pageHeight = page.getMediaBox().getHeight(); flagimg = true;// ww w . j a va2 s . co m System.out.println("*******************************************************************"); System.out.println("Found image [" + objectName.getName() + "]"); Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix(); float yScaling = ctmNew.getYScale(); float angle = (float) Math.acos(ctmNew.getValue(0, 0) / ctmNew.getXScale()); if (ctmNew.getValue(0, 1) < 0 && ctmNew.getValue(1, 0) > 0) { angle = (-1) * angle; } ctmNew.setValue(2, 1, (float) (pageHeight - ctmNew.getYPosition() - Math.cos(angle) * yScaling)); ctmNew.setValue(2, 0, (float) (ctmNew.getXPosition() - Math.sin(angle) * yScaling)); // because of the moved 0,0-reference, we have to shear in the opposite direction ctmNew.setValue(0, 1, (-1) * ctmNew.getValue(0, 1)); ctmNew.setValue(1, 0, (-1) * ctmNew.getValue(1, 0)); AffineTransform ctmAT = ctmNew.createAffineTransform(); ctmAT.scale(1f / imageWidth, 1f / imageHeight); float imageXScale = ctmNew.getXScale(); float imageYScale = ctmNew.getYScale(); System.out.println("position = " + ctmNew.getXPosition() + ", " + ctmNew.getYPosition()); fl[i] = ctmNew.getYPosition();//important xxx[i] = ctmNew.getXPosition(); System.out.println("y value is " + fl[i]); // size in pixel System.out.println("size = " + imageWidth + "px, " + imageHeight + "px"); // size in page units System.out.println("size = " + imageXScale + ", " + imageYScale); // size in inches imageXScale /= 72; imageYScale /= 72; System.out.println("size = " + imageXScale + "in, " + imageYScale + "in"); // size in millimeter imageXScale *= 25.4; imageYScale *= 25.4; System.out.println("size = " + imageXScale + "mm, " + imageYScale + "mm"); widtharray[i] = (imageXScale / 10); heightarray[i] = (imageYScale / 10); i++; System.out.println(); } else if (xobject instanceof PDXObjectForm) { // save the graphics state getGraphicsStack().push((PDGraphicsState) getGraphicsState().clone()); PDPage page = getCurrentPage(); PDXObjectForm form = (PDXObjectForm) xobject; COSStream invoke = (COSStream) form.getCOSObject(); PDResources pdResources = form.getResources(); if (pdResources == null) { pdResources = page.findResources(); } // if there is an optional form matrix, we have to // map the form space to the user space Matrix matrix = form.getMatrix(); if (matrix != null) { Matrix xobjectCTM = matrix.multiply(getGraphicsState().getCurrentTransformationMatrix()); getGraphicsState().setCurrentTransformationMatrix(xobjectCTM); } processSubStream(page, pdResources, invoke); // restore the graphics state setGraphicsState((PDGraphicsState) getGraphicsStack().pop()); } } else { super.processOperator(operator, arguments); } }
From source file:DecodePlate.java
License:Open Source License
private static void printCOSObject(String prefix, String name, COSBase obj) { String n;/*from www .j a va2s.c om*/ if (seenObjects.containsKey(obj)) { n = seenObjects.get(obj); String val = ""; if ((obj instanceof COSFloat) || (obj instanceof COSInteger) || (obj instanceof COSName) || (obj instanceof COSString)) { val = " " + obj.toString(); } System.out.println(spacer + prefix + name + ": " + n + val); return; } n = Integer.toString(seenObjects.size() + 1); while (n.length() < numdigs) n = "0" + n; n = "<" + n + "> "; seenObjects.put(obj, n); if (obj instanceof COSArray) { System.out.println(n + prefix + name + ": COSArray {"); int i = 0; for (COSBase elem : (COSArray) obj) { printCOSObject(prefix + " ", "[" + i + "]", elem); i++; } System.out.println(n + prefix + "}"); } else if (obj instanceof COSDictionary) { System.out.println(n + prefix + name + ": COSDictionary {"); Set<Map.Entry<COSName, COSBase>> entries = ((COSDictionary) obj).entrySet(); for (Map.Entry<COSName, COSBase> entry : entries) { COSName key = entry.getKey(); COSBase val = entry.getValue(); printCOSObject(prefix + " ", key.getName(), val); } System.out.println(n + prefix + "}"); } else if (obj instanceof COSObject) { System.out.println(n + prefix + name + ": " + obj.toString()); COSBase encap = ((COSObject) obj).getObject(); if (encap != null) { printCOSObject(prefix + " ", "encap", encap); } } else { System.out.println(n + prefix + name + ": " + obj.toString()); } }
From source file:ShowSignature.java
License:Apache License
private void showSignature(String[] args) throws IOException, CertificateException { if (args.length != 2) { usage();/*from www. j a va2 s . com*/ } else { String password = args[0]; String infile = args[1]; PDDocument document = null; try { document = PDDocument.load(new File(infile), password); if (!document.isEncrypted()) { System.err.println("Warning: Document is not encrypted."); } COSDictionary trailer = document.getDocument().getTrailer(); COSDictionary root = (COSDictionary) trailer.getDictionaryObject(COSName.ROOT); COSDictionary acroForm = (COSDictionary) root.getDictionaryObject(COSName.ACRO_FORM); COSArray fields = (COSArray) acroForm.getDictionaryObject(COSName.FIELDS); for (int i = 0; i < fields.size(); i++) { COSDictionary field = (COSDictionary) fields.getObject(i); COSName type = field.getCOSName(COSName.FT); if (COSName.SIG.equals(type)) { COSDictionary cert = (COSDictionary) field.getDictionaryObject(COSName.V); if (cert != null) { System.out.println("Certificate found"); System.out.println("Name=" + cert.getDictionaryObject(COSName.NAME)); System.out.println("Modified=" + cert.getDictionaryObject(COSName.M)); COSName subFilter = (COSName) cert.getDictionaryObject(COSName.SUB_FILTER); if (subFilter != null) { if (subFilter.getName().equals("adbe.x509.rsa_sha1")) { COSString certString = (COSString) cert .getDictionaryObject(COSName.getPDFName("Cert")); byte[] certData = certString.getBytes(); CertificateFactory factory = CertificateFactory.getInstance("X.509"); ByteArrayInputStream certStream = new ByteArrayInputStream(certData); Collection<? extends Certificate> certs = factory .generateCertificates(certStream); System.out.println("certs=" + certs); } else if (subFilter.getName().equals("adbe.pkcs7.sha1")) { COSString certString = (COSString) cert.getDictionaryObject(COSName.CONTENTS); byte[] certData = certString.getBytes(); CertificateFactory factory = CertificateFactory.getInstance("X.509"); ByteArrayInputStream certStream = new ByteArrayInputStream(certData); Collection<? extends Certificate> certs = factory .generateCertificates(certStream); System.out.println("certs=" + certs); } else { System.err.println("Unknown certificate type:" + subFilter); } } else { throw new IOException("Missing subfilter for cert dictionary"); } } else { System.out.println("Signature found, but no certificate"); } } } } finally { if (document != null) { document.close(); } } } }
From source file:at.gv.egiz.pdfas.lib.impl.pdfbox.placeholder.SignaturePlaceholderExtractor.java
License:EUPL
@Override protected void processOperator(PDFOperator operator, List<COSBase> arguments) throws IOException { String operation = operator.getOperation(); if (operation.equals("Do")) { COSName objectName = (COSName) arguments.get(0); Map<?, ?> xobjects = getResources().getXObjects(); PDXObject xobject = (PDXObject) xobjects.get(objectName.getName()); if (xobject instanceof PDXObjectImage) { try { PDXObjectImage image = (PDXObjectImage) xobject; SignaturePlaceholderData data = checkImage(image); if (data != null) { PDPage page = getCurrentPage(); Matrix ctm = getGraphicsState().getCurrentTransformationMatrix(); int pageRotation = page.findRotation(); pageRotation = pageRotation % 360; double rotationInRadians = Math.toRadians(pageRotation);//(page.findRotation() * Math.PI) / 180; AffineTransform rotation = new AffineTransform(); rotation.setToRotation(rotationInRadians); AffineTransform rotationInverse = rotation.createInverse(); Matrix rotationInverseMatrix = new Matrix(); rotationInverseMatrix.setFromAffineTransform(rotationInverse); Matrix rotationMatrix = new Matrix(); rotationMatrix.setFromAffineTransform(rotation); Matrix unrotatedCTM = ctm.multiply(rotationInverseMatrix); float x = unrotatedCTM.getXPosition(); float yPos = unrotatedCTM.getYPosition(); float yScale = unrotatedCTM.getYScale(); float y = yPos + yScale; float w = unrotatedCTM.getXScale(); logger.debug("Page height: {}", page.findCropBox().getHeight()); logger.debug("Page width: {}", page.findCropBox().getWidth()); if (pageRotation == 90) { y = page.findCropBox().getWidth() - (y * (-1)); } else if (pageRotation == 180) { x = page.findCropBox().getWidth() + x; y = page.findCropBox().getHeight() - (y * (-1)); } else if (pageRotation == 270) { x = page.findCropBox().getHeight() + x; }/*from ww w . j av a2 s . c om*/ String posString = "p:" + currentPage + ";x:" + x + ";y:" + y + ";w:" + w; logger.debug("Found Placeholder at: {}", posString); try { data.setTablePos(new TablePos(posString)); data.setPlaceholderName(objectName.getName()); placeholders.add(data); } catch (PdfAsException e) { throw new WrappedIOException(e); } } } catch (NoninvertibleTransformException e) { throw new WrappedIOException(e); } } } else { super.processOperator(operator, arguments); } }
From source file:at.gv.egiz.pdfas.lib.impl.pdfbox.placeholder.SignaturePlaceholderExtractor.java
License:EUPL
@Override public Map<String, PDFont> getFonts() { if (fonts == null) { // at least an empty map will be returned // TODO we should return null instead of an empty map fonts = new HashMap<String, PDFont>(); if (this.getResources() != null && this.getResources().getCOSDictionary() != null) { COSDictionary fontsDictionary = (COSDictionary) this.getResources().getCOSDictionary() .getDictionaryObject(COSName.FONT); if (fontsDictionary == null) { // ignore we do not want to set anything, never when creating a signature!!!!! //fontsDictionary = new COSDictionary(); //this.getResources().getCOSDictionary().setItem(COSName.FONT, fontsDictionary); } else { for (COSName fontName : fontsDictionary.keySet()) { COSBase font = fontsDictionary.getDictionaryObject(fontName); // data-000174.pdf contains a font that is a COSArray, looks to be an error in the // PDF, we will just ignore entries that are not dictionaries. if (font instanceof COSDictionary) { PDFont newFont = null; try { newFont = PDFontFactory.createFont((COSDictionary) font); } catch (IOException exception) { logger.error("error while creating a font", exception); }//from w w w. ja v a 2 s. c o m if (newFont != null) { fonts.put(fontName.getName(), newFont); } } } } } } return fonts; }
From source file:at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignaturePlaceholderExtractor.java
License:EUPL
@Override protected void processOperator(Operator operator, List<COSBase> arguments) throws IOException { String operation = operator.getName(); if (operation.equals("Do")) { COSName objectName = (COSName) arguments.get(0); PDXObject xobject = (PDXObject) getResources().getXObject(objectName); if (xobject instanceof PDImageXObject) { try { PDImageXObject image = (PDImageXObject) xobject; SignaturePlaceholderData data = checkImage(image); if (data != null) { PDPage page = getCurrentPage(); Matrix ctm = getGraphicsState().getCurrentTransformationMatrix(); int pageRotation = page.getRotation(); pageRotation = pageRotation % 360; double rotationInRadians = Math.toRadians(pageRotation);//(page.findRotation() * Math.PI) / 180; AffineTransform rotation = new AffineTransform(); rotation.setToRotation(rotationInRadians); AffineTransform rotationInverse = rotation.createInverse(); Matrix rotationInverseMatrix = new Matrix(); rotationInverseMatrix.setFromAffineTransform(rotationInverse); Matrix rotationMatrix = new Matrix(); rotationMatrix.setFromAffineTransform(rotation); Matrix unrotatedCTM = ctm.multiply(rotationInverseMatrix); float x = unrotatedCTM.getXPosition(); float yPos = unrotatedCTM.getYPosition(); float yScale = unrotatedCTM.getScaleY(); float y = yPos + yScale; float w = unrotatedCTM.getScaleX(); ;/* w w w . ja v a 2 s.c o m*/ logger.debug("Page height: {}", page.getCropBox().getHeight()); logger.debug("Page width: {}", page.getCropBox().getWidth()); if (pageRotation == 90) { y = page.getCropBox().getWidth() - (y * (-1)); } else if (pageRotation == 180) { x = page.getCropBox().getWidth() + x; y = page.getCropBox().getHeight() - (y * (-1)); } else if (pageRotation == 270) { x = page.getCropBox().getHeight() + x; } String posString = "p:" + currentPage + ";x:" + x + ";y:" + y + ";w:" + w; logger.debug("Found Placeholder at: {}", posString); try { data.setTablePos(new TablePos(posString)); data.setPlaceholderName(objectName.getName()); placeholders.add(data); } catch (PdfAsException e) { throw new IOException(); } } } catch (NoninvertibleTransformException e) { throw new IOException(e); } } } else { super.processOperator(operator, arguments); } }
From source file:at.gv.egiz.pdfas.lib.impl.pdfbox2.placeholder.SignaturePlaceholderExtractor.java
License:EUPL
public Map<String, PDFont> getFonts() { if (fonts == null) { // at least an empty map will be returned // TODO we should return null instead of an empty map fonts = new HashMap<String, PDFont>(); if (this.getResources() != null && this.getResources().getCOSObject() != null) { COSDictionary fontsDictionary = (COSDictionary) this.getResources().getCOSObject() .getDictionaryObject(COSName.FONT); if (fontsDictionary == null) { // ignore we do not want to set anything, never when creating a signature!!!!! //fontsDictionary = new COSDictionary(); //this.getResources().getCOSDictionary().setItem(COSName.FONT, fontsDictionary); } else { for (COSName fontName : fontsDictionary.keySet()) { COSBase font = fontsDictionary.getDictionaryObject(fontName); // data-000174.pdf contains a font that is a COSArray, looks to be an error in the // PDF, we will just ignore entries that are not dictionaries. if (font instanceof COSDictionary) { PDFont newFont = null; try { newFont = PDFontFactory.createFont((COSDictionary) font); } catch (IOException exception) { logger.error("error while creating a font", exception); }//from w w w .j av a2 s .c om if (newFont != null) { fonts.put(fontName.getName(), newFont); } } } } } } return fonts; }
From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.TableDrawUtils.java
License:EUPL
private static String getFontID(PDFont font, PDResources resources) { // Iterator<java.util.Map.Entry<String, PDFont>> it = resources.getfon // .entrySet().iterator(); Iterator<COSName> it = resources.getFontNames().iterator(); while (it.hasNext()) { COSName entry = it.next(); if (entry.getName().equals(font.getName())) { return entry.getName(); }/*w w w. j a v a 2s.c o m*/ } return ""; }
From source file:chiliad.parser.pdf.extractor.image.operator.ShowImage.java
License:Apache License
@Override public void process(PDFOperator operator, List<COSBase> arguments) throws IOException { final ImageExtractor extractor = (ImageExtractor) context; final COSName objectName = (COSName) arguments.get(0); final Map<String, PDXObject> xobjects = context.getResources().getXObjects(); final PDXObject xobject = (PDXObject) xobjects.get(objectName.getName()); if (xobject instanceof PDXObjectImage) { System.out.println("ImageName=" + ((COSName) arguments.get(0)).getName()); PDXObjectImage image = (PDXObjectImage) xobject; Matrix ctmNew = extractor.getGraphicsState().getCurrentTransformationMatrix(); PDPage page = extractor.getCurrentPage(); double pageHeight = page.getMediaBox().getHeight(); extractor.addPDFImage(new PDFImage(((COSName) arguments.get(0)).getName(), image, ctmNew, pageHeight)); } else if (xobject instanceof PDXObjectForm) { // save the graphics state extractor.getGraphicsStack().push((PDGraphicsState) extractor.getGraphicsState().clone()); PDPage page = extractor.getCurrentPage(); PDXObjectForm form = (PDXObjectForm) xobject; COSStream invoke = (COSStream) form.getCOSObject(); PDResources pdResources = form.getResources(); if (pdResources == null) { pdResources = page.findResources(); }/*from www . j av a2 s .c o m*/ // if there is an optional form matrix, we have to // map the form space to the user space Matrix matrix = form.getMatrix(); if (matrix != null) { Matrix xobjectCTM = matrix.multiply(extractor.getGraphicsState().getCurrentTransformationMatrix()); extractor.getGraphicsState().setCurrentTransformationMatrix(xobjectCTM); } extractor.processSubStream(page, pdResources, invoke); // restore the graphics state extractor.setGraphicsState((PDGraphicsState) extractor.getGraphicsStack().pop()); } }
From source file:chiliad.parser.pdf.extractor.vectorgraphics.operator.Invoke.java
License:Apache License
/** * process : Do : Paint the specified XObject (section 4.7). * * @param operator The operator that is being executed. * @param arguments List/*www . j av a 2s.c o m*/ * @throws IOException If there is an error invoking the sub object. */ @Override public void process(PDFOperator operator, List<COSBase> arguments) throws IOException { VectorGraphicsExtractor extractor = (VectorGraphicsExtractor) context; PDPage page = extractor.getPage(); COSName objectName = (COSName) arguments.get(0); Map<String, PDXObject> xobjects = extractor.getResources().getXObjects(); PDXObject xobject = (PDXObject) xobjects.get(objectName.getName()); if (xobject == null) { LOG.warn("Can't find the XObject for '" + objectName.getName() + "'"); } else if (xobject instanceof PDXObjectImage) { PDXObjectImage image = (PDXObjectImage) xobject; try { if (image.getImageMask()) { // set the current non stroking colorstate, so that it can // be used to create a stencil masked image image.setStencilColor(extractor.getGraphicsState().getNonStrokingColor()); } BufferedImage awtImage = image.getRGBImage(); if (awtImage == null) { LOG.warn("getRGBImage returned NULL"); return;//TODO PKOCH } int imageWidth = awtImage.getWidth(); int imageHeight = awtImage.getHeight(); double pageHeight = extractor.getPageSize().getHeight(); LOG.debug("imageWidth: " + imageWidth + "\t\timageHeight: " + imageHeight); Matrix ctm = extractor.getGraphicsState().getCurrentTransformationMatrix(); float yScaling = ctm.getYScale(); float angle = (float) Math.acos(ctm.getValue(0, 0) / ctm.getXScale()); if (ctm.getValue(0, 1) < 0 && ctm.getValue(1, 0) > 0) { angle = (-1) * angle; } ctm.setValue(2, 1, (float) (pageHeight - ctm.getYPosition() - Math.cos(angle) * yScaling)); ctm.setValue(2, 0, (float) (ctm.getXPosition() - Math.sin(angle) * yScaling)); // because of the moved 0,0-reference, we have to shear in the opposite direction ctm.setValue(0, 1, (-1) * ctm.getValue(0, 1)); ctm.setValue(1, 0, (-1) * ctm.getValue(1, 0)); AffineTransform ctmAT = ctm.createAffineTransform(); ctmAT.scale(1f / imageWidth, 1f / imageHeight); extractor.drawImage(awtImage, ctmAT); } catch (Exception e) { LOG.error(e, e); } } else if (xobject instanceof PDXObjectForm) { // save the graphics state context.getGraphicsStack().push((PDGraphicsState) context.getGraphicsState().clone()); PDXObjectForm form = (PDXObjectForm) xobject; COSStream formContentstream = form.getCOSStream(); // find some optional resources, instead of using the current resources PDResources pdResources = form.getResources(); // if there is an optional form matrix, we have to map the form space to the user space Matrix matrix = form.getMatrix(); if (matrix != null) { Matrix xobjectCTM = matrix.multiply(context.getGraphicsState().getCurrentTransformationMatrix()); context.getGraphicsState().setCurrentTransformationMatrix(xobjectCTM); } if (form.getBBox() != null) { PDGraphicsState graphicsState = context.getGraphicsState(); PDRectangle bBox = form.getBBox(); float x1 = bBox.getLowerLeftX(); float y1 = bBox.getLowerLeftY(); float x2 = bBox.getUpperRightX(); float y2 = bBox.getUpperRightY(); Point2D p0 = extractor.transformedPoint(x1, y1); Point2D p1 = extractor.transformedPoint(x2, y1); Point2D p2 = extractor.transformedPoint(x2, y2); Point2D p3 = extractor.transformedPoint(x1, y2); GeneralPath bboxPath = new GeneralPath(); bboxPath.moveTo((float) p0.getX(), (float) p0.getY()); bboxPath.lineTo((float) p1.getX(), (float) p1.getY()); bboxPath.lineTo((float) p2.getX(), (float) p2.getY()); bboxPath.lineTo((float) p3.getX(), (float) p3.getY()); bboxPath.closePath(); Area resultClippingArea = new Area(graphicsState.getCurrentClippingPath()); Area newArea = new Area(bboxPath); resultClippingArea.intersect(newArea); graphicsState.setCurrentClippingPath(resultClippingArea); } getContext().processSubStream(page, pdResources, formContentstream); // restore the graphics state context.setGraphicsState((PDGraphicsState) context.getGraphicsStack().pop()); } }