Java Code Examples for org.apache.pdfbox.cos.COSDictionary#setInt()

The following examples show how to use org.apache.pdfbox.cos.COSDictionary#setInt() . 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: CCITTFactory.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private static PDImageXObject prepareImageXObject(PDDocument document,
        byte[] byteArray, int width, int height,
        PDColorSpace initColorSpace) throws IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Filter filter = FilterFactory.INSTANCE.getFilter(COSName.CCITTFAX_DECODE);
    COSDictionary dict = new COSDictionary();
    dict.setInt(COSName.COLUMNS, width);
    dict.setInt(COSName.ROWS, height);
    filter.encode(new ByteArrayInputStream(byteArray), baos, dict, 0);

    ByteArrayInputStream encodedByteStream = new ByteArrayInputStream(baos.toByteArray());
    PDImageXObject image = new PDImageXObject(document, encodedByteStream, COSName.CCITTFAX_DECODE,
            width, height, 1, initColorSpace);
    dict.setInt(COSName.K, -1);
    image.getCOSObject().setItem(COSName.DECODE_PARMS, dict);
    return image;
}
 
Example 2
Source File: PDCIDFontType2Embedder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private COSDictionary toCIDSystemInfo(String registry, String ordering, int supplement)
{
    COSDictionary info = new COSDictionary();
    info.setString(COSName.REGISTRY, registry);
    info.setString(COSName.ORDERING, ordering);
    info.setInt(COSName.SUPPLEMENT, supplement);
    return info;
}
 
Example 3
Source File: PDTrueTypeFontEmbedder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets the glyph widths in the font dictionary.
 */
private void setWidths(COSDictionary font, GlyphList glyphList) throws IOException
{
    float scaling = 1000f / ttf.getHeader().getUnitsPerEm();
    HorizontalMetricsTable hmtx = ttf.getHorizontalMetrics();

    Map<Integer, String> codeToName = getFontEncoding().getCodeToNameMap();

    int firstChar = Collections.min(codeToName.keySet());
    int lastChar = Collections.max(codeToName.keySet());

    List<Integer> widths = new ArrayList<Integer>(lastChar - firstChar + 1);
    for (int i = 0; i < lastChar - firstChar + 1; i++)
    {
        widths.add(0);
    }

    // a character code is mapped to a glyph name via the provided font encoding
    // afterwards, the glyph name is translated to a glyph ID.
    for (Map.Entry<Integer, String> entry : codeToName.entrySet())
    {
        int code = entry.getKey();
        String name = entry.getValue();

        if (code >= firstChar && code <= lastChar)
        {
            String uni = glyphList.toUnicode(name);
            int charCode = uni.codePointAt(0);
            int gid = cmapLookup.getGlyphId(charCode);
            widths.set(entry.getKey() - firstChar,
                       Math.round(hmtx.getAdvanceWidth(gid) * scaling));
        }
    }

    font.setInt(COSName.FIRST_CHAR, firstChar);
    font.setInt(COSName.LAST_CHAR, lastChar);
    font.setItem(COSName.WIDTHS, COSArrayList.converterToCOSArray(widths));
}
 
Example 4
Source File: PDCIDSystemInfo.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
PDCIDSystemInfo(String registry, String ordering, int supplement)
{
    dictionary = new COSDictionary();
    dictionary.setString(COSName.REGISTRY, registry);
    dictionary.setString(COSName.ORDERING, ordering);
    dictionary.setInt(COSName.SUPPLEMENT, supplement);
}
 
Example 5
Source File: AddSpecialCharacterWithoutEmbedding.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
COSDictionary buildUnembeddedArialWithSpecialEncoding() {
    COSArray differences = new COSArray();
    differences.add(COSInteger.get(32));
    differences.add(COSName.getPDFName("uniAB55"));

    COSDictionary fontDescDict = new COSDictionary();
    fontDescDict.setName("Type", "FontDescriptor");
    fontDescDict.setName("FontName", "Arial");
    fontDescDict.setString("FontFamily", "Arial");
    fontDescDict.setInt("Flags", 32);
    fontDescDict.setItem("FontBBox", new PDRectangle(-665, -325, 2665, 1365));
    fontDescDict.setInt("ItalicAngle", 0);
    fontDescDict.setInt("Ascent", 1040);
    fontDescDict.setInt("Descent", -325);
    fontDescDict.setInt("CapHeight", 716);
    fontDescDict.setInt("StemV", 88);
    fontDescDict.setInt("XHeight", 519);

    COSDictionary encodingDict = new COSDictionary();
    encodingDict.setName("Type", "Encoding");
    encodingDict.setName("BaseEncoding", "WinAnsiEncoding");
    encodingDict.setItem("Differences", differences);

    COSArray widths = new COSArray();
    widths.add(COSInteger.get(500));

    COSDictionary fontDict = new COSDictionary();
    fontDict.setName("Type", "Font");
    fontDict.setName("Subtype", "TrueType");
    fontDict.setName("BaseFont", "Arial");
    fontDict.setInt("FirstChar", 32);
    fontDict.setInt("LastChar", 32);
    fontDict.setItem("Widths", widths);
    fontDict.setItem("FontDescriptor", fontDescDict);
    fontDict.setItem("Encoding", encodingDict);

    return fontDict;
}
 
Example 6
Source File: PdfBoxSignatureService.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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.
 * @param signature         The signature object.
 * @param accessPermissions The permission value (1, 2 or 3).
 */
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("TransformParams", transformParameters);
	referenceDict.setNeedToBeUpdated(true);

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

	// Document 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);
}
 
Example 7
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private int checkPagesDictionary(COSDictionary pagesDict, Set<COSObject> set)
{
    // check for kids
    COSBase kids = pagesDict.getDictionaryObject(COSName.KIDS);
    int numberOfPages = 0;
    if (kids instanceof COSArray)
    {
        COSArray kidsArray = (COSArray) kids;
        List<? extends COSBase> kidsList = kidsArray.toList();
        for (COSBase kid : kidsList)
        {
            if (!(kid instanceof COSObject) || set.contains((COSObject) kid))
            {
                kidsArray.remove(kid);
                continue;
            }
            COSObject kidObject = (COSObject) kid;
            COSBase kidBaseobject = kidObject.getObject();
            // object wasn't dereferenced -> remove it
            if (kidBaseobject == null || kidBaseobject.equals(COSNull.NULL))
            {
                LOG.warn("Removed null object " + kid + " from pages dictionary");
                kidsArray.remove(kid);
            }
            else if (kidBaseobject instanceof COSDictionary)
            {
                COSDictionary kidDictionary = (COSDictionary) kidBaseobject;
                COSName type = kidDictionary.getCOSName(COSName.TYPE);
                if (COSName.PAGES.equals(type))
                {
                    // process nested pages dictionaries
                    set.add(kidObject);
                    numberOfPages += checkPagesDictionary(kidDictionary, set);
                }
                else if (COSName.PAGE.equals(type))
                {
                    // count pages
                    numberOfPages++;
                }
            }
        }
    }
    // fix counter
    pagesDict.setInt(COSName.COUNT, numberOfPages);
    return numberOfPages;
}
 
Example 8
Source File: FDFAnnotationStamp.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private COSDictionary parseDictElement(Element dictEl) throws IOException
{
    LOG.debug("Parse " + dictEl.getAttribute("KEY") + " Dictionary");
    COSDictionary dict = new COSDictionary();

    NodeList nodeList = dictEl.getChildNodes();
    String parentAttrKey = dictEl.getAttribute("KEY");

    for (int i = 0; i < nodeList.getLength(); i++)
    {
        Node node = nodeList.item(i);
        if (node instanceof Element)
        {
            Element child = (Element) node;
            String childAttrKey = child.getAttribute("KEY");
            String childAttrVal = child.getAttribute("VAL");

            if ("DICT".equals(child.getTagName()))
            {
                LOG.debug(parentAttrKey + " => Handling DICT element with key: " + childAttrKey);
                dict.setItem(COSName.getPDFName(childAttrKey), parseDictElement(child));
                LOG.debug(parentAttrKey + " => Set " + childAttrKey);
            }
            else if ("STREAM".equals(child.getTagName()))
            {
                LOG.debug(parentAttrKey + " => Handling STREAM element with key: " + childAttrKey);
                dict.setItem(COSName.getPDFName(childAttrKey), parseStreamElement(child));
            }
            else if ("NAME".equals(child.getTagName()))
            {
                LOG.debug(parentAttrKey + " => Handling NAME element with key: " + childAttrKey);
                dict.setName(COSName.getPDFName(childAttrKey), childAttrVal);
                LOG.debug(parentAttrKey + " => Set " + childAttrKey + ": " + childAttrVal);
            }
            else if ("INT".equalsIgnoreCase(child.getTagName()))
            {
                dict.setInt(COSName.getPDFName(childAttrKey), Integer.parseInt(childAttrVal));
                LOG.debug(parentAttrKey + " => Set " + childAttrKey + ": " + childAttrVal);
            }
            else if ("FIXED".equalsIgnoreCase(child.getTagName()))
            {
                dict.setFloat(COSName.getPDFName(childAttrKey), Float.parseFloat(childAttrVal));
                LOG.debug(parentAttrKey + " => Set " + childAttrKey + ": " + childAttrVal);
            }
            else if ("BOOL".equalsIgnoreCase(child.getTagName()))
            {
                dict.setBoolean(COSName.getPDFName(childAttrKey), Boolean.parseBoolean(childAttrVal));
                LOG.debug(parentAttrKey + " => Set " + childAttrVal);
            }
            else if ("ARRAY".equalsIgnoreCase(child.getTagName()))
            {
                dict.setItem(COSName.getPDFName(childAttrKey), parseArrayElement(child));
                LOG.debug(parentAttrKey + " => Set " + childAttrKey);
            }
            else
            {
                LOG.warn(parentAttrKey + " => NOT handling child element: " + child.getTagName());
            }
        }
    }

    return dict;
}
 
Example 9
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);
}