org.apache.pdfbox.cos.COSBase Java Examples

The following examples show how to use org.apache.pdfbox.cos.COSBase. 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: FDFField.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return a list of options for a choice field. The value in the list will be 1 of 2 types.
 * java.lang.String or FDFOptionElement.
 *
 * @return A list of all options.
 */
public List<Object> getOptions()
{
    List<Object> retval = null;
    COSArray array = (COSArray) field.getDictionaryObject(COSName.OPT);
    if (array != null)
    {
        List<Object> objects = new ArrayList<Object>();
        for (int i = 0; i < array.size(); i++)
        {
            COSBase next = array.getObject(i);
            if (next instanceof COSString)
            {
                objects.add(((COSString) next).getString());
            }
            else
            {
                COSArray value = (COSArray) next;
                objects.add(new FDFOptionElement(value));
            }
        }
        retval = new COSArrayList<Object>(objects, array);
    }
    return retval;
}
 
Example #2
Source File: HelloSignAnalyzer.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
@Override
protected void processOperator(Operator operator, List<COSBase> operands) throws IOException
{
    String currentFormName = formName; 
    if (operator != null && "Do".equals(operator.getName()) && operands != null && operands.size() > 0)
    {
        COSBase base0 = operands.get(0);
        if (base0 instanceof COSName)
        {
            formName = ((COSName)base0).getName();
            if (currentFormName == null)
                lastFormName = formName;
        }
    }
    try
    {
        super.processOperator(operator, operands);
    }
    finally
    {
        formName = currentFormName;
    }
}
 
Example #3
Source File: PdfToTextInfoConverter.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> operands) throws IOException {
    if (operands.size() < 2) {
        throw new MissingOperandException(operator, operands);
    }
    COSBase base0 = operands.get(0);
    if (!(base0 instanceof COSNumber)) {
        return;
    }
    COSBase base1 = operands.get(1);
    if (!(base1 instanceof COSNumber)) {
        return;
    }
    COSNumber x = (COSNumber) base0;
    COSNumber y = (COSNumber) base1;
    Point2D.Float pos = context.transformedPoint(x.floatValue(), y.floatValue());
    linePath.moveTo(pos.x, pos.y);
}
 
Example #4
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Resolves all not already parsed objects of a dictionary recursively.
 * 
 * @param dictionaryObject dictionary to be parsed
 * @throws IOException if something went wrong
 * 
 */
private void parseDictionaryRecursive(COSObject dictionaryObject) throws IOException
{
    parseObjectDynamically(dictionaryObject, true);
    if (!(dictionaryObject.getObject() instanceof COSDictionary))
    {
        // we can't be lenient here, this is called by prepareDecryption()
        // to get the encryption directory
        throw new IOException("Dictionary object expected at offset " + source.getPosition());
    }
    COSDictionary dictionary = (COSDictionary) dictionaryObject.getObject();
    for (COSBase value : dictionary.getValues())
    {
        if (value instanceof COSObject)
        {
            COSObject object = (COSObject) value;
            if (object.getObject() == null)
            {
                parseDictionaryRecursive(object);
            }
        }
    }
}
 
Example #5
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the resource with the given name and kind as an indirect object, or null.
 */
private COSObject getIndirect(COSName kind, COSName name)
{
    COSDictionary dict = (COSDictionary)resources.getDictionaryObject(kind);
    if (dict == null)
    {
        return null;
    }
    COSBase base = dict.getItem(name);
    if (base instanceof COSObject)
    {
        return (COSObject)base;
    }
    // not an indirect object. Resource may have been added at runtime.
    return null;
}
 
Example #6
Source File: FDFJavaScript.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will get the javascript that is executed after the import.
 *
 * @return Some javascript code.
 */
public String getAfter()
{
    COSBase base = dictionary.getDictionaryObject(COSName.AFTER);
    if (base instanceof COSString)
    {
        return ((COSString) base).getString();
    }
    else if (base instanceof COSStream)
    {
        return ((COSStream) base).toTextString();
    }
    else
    {
        return null;
    }
}
 
Example #7
Source File: PDFMergerUtility.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Update the Pg and Obj references to the new (merged) page.
 */
private void updatePageReferences(PDFCloneUtility cloner,
        Map<Integer, COSObjectable> numberTreeAsMap,
        Map<COSDictionary, COSDictionary> objMapping) throws IOException
{
    for (COSObjectable obj : numberTreeAsMap.values())
    {
        if (obj == null)
        {
            continue;
        }
        PDParentTreeValue val = (PDParentTreeValue) obj;
        COSBase base = val.getCOSObject();
        if (base instanceof COSArray)
        {
            updatePageReferences(cloner, (COSArray) base, objMapping);
        }
        else
        {
            updatePageReferences(cloner, (COSDictionary) base, objMapping);
        }
    }
}
 
Example #8
Source File: PDIndexed.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private byte[] getLookupData() throws IOException
{
    if (lookupData == null)
    {
        COSBase lookupTable = array.getObject(3);
        if (lookupTable instanceof COSString)
        {
            lookupData = ((COSString) lookupTable).getBytes();
        }
        else if (lookupTable instanceof COSStream)
        {
            lookupData = new PDStream((COSStream)lookupTable).toByteArray();
        }
        else if (lookupTable == null)
        {
            lookupData = new byte[0];
        }
        else
        {
            throw new IOException("Error: Unknown type for lookup table " + lookupTable);
        }
    }
    return lookupData;
}
 
Example #9
Source File: SetGraphicsStateParameters.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    if (arguments.isEmpty())
    {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    if (!(base0 instanceof COSName))
    {
        return;
    }
    
    // set parameters from graphics state parameter dictionary
    COSName graphicsName = (COSName) base0;
    PDExtendedGraphicsState gs = context.getResources().getExtGState(graphicsName);
    if (gs == null)
    {
        LOG.error("name for 'gs' operator not found in resources: /" + graphicsName.getName());
        return;
    }
    gs.copyIntoGraphicsState( context.getGraphicsState() );
}
 
Example #10
Source File: PDAnnotation.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
protected PDColor getColor(COSName itemName)
{
    COSBase c = this.getCOSObject().getItem(itemName);
    if (c instanceof COSArray)
    {
        PDColorSpace colorSpace = null;
        switch (((COSArray) c).size())
        {
        case 1:
            colorSpace = PDDeviceGray.INSTANCE;
            break;
        case 3:
            colorSpace = PDDeviceRGB.INSTANCE;
            break;
        case 4:
            colorSpace = PDDeviceCMYK.INSTANCE;
            break;
        default:
            break;
        }
        return new PDColor((COSArray) c, colorSpace);
    }
    return null;
}
 
Example #11
Source File: PDFVisibleTextStripper.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> operands) throws IOException {
    if (operands.size() < 4) {
        throw new MissingOperandException(operator, operands);
    }
    if (!checkArrayTypesClass(operands, COSNumber.class)) {
        return;
    }
    COSNumber x1 = (COSNumber) operands.get(0);
    COSNumber y1 = (COSNumber) operands.get(1);
    COSNumber x3 = (COSNumber) operands.get(2);
    COSNumber y3 = (COSNumber) operands.get(3);

    Point2D.Float point1 = context.transformedPoint(x1.floatValue(), y1.floatValue());
    Point2D.Float point3 = context.transformedPoint(x3.floatValue(), y3.floatValue());

    linePath.curveTo(point1.x, point1.y, point3.x, point3.y, point3.x, point3.y);
}
 
Example #12
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This is used to handle an operation.
 * 
 * @param operator The operation to perform.
 * @param operands The list of arguments.
 * @throws IOException If there is an error processing the operation.
 */
protected void processOperator(Operator operator, List<COSBase> operands) throws IOException
{
    String name = operator.getName();
    OperatorProcessor processor = operators.get(name);
    if (processor != null)
    {
        processor.setContext(this);
        try
        {
            processor.process(operator, operands);
        }
        catch (IOException e)
        {
            operatorException(operator, operands, e);
        }
    }
    else
    {
        unsupportedOperator(operator, operands);
    }
}
 
Example #13
Source File: PDFVisibleTextStripper.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> operands) throws IOException {
    if (operands.size() < 6) {
        throw new MissingOperandException(operator, operands);
    }
    if (!checkArrayTypesClass(operands, COSNumber.class)) {
        return;
    }
    COSNumber x1 = (COSNumber) operands.get(0);
    COSNumber y1 = (COSNumber) operands.get(1);
    COSNumber x2 = (COSNumber) operands.get(2);
    COSNumber y2 = (COSNumber) operands.get(3);
    COSNumber x3 = (COSNumber) operands.get(4);
    COSNumber y3 = (COSNumber) operands.get(5);

    Point2D.Float point1 = context.transformedPoint(x1.floatValue(), y1.floatValue());
    Point2D.Float point2 = context.transformedPoint(x2.floatValue(), y2.floatValue());
    Point2D.Float point3 = context.transformedPoint(x3.floatValue(), y3.floatValue());

    linePath.curveTo(point1.x, point1.y, point2.x, point2.y, point3.x, point3.y);
}
 
Example #14
Source File: SetCharSpacing.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    if (arguments.isEmpty())
    {
        throw new MissingOperandException(operator, arguments);
    }

    // there are some documents which are incorrectly structured, and have
    // a wrong number of arguments to this, so we will assume the last argument
    // in the list
    Object charSpacing = arguments.get(arguments.size()-1);
    if (charSpacing instanceof COSNumber)
    {
        COSNumber characterSpacing = (COSNumber)charSpacing;
        context.getGraphicsState().getTextState().setCharacterSpacing(characterSpacing.floatValue());
    }
}
 
Example #15
Source File: PDFunction.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Create the correct PD Model function based on the COS base function.
 *
 * @param function The COS function dictionary.
 *
 * @return The PDModel Function object.
 *
 * @throws IOException If we are unable to create the PDFunction object.
 */
public static PDFunction create( COSBase function ) throws IOException
{
    if (function == COSName.IDENTITY)
    {
        return new PDFunctionTypeIdentity(null);
    }
    
    COSBase base = function;
    if (function instanceof COSObject)
    {
        base = ((COSObject) function).getObject();
    }
    if (!(base instanceof COSDictionary))
    {
        throw new IOException("Error: Function must be a Dictionary, but is " +
                base.getClass().getSimpleName());
    }
    COSDictionary functionDictionary = (COSDictionary) base;
    int functionType = functionDictionary.getInt(COSName.FUNCTION_TYPE);
    switch (functionType)
    {
        case 0:
            return new PDFunctionType0(functionDictionary);
        case 2:
            return new PDFunctionType2(functionDictionary);
        case 3:
            return new PDFunctionType3(functionDictionary);
        case 4:
            return new PDFunctionType4(functionDictionary);
        default:
            throw new IOException("Error: Unknown function type " + functionType);
    }
}
 
Example #16
Source File: MoveTo.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> operands) throws IOException
{
    if (operands.size() < 2)
    {
        throw new MissingOperandException(operator, operands);
    }
    COSBase base0 = operands.get(0);
    if (!(base0 instanceof COSNumber))
    {
        return;
    }
    COSBase base1 = operands.get(1);
    if (!(base1 instanceof COSNumber))
    {
        return;
    }
    COSNumber x = (COSNumber) base0;
    COSNumber y = (COSNumber) base1;
    Point2D.Float pos = context.transformedPoint(x.floatValue(), y.floatValue());
    context.moveTo(pos.x, pos.y);
}
 
Example #17
Source File: PDOptionalContentProperties.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Indicates whether <em>at least one</em> optional content group with this name is enabled.
 * There may be disabled optional content groups with this name even if this function returns
 * true.
 *
 * @param groupName the group name
 * @return true if at least one group is enabled
 */
public boolean isGroupEnabled(String groupName)
{
    boolean result = false;
    COSArray ocgs = getOCGs();
    for (COSBase o : ocgs)
    {
        COSDictionary ocg = toDictionary(o);
        String name = ocg.getString(COSName.NAME);
        if (groupName.equals(name) && isGroupEnabled(new PDOptionalContentGroup(ocg)))
        {
            result = true;
        }
    }
    return result;
}
 
Example #18
Source File: EditPageContent.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/61202822/remove-large-tokens-from-pdf-using-pdfbox-or-equivalent-library">
 * Remove Large Tokens from PDF using PDFBox or equivalent library
 * </a>
 * <br/>
 * <a href="https://drive.google.com/file/d/184waC6PjjDi8yolIZN5R-6vgWGR5SvKl/view?usp=sharing">
 * kommers_annons_elite.pdf
 * </a>
 * <p>
 * This test shows how to remove text filtered by actual font size.
 * </p>
 */
@Test
public void testRemoveBigTextKommersAnnonsElite() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("kommers_annons_elite.pdf");
            PDDocument document = Loader.loadPDF(resource)) {
        PDPage page = document.getPage(0);
        PdfContentStreamEditor editor = new PdfContentStreamEditor(document, page) {
            @Override
            protected void write(ContentStreamWriter contentStreamWriter, Operator operator, List<COSBase> operands) throws IOException {
                String operatorString = operator.getName();

                if (TEXT_SHOWING_OPERATORS.contains(operatorString))
                {
                    float fs = getGraphicsState().getTextState().getFontSize();
                    Matrix matrix = getTextMatrix().multiply(getGraphicsState().getCurrentTransformationMatrix());
                    Point2D.Float transformedFsVector = matrix.transformPoint(0, fs);
                    Point2D.Float transformedOrigin = matrix.transformPoint(0, 0);
                    double transformedFs = transformedFsVector.distance(transformedOrigin);
                    if (transformedFs > 50)
                        return;
                }

                super.write(contentStreamWriter, operator, operands);
            }

            final List<String> TEXT_SHOWING_OPERATORS = Arrays.asList("Tj", "'", "\"", "TJ");
        };
        editor.processPage(page);
        document.save(new File(RESULT_FOLDER, "kommers_annons_elite-noBigText.pdf"));
    }
}
 
Example #19
Source File: PDOptionalContentProperties.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the collection of all optional content groups.
 * @return the optional content groups
 */
public Collection<PDOptionalContentGroup> getOptionalContentGroups()
{
    Collection<PDOptionalContentGroup> coll = new ArrayList<PDOptionalContentGroup>();
    COSArray ocgs = getOCGs();
    for (COSBase base : ocgs)
    {
        coll.add(new PDOptionalContentGroup(toDictionary(base)));
    }
    return coll;
}
 
Example #20
Source File: PDJavascriptNameTreeNode.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected PDActionJavaScript convertCOSToPD( COSBase base ) throws IOException
{
    if (!(base instanceof COSDictionary))
    {
        throw new IOException( "Error creating Javascript object, expected a COSDictionary and not " + base);
    }
    return (PDActionJavaScript)PDActionFactory.createAction((COSDictionary) base);
}
 
Example #21
Source File: PDDefaultAppearanceString.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Process the set font and font size operator.
 * 
 * @param operands the font name and size
 * @throws IOException in case there are missing operators or the font is not within the resources
 */
private void processSetFont(List<COSBase> operands) throws IOException
{
    if (operands.size() < 2)
    {
        throw new IOException("Missing operands for set font operator " + Arrays.toString(operands.toArray()));
    }

    COSBase base0 = operands.get(0);
    COSBase base1 = operands.get(1);
    if (!(base0 instanceof COSName))
    {
        return;
    }
    if (!(base1 instanceof COSNumber))
    {
        return;
    }
    COSName fontName = (COSName) base0;
    
    PDFont font = defaultResources.getFont(fontName);
    float fontSize = ((COSNumber) base1).floatValue();
    
    // todo: handle cases where font == null with special mapping logic (see PDFBOX-2661)
    if (font == null)
    {
        throw new IOException("Could not find font: /" + fontName.getName());
    }
    setFontName(fontName);
    setFont(font);
    setFontSize(fontSize);
}
 
Example #22
Source File: PDPolylineAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the line with of the border.
 * 
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 * 
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth()
{
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();

    PDBorderStyleDictionary bs = annotation.getBorderStyle();

    if (bs != null)
    {
        return bs.getWidth();
    }

    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3)
    {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber)
        {
            return ((COSNumber) base).floatValue();
        }
    }

    return 1;
}
 
Example #23
Source File: PDExtendedGraphicsState.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will get the transfer function of the /TR dictionary.
 *
 * @return The transfer function. According to the PDF specification, this is either a single
 * function (which applies to all process colorants) or an array of four functions (which apply
 * to the process colorants individually). The name Identity may be used to represent the
 * identity function.
 */
public COSBase getTransfer()
{
    COSBase base = dict.getDictionaryObject(COSName.TR);
    if (base instanceof COSArray && ((COSArray) base).size() != 4)
    {
        return null;
    }
    return base;
}
 
Example #24
Source File: DrawObject.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
{
    if (arguments.isEmpty())
    {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    if (!(base0 instanceof COSName))
    {
        return;
    }
    COSName name = (COSName) base0;

    if (context.getResources().isImageXObject(name))
    {
        // we're done here, don't decode images when doing text extraction
        return;
    }
    
    PDXObject xobject = context.getResources().getXObject(name);

    if (xobject instanceof PDTransparencyGroup)
    {
        context.showTransparencyGroup((PDTransparencyGroup) xobject);
    }
    else if (xobject instanceof PDFormXObject)
    {
        PDFormXObject form = (PDFormXObject) xobject;
        context.showForm(form);
    }
}
 
Example #25
Source File: PDAcroForm.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the XFA resource, the XFA resource is only used for PDF 1.5+ forms.
 *
 * @return The xfa resource or null if it does not exist.
 */
public PDXFAResource getXFA()
{
    PDXFAResource xfa = null;
    COSBase base = dictionary.getDictionaryObject(COSName.XFA);
    if (base != null)
    {
        xfa = new PDXFAResource(base);
    }
    return xfa;
}
 
Example #26
Source File: ExtractImageLocations.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void processOperator(Operator operator, List<COSBase> operands) throws IOException {
    String operation = operator.getName();
    if (fillOperations.contains(operation)) {
        PDColor color = getGraphicsState().getNonStrokingColor();
        PDAbstractPattern pattern = getResources().getPattern(color.getPatternName());
        if (pattern instanceof PDTilingPattern) {
            processTilingPattern((PDTilingPattern) pattern, null, null);
        }
    }
    super.processOperator(operator, operands);
}
 
Example #27
Source File: PdfContentStreamEditor.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void processOperator(Operator operator, List<COSBase> operands) throws IOException {
    if (inOperator) {
        super.processOperator(operator, operands);
    } else {
        inOperator = true;
        nextOperation(operator, operands);
        super.processOperator(operator, operands);
        write(replacement, operator, operands);
        inOperator = false;
    }
}
 
Example #28
Source File: PDAnnotationTextMarkup.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will retrieve the set of quadpoints which encompass the areas of this annotation.
 *
 * @return An array of floats representing the quad points.
 */
public float[] getQuadPoints()
{
    COSBase base = getCOSObject().getDictionaryObject(COSName.QUADPOINTS);
    if (base instanceof COSArray)
    {
        return ((COSArray) base).toFloatArray();
    }
    // Should never happen as this is a required item
    return null; 
}
 
Example #29
Source File: PDShadingPattern.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will get the shading resources for this pattern.
 * @return The shading resources for this pattern.
 * @throws IOException if something went wrong
 */
public PDShading getShading() throws IOException
{
    if (shading == null)
    {
        COSBase base = getCOSObject().getDictionaryObject(COSName.SHADING);
        if (base instanceof COSDictionary)
        {
            shading = PDShading.create((COSDictionary) base);
        }
    }
    return shading;
}
 
Example #30
Source File: PDAnnotationMarkup.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will retrieve the line ending style for the end point, possible values shown in the LE_ constants section.
 *
 * @return The ending style for the end point, LE_NONE if missing, never null.
 */
public String getEndPointEndingStyle()
{
    COSBase base = getCOSObject().getDictionaryObject(COSName.LE);
    if (base instanceof COSArray && ((COSArray) base).size() >= 2)
    {
        return ((COSArray) base).getName(1, PDAnnotationLine.LE_NONE);
    }
    return PDAnnotationLine.LE_NONE;
}