Java Code Examples for org.apache.pdfbox.cos.COSName

The following examples show how to use org.apache.pdfbox.cos.COSName. These examples are extracted from open source projects. 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 Project: gcs   Author: richardwilkes   File: FDFCatalog.java    License: 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 #2
Source Project: gcs   Author: richardwilkes   File: PDAnnotationMarkup.java    License: 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 Project: gcs   Author: richardwilkes   File: PDPageTree.java    License: 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 #4
Source Project: tabula-java   Author: tabulapdf   File: NurminenDetectionAlgorithm.java    License: 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 #5
Source Project: Pdf2Dom   Author: radkovo   File: PDFBoxTree.java    License: 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 #6
Source Project: gcs   Author: richardwilkes   File: PDSeedValueCertificate.java    License: 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 Project: gcs   Author: richardwilkes   File: PDSeedValueCertificate.java    License: 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 Project: gcs   Author: richardwilkes   File: PDComplexFileSpecification.java    License: 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 Project: gcs   Author: richardwilkes   File: PDAnnotationLine.java    License: 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 #10
Source Project: gcs   Author: richardwilkes   File: PDExtendedGraphicsState.java    License: 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 Project: gcs   Author: richardwilkes   File: PDVisibleSigBuilder.java    License: 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 Project: gcs   Author: richardwilkes   File: PDExtendedGraphicsState.java    License: 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 Project: gcs   Author: richardwilkes   File: PDDefaultAttributeObject.java    License: 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 Project: gcs   Author: richardwilkes   File: PDNonTerminalField.java    License: 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 Project: gcs   Author: richardwilkes   File: SetNonStrokingDeviceGrayColor.java    License: 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 Project: gcs   Author: richardwilkes   File: PDPageTree.java    License: 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 #17
Source Project: gcs   Author: richardwilkes   File: SetStrokingColorSpace.java    License: 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 Project: gcs   Author: richardwilkes   File: PDFontDescriptor.java    License: 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 Project: gcs   Author: richardwilkes   File: PDColor.java    License: 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 Project: gcs   Author: richardwilkes   File: FDFField.java    License: 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 Project: gcs   Author: richardwilkes   File: PDPage.java    License: 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 Project: gcs   Author: richardwilkes   File: PDStandardAttributeObject.java    License: 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 Project: gcs   Author: richardwilkes   File: PDStructureElement.java    License: 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 Project: gcs   Author: richardwilkes   File: PDFunction.java    License: 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 Project: gcs   Author: richardwilkes   File: PDFMergerUtility.java    License: 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 Project: gcs   Author: richardwilkes   File: PDCIDFontType2Embedder.java    License: 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 #27
Source Project: inception   Author: inception-project   File: PDFExtractor.java    License: Apache License 2.0 4 votes vote down vote up
@Override public void shadingFill(COSName cosName)
{
}
 
Example #28
Source Project: coming   Author: SpoonLabs   File: NPEfix16_sixteen_t.java    License: MIT License 4 votes vote down vote up
/**
 * This will set the number of color components.
 *
 * @param n The number of color components.
 */
public void setNumberOfComponents( int n )
{
    numberOfComponents = n;
    stream.getStream().setInt( COSName.N, n );
}
 
Example #29
Source Project: gcs   Author: richardwilkes   File: PDAnnotationText.java    License: Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Constructor.
 */
public PDAnnotationText()
{
    getCOSObject().setName(COSName.SUBTYPE, SUB_TYPE);
}
 
Example #30
Source Project: gcs   Author: richardwilkes   File: PDField.java    License: Mozilla Public License 2.0 4 votes vote down vote up
/**
 * 
 * @return true if the field is not to be exported.
 */
public boolean isNoExport()
{
    return dictionary.getFlag(COSName.FF, FLAG_NO_EXPORT);
}