org.apache.pdfbox.cos.COSName Java Examples

The following examples show how to use org.apache.pdfbox.cos.COSName. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PDPageTree.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Constructor for reading.
 *
 * @param root A page tree root.
 * @param document The document which contains "root".
 */
PDPageTree(COSDictionary root, PDDocument document)
{
    if (root == null)
    {
        throw new IllegalArgumentException("page tree root cannot be null");
    }
    // repair bad PDFs which contain a Page dict instead of a page tree, see PDFBOX-3154
    if (COSName.PAGE.equals(root.getCOSName(COSName.TYPE)))
    {
        COSArray kids = new COSArray();
        kids.add(root);
        this.root = new COSDictionary();
        this.root.setItem(COSName.KIDS, kids);
        this.root.setInt(COSName.COUNT, 1);
    }
    else
    {
        this.root = root;
    }
    this.document = document;
}
 
Example #2
Source File: PDAnnotationMarkup.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Sets the paths that make this annotation.
 *
 * @param inkList An array of arrays, each representing a stroked path. Each array shall be a
 * series of alternating horizontal and vertical coordinates. If the parameter is null the entry
 * will be removed.
 */
public void setInkList(float[][] inkList)
{
    if (inkList == null)
    {
        getCOSObject().removeItem(COSName.INKLIST);
        return;
    }
    COSArray array = new COSArray();
    for (float[] path : inkList)
    {
        COSArray innerArray = new COSArray();
        innerArray.setFloatArray(path);
        array.add(innerArray);
    }
    getCOSObject().setItem(COSName.INKLIST, array);
}
 
Example #3
Source File: FDFCatalog.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will get the FDF dictionary.
 *
 * @return The FDF dictionary.
 */
public FDFDictionary getFDF()
{
    COSDictionary fdf = (COSDictionary) catalog.getDictionaryObject(COSName.FDF);
    FDFDictionary retval;
    if (fdf != null)
    {
        retval = new FDFDictionary(fdf);
    }
    else
    {
        retval = new FDFDictionary();
        setFDF(retval);
    }
    return retval;
}
 
Example #4
Source File: PDCIDFontType2Embedder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new TrueType font embedder for the given TTF as a PDCIDFontType2.
 *
 * @param document parent document
 * @param dict font dictionary
 * @param ttf True Type Font
 * @param parent parent Type 0 font
 * @throws IOException if the TTF could not be read
 */
PDCIDFontType2Embedder(PDDocument document, COSDictionary dict, TrueTypeFont ttf,
        boolean embedSubset, PDType0Font parent, boolean vertical) throws IOException
{
    super(document, dict, ttf, embedSubset);
    this.document = document;
    this.dict = dict;
    this.parent = parent;
    this.vertical = vertical;

    // parent Type 0 font
    dict.setItem(COSName.SUBTYPE, COSName.TYPE0);
    dict.setName(COSName.BASE_FONT, fontDescriptor.getFontName());
    dict.setItem(COSName.ENCODING, vertical ? COSName.IDENTITY_V : COSName.IDENTITY_H); // CID = GID

    // descendant CIDFont
    cidFont = createCIDFont();
    COSArray descendantFonts = new COSArray();
    descendantFonts.add(cidFont);
    dict.setItem(COSName.DESCENDANT_FONTS, descendantFonts);

    if (!embedSubset)
    {
        // build GID -> Unicode map
        buildToUnicodeCMap(null);
    }
}
 
Example #5
Source File: PDAnnotationLine.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructor.
 */
public PDAnnotationLine()
{
    getCOSObject().setName(COSName.SUBTYPE, SUB_TYPE);
    // Dictionary value L is mandatory, fill in with arbitary value
    setLine(new float[] { 0, 0, 0, 0 });
}
 
Example #6
Source File: PDSeedValueCertificate.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Removes a key usage extension
 *
 * @param keyUsageExtension ASCII string that consists of {0, 1, X}
 */
public void removeKeyUsage(String keyUsageExtension)
{
    COSBase base = this.dictionary.getDictionaryObject(COSName.KEY_USAGE);
    if (base instanceof COSArray)
    {
        COSArray array = (COSArray) base;
        array.remove(new COSString(keyUsageExtension));
    }
}
 
Example #7
Source File: PDSeedValueCertificate.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * removes a subject from the list
 *
 * @param subject byte array containing DER-encoded X.509v3 certificate
 */
public void removeSubject(byte[] subject)
{
    COSBase base = this.dictionary.getDictionaryObject(COSName.SUBJECT);
    if (base instanceof COSArray)
    {
        COSArray array = (COSArray) base;
        array.remove(new COSString(subject));
    }
}
 
Example #8
Source File: PDComplexFileSpecification.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private COSBase getObjectFromEFDictionary(COSName key)
{
    COSDictionary ef = getEFDictionary();
    if (ef != null)
    {
        return ef.getDictionaryObject(key);
    }
    return null;
}
 
Example #9
Source File: PDFBoxTree.java    From Pdf2Dom with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void processFontResources(PDResources resources, FontTable table) throws IOException
{
    String fontNotSupportedMessage = "Font: {} skipped because type '{}' is not supported.";

    for (COSName key : resources.getFontNames())
    {
        PDFont font = resources.getFont(key);
        if (font instanceof PDTrueTypeFont)
        {
            table.addEntry( font);
            log.debug("Font: " + font.getName() + " TTF");
        }
        else if (font instanceof PDType0Font)
        {
            PDCIDFont descendantFont = ((PDType0Font) font).getDescendantFont();
            if (descendantFont instanceof PDCIDFontType2)
                table.addEntry(font);
            else
                log.warn(fontNotSupportedMessage, font.getName(), font.getClass().getSimpleName());
        }
        else if (font instanceof PDType1CFont)
            table.addEntry(font);
        else
            log.warn(fontNotSupportedMessage, font.getName(), font.getClass().getSimpleName());
    }

    for (COSName name : resources.getXObjectNames())
    {
        PDXObject xobject = resources.getXObject(name);
        if (xobject instanceof PDFormXObject)
        {
            PDFormXObject xObjectForm = (PDFormXObject) xobject;
            PDResources formResources = xObjectForm.getResources();
            if (formResources != null && formResources != resources && formResources.getCOSObject() != resources.getCOSObject())
                processFontResources(formResources, table);
        }
    }

}
 
Example #10
Source File: PDExtendedGraphicsState.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will set a float object.
 *
 * @param key The key to the data that we are setting.
 * @param value The value that we are setting.
 */
private void setFloatItem( COSName key, Float value )
{
    if( value == null )
    {
        dict.removeItem(key);
    }
    else
    {
        dict.setItem(key, new COSFloat(value));
    }
}
 
Example #11
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void insertInnerFormToHolderResources(PDFormXObject innerForm,
                                             PDResources holderFormResources)
{
    holderFormResources.put(COSName.FRM, innerForm);
    pdfStructure.setInnerFormName(COSName.FRM);
    LOG.info("Now inserted inner form inside holder form");
}
 
Example #12
Source File: PDExtendedGraphicsState.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the soft mask stored in the COS dictionary
 *
 * @return the soft mask or null if there isn't any.
 */
public PDSoftMask getSoftMask()
{
    if (!dict.containsKey(COSName.SMASK))
    {
        return null;
    }
    return PDSoftMask.create(dict.getDictionaryObject(COSName.SMASK));
}
 
Example #13
Source File: PDDefaultAttributeObject.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets an attribute.
 * 
 * @param attrName the attribute name
 * @param attrValue the attribute value
 */
public void setAttribute(String attrName, COSBase attrValue)
{
    COSBase old = this.getAttributeValue(attrName);
    this.getCOSObject().setItem(COSName.getPDFName(attrName), attrValue);
    this.potentiallyNotifyChanged(old, attrValue);
}
 
Example #14
Source File: PDNonTerminalField.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public int getFieldFlags()
{
    int retval = 0;
    COSInteger ff = (COSInteger) getCOSObject().getDictionaryObject(COSName.FF);
    if (ff != null)
    {
        retval = ff.intValue();
    }
    // There is no need to look up the parent hierarchy within a non terminal field
    return retval;
}
 
Example #15
Source File: SetNonStrokingDeviceGrayColor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICEGRAY);
    context.getGraphicsState().setNonStrokingColorSpace(cs);
    super.process(operator, arguments);
}
 
Example #16
Source File: NurminenDetectionAlgorithm.java    From tabula-java with MIT License 5 votes vote down vote up
private PDDocument removeText(PDPage page) throws IOException {

        PDFStreamParser parser = new PDFStreamParser(page);
        parser.parse();
        List<Object> tokens = parser.getTokens();
        List<Object> newTokens = new ArrayList<>();
        for (Object token : tokens) {
            if (token instanceof Operator) {
                Operator op = (Operator) token;
                if (op.getName().equals("TJ") || op.getName().equals("Tj")) {
                    //remove the one argument to this operator
                    newTokens.remove(newTokens.size() - 1);
                    continue;
                }
            }
            newTokens.add(token);
        }

        PDDocument document = new PDDocument();
        PDPage newPage = document.importPage(page);
        newPage.setResources(page.getResources());

        PDStream newContents = new PDStream(document);
        OutputStream out = newContents.createOutputStream(COSName.FLATE_DECODE);
        ContentStreamWriter writer = new ContentStreamWriter(out);
        writer.writeTokens(newTokens);
        out.close();
        newPage.setContents(newContents);
        return document;
    }
 
Example #17
Source File: SetStrokingColorSpace.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    COSBase base = arguments.get(0);
    if (!(base instanceof COSName))
    {
        return;
    }
    PDColorSpace cs = context.getResources().getColorSpace((COSName) base);
    context.getGraphicsState().setStrokingColorSpace(cs);
    context.getGraphicsState().setStrokingColor(cs.getInitialColor());
}
 
Example #18
Source File: PDFontDescriptor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * A string representing the preferred font stretch.
 * According to the PDF Spec:
 * The font stretch value; it must be one of the following (ordered from
 * narrowest to widest): UltraCondensed, ExtraCondensed, Condensed, SemiCondensed,
 * Normal, SemiExpanded, Expanded, ExtraExpanded or UltraExpanded.
 *
 * @return The stretch of the font.
 */
public String getFontStretch()
{
    String retval = null;
    COSName name = (COSName)dic.getDictionaryObject( COSName.FONT_STRETCH );
    if( name != null )
    {
        retval = name.getName();
    }
    return retval;
}
 
Example #19
Source File: PDColor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a PDColor containing the given pattern name.
 * @param patternName the name of a pattern in a pattern dictionary
 * @param colorSpace color space in which the pattern is defined
 */
public PDColor(COSName patternName, PDColorSpace colorSpace)
{
    this.components = new float[0];
    this.patternName = patternName;
    this.colorSpace = colorSpace;
}
 
Example #20
Source File: FDFField.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will get the widget field flags that are associated with this field. The F entry in the FDF field
 * dictionary.
 *
 * @param f The new value for the field flags.
 */
public void setWidgetFieldFlags(Integer f)
{
    COSInteger value = null;
    if (f != null)
    {
        value = COSInteger.get(f);
    }
    field.setItem(COSName.F, value);
}
 
Example #21
Source File: PDPage.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the metadata that is part of the document catalog. This will return null if there is
 * no meta data for this object.
 * 
 * @return The metadata for this object.
 */
public PDMetadata getMetadata()
{
    PDMetadata retval = null;
    COSBase base = page.getDictionaryObject(COSName.METADATA);
    if (base instanceof COSStream)
    {
        retval = new PDMetadata((COSStream) base);
    }
    return retval;
}
 
Example #22
Source File: PDStandardAttributeObject.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets an array of name values.
 * 
 * @param name the attribute name
 * @param values the array of name values
 */
protected void setArrayOfName(String name, String[] values)
{
    COSBase oldBase = this.getCOSObject().getDictionaryObject(name);
    COSArray array = new COSArray();
    for (String value : values)
    {
        array.add(COSName.getPDFName(value));
    }
    this.getCOSObject().setItem(name, array);
    COSBase newBase = this.getCOSObject().getDictionaryObject(name);
    this.potentiallyNotifyChanged(oldBase, newBase);
}
 
Example #23
Source File: PDStructureElement.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the parent in the structure hierarchy (P).
 * 
 * @return the parent in the structure hierarchy
 */
public PDStructureNode getParent()
{
    COSBase base = this.getCOSObject().getDictionaryObject(COSName.P);
    if (base instanceof COSDictionary)
    {
        return PDStructureNode.create((COSDictionary) base);
    }
    return null;
}
 
Example #24
Source File: PDFunction.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param function The function stream.
 * 
 */
public PDFunction( COSBase function )
{
    if (function instanceof COSStream)
    {
        functionStream = new PDStream( (COSStream)function );
        functionStream.getCOSObject().setItem( COSName.TYPE, COSName.FUNCTION );
    }
    else if (function instanceof COSDictionary)
    {
        functionDictionary = (COSDictionary)function;
    }
}
 
Example #25
Source File: PDFMergerUtility.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void cleanupFieldCOSDictionary(COSDictionary fieldCos)
{
    //TODO: align that list with the PDF spec. Vurrently only based on sample forms
    fieldCos.removeItem(COSName.F);
    fieldCos.removeItem(COSName.MK);
    fieldCos.removeItem(COSName.P);
    fieldCos.removeItem(COSName.RECT);
    fieldCos.removeItem(COSName.SUBTYPE);
    fieldCos.removeItem(COSName.TYPE);
}
 
Example #26
Source File: PDPageTree.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Helper to get kids from malformed PDFs.
 * @param node page tree node
 * @return list of kids
 */
private List<COSDictionary> getKids(COSDictionary node)
{
    List<COSDictionary> result = new ArrayList<COSDictionary>();

    COSArray kids = node.getCOSArray(COSName.KIDS);
    if (kids == null)
    {
        // probably a malformed PDF
        return result;
    }

    for (int i = 0, size = kids.size(); i < size; i++)
    {
        COSBase base = kids.getObject(i);
        if (base instanceof COSDictionary)
        {
            result.add((COSDictionary) base);
        }
        else
        {
            LOG.warn("COSDictionary expected, but got " +
                    (base == null ? "null" : base.getClass().getSimpleName()));
        }
    }

    return result;
}
 
Example #27
Source File: PDType1FontEmbedder.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * This will load a PFB to be embedded into a document.
 *
 * @param doc The PDF document that will hold the embedded font.
 * @param dict The Font dictionary to write to.
 * @param pfbStream The pfb input.
 * @throws IOException If there is an error loading the data.
 */
PDType1FontEmbedder(PDDocument doc, COSDictionary dict, InputStream pfbStream,
                    Encoding encoding) throws IOException
{
    dict.setItem(COSName.SUBTYPE, COSName.TYPE1);

    // read the pfb
    byte[] pfbBytes = IOUtils.toByteArray(pfbStream);
    PfbParser pfbParser = new PfbParser(pfbBytes);
    type1 = Type1Font.createWithPFB(pfbBytes);
    
    if (encoding == null)
    {
        fontEncoding = Type1Encoding.fromFontBox(type1.getEncoding());
    }
    else
    {
        fontEncoding = encoding;
    }

    // build font descriptor
    PDFontDescriptor fd = buildFontDescriptor(type1);

    PDStream fontStream = new PDStream(doc, pfbParser.getInputStream(), COSName.FLATE_DECODE);
    fontStream.getCOSObject().setInt("Length", pfbParser.size());
    for (int i = 0; i < pfbParser.getLengths().length; i++)
    {
        fontStream.getCOSObject().setInt("Length" + (i + 1), pfbParser.getLengths()[i]);
    }
    fd.setFontFile(fontStream);

    // set the values
    dict.setItem(COSName.FONT_DESC, fd);
    dict.setName(COSName.BASE_FONT, type1.getName());

    // widths
    List<Integer> widths = new ArrayList<Integer>(256);
    for (int code = 0; code <= 255; code++)
    {
        String name = fontEncoding.getName(code);
        int width = Math.round(type1.getWidth(name));
        widths.add(width);
    }
    
    dict.setInt(COSName.FIRST_CHAR, 0);
    dict.setInt(COSName.LAST_CHAR, 255);
    dict.setItem(COSName.WIDTHS, COSArrayList.converterToCOSArray(widths));
    dict.setItem(COSName.ENCODING, encoding);
}
 
Example #28
Source File: PDShadingType2.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Sets the Coords entry for this shading.
 *
 * @param newCoords the coordinates array
 */
public void setCoords(COSArray newCoords)
{
    coords = newCoords;
    getCOSObject().setItem(COSName.COORDS, newCoords);
}
 
Example #29
Source File: PDLab.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a new Lab color space.
 */
public PDLab()
{
    super(COSName.LAB);
}
 
Example #30
Source File: PDFormXObject.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a Form Image XObject for writing, in the given document.
 * @param document The current document
 */
public PDFormXObject(PDDocument document)
{
    super(document, COSName.FORM);
    cache = null;
}