Example usage for org.apache.pdfbox.cos COSName getPDFName

List of usage examples for org.apache.pdfbox.cos COSName getPDFName

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSName getPDFName.

Prototype

public static COSName getPDFName(String aName) 

Source Link

Document

This will get a COSName object with that name.

Usage

From source file:com.formkiq.core.service.generator.pdfbox.SigUtils.java

License:Apache License

/**
 * Set the access permissions granted for this document in the DocMDP
 * transform parameters dictionary. Details are described in the table
 * "Entries in the DocMDP transform parameters dictionary" in the PDF
 * specification./* ww  w . j a  v a  2 s .  c  o m*/
 *
 * @param doc
 *            The document.
 * @param signature
 *            The signature object.
 * @param accessPermissions
 *            The permission value (1, 2 or 3).
 */
public static void setMDPPermission(final PDDocument doc, final PDSignature signature,
        final int accessPermissions) {

    COSDictionary sigDict = signature.getCOSObject();

    // DocMDP specific stuff
    COSDictionary transformParameters = new COSDictionary();
    transformParameters.setItem(COSName.TYPE, COSName.getPDFName("TransformParams"));
    transformParameters.setInt(COSName.P, accessPermissions);
    transformParameters.setName(COSName.V, "1.2");
    transformParameters.setNeedToBeUpdated(true);

    COSDictionary referenceDict = new COSDictionary();
    referenceDict.setItem(COSName.TYPE, COSName.getPDFName("SigRef"));
    referenceDict.setItem("TransformMethod", COSName.DOCMDP);
    referenceDict.setItem("DigestMethod", COSName.getPDFName("SHA1"));
    referenceDict.setItem("TransformParams", transformParameters);
    referenceDict.setNeedToBeUpdated(true);

    COSArray referenceArray = new COSArray();
    referenceArray.add(referenceDict);
    sigDict.setItem("Reference", referenceArray);
    referenceArray.setNeedToBeUpdated(true);

    // Catalog
    COSDictionary catalogDict = doc.getDocumentCatalog().getCOSObject();
    COSDictionary permsDict = new COSDictionary();
    catalogDict.setItem(COSName.PERMS, permsDict);
    permsDict.setItem(COSName.DOCMDP, signature);
    catalogDict.setNeedToBeUpdated(true);
    permsDict.setNeedToBeUpdated(true);
}

From source file:com.modemo.javase.signature.SigUtils.java

License:Apache License

/**
 * Set the access permissions granted for this document in the DocMDP transform parameters
 * dictionary. Details are described in the table "Entries in the DocMDP transform parameters
 * dictionary" in the PDF specification.
 *
 * @param doc The document./*  www.j a  v a2 s  .  co m*/
 * @param signature The signature object.
 * @param accessPermissions The permission value (1, 2 or 3).
 */
static public void setMDPPermission(PDDocument doc, PDSignature signature, int accessPermissions) {
    COSDictionary sigDict = signature.getCOSObject();

    // DocMDP specific stuff
    COSDictionary transformParameters = new COSDictionary();
    transformParameters.setItem(COSName.TYPE, COSName.getPDFName("TransformParams"));
    transformParameters.setInt(COSName.P, accessPermissions);
    transformParameters.setName(COSName.V, "1.2");
    transformParameters.setNeedToBeUpdated(true);

    COSDictionary referenceDict = new COSDictionary();
    referenceDict.setItem(COSName.TYPE, COSName.getPDFName("SigRef"));
    referenceDict.setItem("TransformMethod", COSName.DOCMDP);
    referenceDict.setItem("DigestMethod", COSName.getPDFName("SHA1"));
    referenceDict.setItem("TransformParams", transformParameters);
    referenceDict.setNeedToBeUpdated(true);

    COSArray referenceArray = new COSArray();
    referenceArray.add(referenceDict);
    sigDict.setItem("Reference", referenceArray);
    referenceArray.setNeedToBeUpdated(true);

    // Catalog
    COSDictionary catalogDict = doc.getDocumentCatalog().getCOSObject();
    COSDictionary permsDict = new COSDictionary();
    catalogDict.setItem(COSName.PERMS, permsDict);
    permsDict.setItem(COSName.DOCMDP, signature);
    catalogDict.setNeedToBeUpdated(true);
    permsDict.setNeedToBeUpdated(true);
}

From source file:eu.europa.ec.markt.dss.signature.pdf.pdfbox.PdfBoxDict.java

License:Open Source License

public PdfBoxDict(String type) {
    wrapped = new COSDictionary();
    if (type != null) {
        wrapped.setItem("Type", COSName.getPDFName(type));
    }
}

From source file:fi.nls.oskari.printout.printing.PDPageContentStream.java

License:Apache License

private void writeColorSpace(PDColorSpace colorSpace) throws IOException {
    COSName key = null;/*from   w ww . j  a  v  a 2  s  .c o m*/
    if (colorSpace instanceof PDDeviceGray || colorSpace instanceof PDDeviceRGB
            || colorSpace instanceof PDDeviceCMYK) {
        key = COSName.getPDFName(colorSpace.getName());
    } else {
        COSDictionary colorSpaces = (COSDictionary) resources.getCOSDictionary()
                .getDictionaryObject(COSName.COLORSPACE);
        if (colorSpaces == null) {
            colorSpaces = new COSDictionary();
            resources.getCOSDictionary().setItem(COSName.COLORSPACE, colorSpaces);
        }
        key = colorSpaces.getKeyForValue(colorSpace.getCOSObject());

        if (key == null) {
            int counter = 0;
            String csName = "CS";
            while (colorSpaces.containsValue(csName + counter)) {
                counter++;
            }
            key = COSName.getPDFName(csName + counter);
            colorSpaces.setItem(key, colorSpace);
        }
    }
    key.writePDF(output);
    appendRawCommands(SPACE);
}

From source file:fi.riista.feature.permit.invoice.pdf.PermitHarvestInvoicePdfBuilder.java

private void addFormFieldData() throws IOException {
    textField("iban", model.getInvoiceAccountDetails().getCombinedBankNameAndIbanString());
    textField("bic", model.getInvoiceAccountDetails().getBic().toString());
    textField("saaja", model.getPaymentRecipient());
    textField("maksaja", Joiner.on('\n').join(model.getInvoiceRecipient().formatAsLines()));
    textField("summa", model.getAmountText());
    textField("viitenumero", model.getInvoiceReferenceForHuman());
    textField("lisatiedot", model.getInvoiceAdditionalInfo());

    this.acroForm.setNeedAppearances(false);

    // Fix annotations
    for (PDPage page : this.pdfDocument.getPages()) {
        for (PDAnnotation annot : page.getAnnotations()) {
            annot.setPage(page);//ww w.j av  a 2 s .  co m
        }
    }

    // Define font resources names used in PDF template
    final PDResources dr = new PDResources();
    dr.put(COSName.getPDFName("Helv"), PDType1Font.HELVETICA);
    dr.put(COSName.getPDFName("HeBo"), PDType1Font.HELVETICA_BOLD);
    this.acroForm.setDefaultResources(dr);

    // Convert form fields to text
    this.acroForm.flatten();
}

From source file:fixture.pdfboxeg.CreateSignatureBase.java

License:Apache License

public void setMDPPermission(PDDocument doc, PDSignature signature, int accessPermissions) {
    COSDictionary sigDict = signature.getCOSObject();

    // DocMDP specific stuff
    COSDictionary transformParameters = new COSDictionary();
    transformParameters.setItem(COSName.TYPE, COSName.getPDFName("TransformParams"));
    transformParameters.setInt(COSName.P, accessPermissions);
    transformParameters.setName(COSName.V, "1.2");
    transformParameters.setNeedToBeUpdated(true);

    COSDictionary referenceDict = new COSDictionary();
    referenceDict.setItem(COSName.TYPE, COSName.getPDFName("SigRef"));
    referenceDict.setItem("TransformMethod", COSName.getPDFName("DocMDP"));
    referenceDict.setItem("DigestMethod", COSName.getPDFName("SHA1"));
    referenceDict.setItem("TransformParams", transformParameters);
    referenceDict.setNeedToBeUpdated(true);

    COSArray referenceArray = new COSArray();
    referenceArray.add(referenceDict);/*w ww .  j a va2 s  . co m*/
    sigDict.setItem("Reference", referenceArray);
    referenceArray.setNeedToBeUpdated(true);

    // Catalog
    COSDictionary catalogDict = doc.getDocumentCatalog().getCOSObject();
    COSDictionary permsDict = new COSDictionary();
    catalogDict.setItem(COSName.PERMS, permsDict);
    permsDict.setItem(COSName.DOCMDP, signature);
    catalogDict.setNeedToBeUpdated(true);
    permsDict.setNeedToBeUpdated(true);
}

From source file:modules.PDFFontDependencyExtractorModule.java

License:Apache License

public PDFFontResults extractFontList(File f) throws IOException, InvalidParameterException {
    PDDocument document;/* w  ww.j  a  v a 2  s  .  c om*/
    try {
        document = PDDocument.load(f);
    } catch (IOException x) {
        throw new InvalidParameterException("Not a PDF file");
    }
    SortedSet<FontInformation> ret = new TreeSet<FontInformation>(new Comparator<FontInformation>() {

        @Override
        public int compare(FontInformation o1, FontInformation o2) {
            int a = o1.fontName.compareTo(o2.fontName);
            if (a != 0)
                return a;
            else
                return o1.fontType.compareTo(o2.fontType);
        }

    });

    document.getDocumentCatalog().getAllPages();
    // The code down here is easier as it gets all the fonts used in the
    // document. Still, this would inlcude unused fonts, so we get the fonts
    // page by page and add them to a Hash table.
    for (COSObject c : document.getDocument().getObjectsByType(COSName.FONT)) {
        if (c == null || !(c.getObject() instanceof COSDictionary)) {
            continue;
            // System.out.println(c.getObject());
        }

        COSDictionary fontDictionary = (COSDictionary) c.getObject();
        // System.out.println(dic.getNameAsString(COSName.BASE_FONT));
        // }
        // }
        // int pagen = document.getNumberOfPages();
        // i=0;
        // for (int p=0;p<pagen;p++){
        // PDPage page = (PDPage)pages.get(p);
        // PDResources res = page.findResources();
        // //for each page resources
        // if (res==null) continue;
        // // get the font dictionary
        // COSDictionary fonts = (COSDictionary)
        // res.getCOSDictionary().getDictionaryObject( COSName.FONT );
        // for( COSName fontName : fonts.keySet() ) {
        // COSObject font = (COSObject) fonts.getItem( fontName );
        // // if the font has already been visited we ingore it
        // long objectId = font.getObjectNumber().longValue();
        // if (ret.get(objectId)!=null)
        // continue;
        // if( font==null || ! (font.getObject() instanceof COSDictionary) )
        // continue;
        // COSDictionary fontDictionary = (COSDictionary)font.getObject();

        // Type MUSt be font
        if (!fontDictionary.getNameAsString(COSName.TYPE).equals("Font")) {
            continue;
        }
        // get the variables
        FontInformation fi = new FontInformation();
        fi.fontType = fontDictionary.getNameAsString(COSName.SUBTYPE);

        String baseFont = fontDictionary.getNameAsString(COSName.BASE_FONT);
        if (baseFont == null) {
            continue;
        }
        if (Arrays.binarySearch(standard14, baseFont) >= 0) {
            continue;
        }
        COSDictionary fontDescriptor = (COSDictionary) fontDictionary.getDictionaryObject(COSName.FONT_DESC);
        COSBase enc = fontDictionary.getItem(COSName.ENCODING);
        COSBase uni = fontDictionary.getItem(COSName.TO_UNICODE);
        fontDictionary.getInt(COSName.FIRST_CHAR);
        fontDictionary.getInt(COSName.LAST_CHAR);
        String encoding;
        boolean toUnicode = uni != null;
        if (enc == null) {
            encoding = "standard14";
        }
        if (enc instanceof COSString) {
            encoding = ((COSString) enc).getString();
        } else {
            encoding = "table";
        }
        fi.isSubset = false;
        boolean t = true;
        // Type one and TT can have subsets defineing the basename see 5.5.3
        // pdfref 1.6
        // if (fi.fontType.lastIndexOf(COSName.TYPE1.getName())!=-1 ||
        // fi.fontType.equals(COSName.TRUE_TYPE.getName()) )
        if (baseFont != null) {
            if (baseFont.length() > 6) {
                for (int k = 0; k < 6; k++)
                    if (!Character.isUpperCase(baseFont.charAt(k))) {
                        t = false;
                    }
                if (baseFont.charAt(6) != '+') {
                    t = false;
                }
            } else {
                t = false;
            }
            fi.isSubset = t;
            if (fi.isSubset) {
                fi.baseName = baseFont.substring(0, 6);
                baseFont = baseFont.substring(7);
            }
        }
        fi.fontFlags = 0;
        if (fi.fontType.equals(COSName.TYPE0.getName()) || fi.fontType.equals(COSName.TYPE3.getName())) {
            fi.isEmbedded = true;
        }

        if (fontDescriptor != null) {
            // in Type1 charset indicates font is subsetted
            if (fontDescriptor.getItem(COSName.CHAR_SET) != null) {
                fi.isSubset = true;
            }
            if (fontDescriptor.getItem(COSName.FONT_FILE) != null
                    || fontDescriptor.getItem(COSName.FONT_FILE3) != null
                    || fontDescriptor.getItem(COSName.FONT_FILE2) != null) {
                fi.isEmbedded = true;
            }
            fi.fontFlags = fontDescriptor.getInt(COSName.getPDFName("Flags"));
            fi.fontFamily = fontDescriptor.getString(COSName.FONT_FAMILY);
            fi.fontStretch = fontDescriptor.getString(COSName.FONT_STRETCH);

        }
        fi.charset = encoding;
        fi.fontName = baseFont;
        fi.isToUnicode = toUnicode;
        fi.encoding = fontDictionary.getNameAsString(COSName.CID_TO_GID_MAP);

        ret.add(fi);

    } // for all fonts

    HashMultimap<String, FontInformation> m = HashMultimap.create();

    for (FontInformation ff : ret) {
        m.put(ff.fontName, ff);
    }
    LinkedList<FontInformation> missing = new LinkedList<FontInformation>();
    Set<String> k = m.keySet();
    for (String kk : k) {
        Set<FontInformation> s = m.get(kk);
        if (s.size() < 1) {
            continue;
        }
        if (s.size() > 1) {
            boolean found = false;
            FontInformation ff = null;
            for (FontInformation fonti : s) {
                if (!fonti.isEmbedded) {
                    ff = fonti;
                } else {
                    found = true;
                }
            }
            if (!found) {
                missing.add(ff);
            }
        } else {
            FontInformation ff = s.iterator().next();
            if (!ff.isEmbedded) {
                missing.add(ff);
            }
        }

    }

    // } // for all pages
    // Iterator<FontInformation> it = ret.iterator();
    // FontInformation prev = null;
    // LinkedList<FontInformation> toDelete = new
    // LinkedList<FontInformation>();
    // while (it.hasNext()) {
    // FontInformation current = it.next();
    //
    // if (prev!= null && prev.fontName.equals(current.fontName) &&
    // (prev.fontType.startsWith("CIDFontType") ||
    // current.fontType.startsWith("CIDFontType")))
    // toDelete.add(current);
    // prev = current;
    // }
    //
    // //ret.removeAll(toDelete);
    // FontInformation[] retArray =toDelete.toArray(new FontInformation[0]);
    //

    if (missing.size() == 0) {
        missing = null;
    } else {
        System.out.println("Found missing fonts: " + f);
        System.out.println(missing);
    }
    return new PDFFontResults(new LinkedList<FontInformation>(ret), missing);
}

From source file:net.padaf.preflight.actions.GoToAction.java

License:Apache License

@Override
protected boolean innerValid(List<ValidationError> error) {
    COSBase d = this.actionDictionnary.getItem(COSName.getPDFName(ACTION_DICTIONARY_KEY_D));

    // ---- D entry is mandatory
    if (d == null) {
        error.add(new ValidationError(ERROR_ACTION_MISING_KEY, "D entry is mandatory for the GoToActions"));
        return false;
    }//from   w w  w. j a v a  2  s.c om

    if (!(d instanceof COSName || COSUtils.isString(d, cDoc) || COSUtils.isArray(d, cDoc))) {
        error.add(new ValidationError(ERROR_ACTION_INVALID_TYPE, "Type of D entry is invalid"));
        return false;
    }

    return true;
}

From source file:net.padaf.preflight.actions.GoToRemoteAction.java

License:Apache License

@Override
protected boolean innerValid(List<ValidationError> error) {
    if (super.innerValid(error)) {
        COSBase f = this.actionDictionnary.getItem(COSName.getPDFName(ACTION_DICTIONARY_KEY_F));
        if (f == null) {
            error.add(new ValidationError(ERROR_ACTION_MISING_KEY,
                    "F entry is mandatory for the GoToRemoteActions"));
            return false;
        }/*from  w  w  w .  j  a v a2s . c  om*/
    }
    return true;
}

From source file:net.padaf.preflight.actions.HideAction.java

License:Apache License

@Override
protected boolean innerValid(List<ValidationError> error) {
    COSBase t = this.actionDictionnary.getItem(COSName.getPDFName(ACTION_DICTIONARY_KEY_T));
    // ---- T entry is mandatory
    if (t == null) {
        error.add(new ValidationError(ERROR_ACTION_MISING_KEY, "T entry is mandatory for the NamedActions"));
        return false;
    }//w  ww. jav a 2  s. com

    if (!(COSUtils.isDictionary(t, cDoc) || COSUtils.isArray(t, cDoc) || COSUtils.isString(t, cDoc))) {
        error.add(new ValidationError(ERROR_ACTION_INVALID_TYPE, "T entry type is invalid"));
        return false;
    }

    /*
     * ---- H entry is optional but the default value is True (annotations of
     * the T entry will be hidden) according to the aim of a PDF/A it should be
     * false (annotations of the T entry will be shown).
     * 
     * We check the H value and we throw an error if it is true because of the
     * PDF/A Application Notes sentence :
     * 
     * The PDF Reference supports a concept whereby something will happen when
     * the user performs an explicit or implicit action in a PDF viewer - these
     * "things" are called Actions. PDF/A-1 permits a limited set of these
     * Actions, which are detailed in section 6.6.1. Specifically, any action
     * that could change the visual representation of the document or is not
     * documented in the PDF Reference is not permitted. This includes the /Hide
     * action which isn't specifically prohibited by PDF/A-1, but should have
     * been.
     */
    boolean h = this.actionDictionnary.getBoolean(COSName.getPDFName(ACTION_DICTIONARY_KEY_H), true);
    if (h) {
        error.add(new ValidationError(ERROR_ACTION_HIDE_H_INVALID, "H entry is \"true\""));
        return false;
    }

    return true;
}