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

The following examples show how to use org.apache.pdfbox.cos.COSDictionary#setName() . 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: CreatePortableCollection.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/46642994/how-to-create-pdf-package-using-pdfbox">
 * How to create pdf package using PdfBox?
 * </a>
 * <p>
 * This test implements the equivalent of the OP's code turning
 * a regular PDF into a portable collection.
 * </p>
 */
@Test
public void test() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("/mkl/testarea/pdfbox2/sign/test.pdf"))
    {
        PDDocument pdDocument = Loader.loadPDF(resource);

        COSDictionary collectionDictionary = new COSDictionary();
        collectionDictionary.setName(COSName.TYPE, "Collection");
        collectionDictionary.setName("View", "T");
        PDDocumentCatalog catalog = pdDocument.getDocumentCatalog();
        catalog.getCOSObject().setItem("Collection", collectionDictionary);

        pdDocument.save(new File(RESULT_FOLDER, "test-collection.pdf"));
    }
}
 
Example 2
Source File: PDStructureTreeRoot.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets the role map.
 * 
 * @param roleMap the role map
 */
public void setRoleMap(Map<String, String> roleMap)
{
    COSDictionary rmDic = new COSDictionary();
    for (Map.Entry<String, String> entry : roleMap.entrySet())
    {
        rmDic.setName(entry.getKey(), entry.getValue());
    }
    this.getCOSObject().setItem(COSName.ROLE_MAP, rmDic);
}
 
Example 3
Source File: TrueTypeEmbedder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new TrueType font for embedding.
 */
@SuppressWarnings("deprecation")
TrueTypeEmbedder(PDDocument document, COSDictionary dict, TrueTypeFont ttf,
                 boolean embedSubset) throws IOException
{
    this.document = document;
    this.embedSubset = embedSubset;
    this.ttf = ttf;
    fontDescriptor = createFontDescriptor(ttf);

    if (!isEmbeddingPermitted(ttf))
    {
        throw new IOException("This font does not permit embedding");
    }

    if (!embedSubset)
    {
        // full embedding
        PDStream stream = new PDStream(document, ttf.getOriginalData(), COSName.FLATE_DECODE);
        stream.getCOSObject().setLong(COSName.LENGTH1, ttf.getOriginalDataSize());
        fontDescriptor.setFontFile2(stream);
    }

    dict.setName(COSName.BASE_FONT, ttf.getName());

    // choose a Unicode "cmap"
    cmap = ttf.getUnicodeCmap();
    cmapLookup = ttf.getUnicodeCmapLookup();
}
 
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: PDCIDFontType2Embedder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private COSDictionary createCIDFont() throws IOException
{
    COSDictionary cidFont = new COSDictionary();

    // Type, Subtype
    cidFont.setItem(COSName.TYPE, COSName.FONT);
    cidFont.setItem(COSName.SUBTYPE, COSName.CID_FONT_TYPE2);

    // BaseFont
    cidFont.setName(COSName.BASE_FONT, fontDescriptor.getFontName());

    // CIDSystemInfo
    COSDictionary info = toCIDSystemInfo("Adobe", "Identity", 0);
    cidFont.setItem(COSName.CIDSYSTEMINFO, info);

    // FontDescriptor
    cidFont.setItem(COSName.FONT_DESC, fontDescriptor.getCOSObject());

    // W - widths
    buildWidths(cidFont);

    // Vertical metrics
    if (vertical)
    {
        buildVerticalMetrics(cidFont);
    }

    // CIDToGIDMap
    cidFont.setItem(COSName.CID_TO_GID_MAP, COSName.IDENTITY);

    return cidFont;
}
 
Example 6
Source File: PDThreadBead.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 *
 */
public PDThreadBead()
{
    bead = new COSDictionary();
    bead.setName( "Type", "Bead" );
    setNextBead( this );
    setPreviousBead( this );
}
 
Example 7
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 8
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 9
Source File: PDAbstractPattern.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a new Pattern dictionary.
 */
public PDAbstractPattern()
{
    patternDictionary = new COSDictionary();
    patternDictionary.setName(COSName.TYPE, COSName.PATTERN.getName());
}
 
Example 10
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 11
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 12
Source File: PDThread.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 *
 */
public PDThread()
{
    thread = new COSDictionary();
    thread.setName( "Type", "Thread" );
}