Example usage for com.itextpdf.text.pdf PdfName TYPE

List of usage examples for com.itextpdf.text.pdf PdfName TYPE

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfName TYPE.

Prototype

PdfName TYPE

To view the source code for com.itextpdf.text.pdf PdfName TYPE.

Click Source Link

Document

A name

Usage

From source file:de.gbv.marginalia.Annotation.java

License:Open Source License

/**
 * Serialize the annotation in XML format.
 * The annotation is emitted as stream of SAX events to a ContentHandler.
 * The XML is XFDF with additional Marginalia elements in its own namespace.
 */// www .j ava2s.co  m
public void serializeXML(ContentHandler handler) throws SAXException {
    SimpleXMLCreator xml = new SimpleXMLCreator(handler, namespaces);

    Set<PdfName> allkeys = this.dict.getKeys();
    allkeys.remove(PdfName.TYPE);
    allkeys.remove(PdfName.SUBTYPE);
    allkeys.remove(PdfName.PARENT);
    allkeys.remove(PdfName.CONTENTS);
    allkeys.remove(PdfName.POPUP);

    Map<String, String> attrs = new HashMap<String, String>();
    for (String aName : this.FIELDS.keySet()) {
        Field f = this.FIELDS.get(aName);
        String value = f.getFrom(this.dict);
        if (value != null) { // TODO: encoding & exception
            attrs.put(aName, value);
            //                allkeys.remove( f.name );
        }
    }

    PdfDictionary pg = getAsDictionary(this.dict, PdfName.P);
    allkeys.remove(PdfName.P);
    //CropBox=[0, 0, 595, 842]
    //Rotate
    //MediaBox=[0, 0, 595, 842]
    // TODO: find out where page number is stored
    if (attrs.get("page") == null)
        attrs.put("page", "" + this.pageNum);

    String element = subtypes.get(this.subtype);
    if (element == null) { // TODO
        element = this.subtype.toString();
    }

    xml.startElement(element, attrs);

    if (element.equals("ink")) {
        PdfArray inklist = this.dict.getAsArray(new PdfName("InkList"));
        if (inklist != null) {
            xml.startElement("inklist");
            for (int i = 0; i < inklist.size(); i++) {
                PdfArray pathArray = inklist.getAsArray(i);
                String s = "";
                for (int j = 0; j < pathArray.size(); j += 2) {
                    if (j > 0)
                        s += ";";
                    s += "" + pathArray.getAsNumber(j).floatValue() + ",";
                    s += "" + pathArray.getAsNumber(j + 1).floatValue();
                }
                xml.contentElement("gesture", s);
            }
            xml.endElement();
        }
    }

    if (attrs.get("rect") != null) {
        Map<String, String> a = new HashMap<String, String>();
        RectField rf = (RectField) this.FIELDS.get("rect");
        PdfRectangle r = null;
        if (rf != null)
            r = (PdfRectangle) rf.getObjectFrom(this.dict);
        if (r != null) {
            a.put("left", "" + r.left());
            a.put("bottom", "" + r.bottom());
            a.put("right", "" + r.right());
            a.put("top", "" + r.top());
            xml.emptyElement("m", "rect", a);
        }
    }

    if (this.content != null && !this.content.equals("")) {
        // TODO: encode content if not UTF-8 ?
        xml.contentElement("content", content.toString());
    }
    // TODO: contents-richtext
    // TODO: popup
    /*
          if ( this.popup != null ) {
            out.println("<!--popup>");
            for ( PdfName n : this.popup.getKeys() ) {
               out.println( n + "=" + this.popup.getDirectObject(n) );
            }
            out.println("</popup-->");
          }
    */
    // remaining dictionary elements
    /*
            for ( PdfName name : allkeys ) {
    Map<String,String> a = new HashMap<String,String>();
    a.put("name",name.toString());
    a.put("value",this.dict.getDirectObject(name).toString());
    xml.emptyElement( "m","unknown", a );
            }
    */
    xml.endElement();
}

From source file:org.h819.commons.file.MyPDFUtils.java

/**
 *  pdf ? pdf/*from   w w  w . j  a  va 2  s  .  c o m*/
 *
 * @param srcPdf      the original PDF
 * @param destPdf     the resulting PDF
 * @param imageFactor The multiplication factor for the image (?  imageFacto =0.5f)
 * @throws IOException
 * @throws DocumentException
 */
public static void compressPdf(File srcPdf, File destPdf, float imageFactor)
        throws IOException, DocumentException {

    PdfReader reader = new PdfReader(srcPdf.getAbsolutePath());
    int n = reader.getXrefSize();
    PdfObject object;
    PRStream stream;
    // Look for image and manipulate image stream
    for (int i = 0; i < n; i++) {
        object = reader.getPdfObject(i);
        if (object == null || !object.isStream())
            continue;
        stream = (PRStream) object;
        if (!PdfName.IMAGE.equals(stream.getAsName(PdfName.SUBTYPE)))
            continue;
        if (!PdfName.DCTDECODE.equals(stream.getAsName(PdfName.FILTER)))
            continue;
        PdfImageObject image = new PdfImageObject(stream);
        BufferedImage bi = image.getBufferedImage();
        if (bi == null)
            continue;
        int width = (int) (bi.getWidth() * imageFactor);
        int height = (int) (bi.getHeight() * imageFactor);
        if (width <= 0 || height <= 0)
            continue;
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        AffineTransform at = AffineTransform.getScaleInstance(imageFactor, imageFactor);
        Graphics2D g = img.createGraphics();
        g.drawRenderedImage(bi, at);
        ByteArrayOutputStream imgBytes = new ByteArrayOutputStream();
        ImageIO.write(img, "JPG", imgBytes);
        stream.clear();
        stream.setData(imgBytes.toByteArray(), false, PRStream.NO_COMPRESSION);
        stream.put(PdfName.TYPE, PdfName.XOBJECT);
        stream.put(PdfName.SUBTYPE, PdfName.IMAGE);
        stream.put(PdfName.FILTER, PdfName.DCTDECODE);
        stream.put(PdfName.WIDTH, new PdfNumber(width));
        stream.put(PdfName.HEIGHT, new PdfNumber(height));
        stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
        stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB);
    }
    reader.removeUnusedObjects();
    // Save altered PDF
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPdf.getAbsolutePath()));
    stamper.setFullCompression();
    stamper.close();
    reader.close();
}

From source file:org.orbisgis.core_export.GeoSpatialPDF.java

License:Open Source License

/**
 * This method is used to georeference the pdf.
 * Note : The CRS is not yet supported./*  w w  w.  j  av a  2s .  c  om*/
 *
 * @param writer
 * @param mt
 * @throws IOException
 * @throws DocumentException
 */
public void georefPdf(PdfWriter writer, MapTransform mt) throws IOException, DocumentException {

    PdfStructureTreeRoot tree = writer.getStructureTreeRoot();

    //the part of the document where maps are displayed
    float mapWidth = width;
    float mapHeight = height;
    float mapLX = 0;
    float mapLY = 0;

    //ViewPort Dictionary
    PdfDictionary viewPortDict = new PdfDictionary();
    viewPortDict.put(PdfName.TYPE, new PdfName("Viewport"));
    viewPortDict.put(PdfName.BBOX, new PdfRectangle(mapLX, mapLY, mapLX + mapWidth, mapLY + mapHeight));
    viewPortDict.put(PdfName.NAME, new PdfString("Layers"));

    //Measure dictionary
    PdfDictionary measureDict = new PdfDictionary();
    measureDict.put(PdfName.TYPE, new PdfName("Measure"));
    measureDict.put(PdfName.SUBTYPE, new PdfName("GEO"));

    //Bounds
    PdfArray bounds = new PdfArray();
    bounds.add(new PdfNumber(0));
    bounds.add(new PdfNumber(0));
    bounds.add(new PdfNumber(1));
    bounds.add(new PdfNumber(0));
    bounds.add(new PdfNumber(1));
    bounds.add(new PdfNumber(1));
    bounds.add(new PdfNumber(0));
    bounds.add(new PdfNumber(1));

    measureDict.put(new PdfName("Bounds"), bounds);

    //GPTS
    Envelope adjustedBbox = mt.getAdjustedExtent();

    if (!adjustedBbox.isNull()) {

        //ly lx ly ux uy ux uy lx
        PdfArray gptsTable = new PdfArray();
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinY()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinX()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinY()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxX()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxY()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxX()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxY()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinX()).toString()));

        measureDict.put(new PdfName("GPTS"), gptsTable);

        //The CRS will be added when the mapcontext will support it.

        //            //GCS Geospatial Coordinate system
        //            PdfDictionary gcsDict = new PdfDictionary();
        //            if (context.getBbox().getCrs() != null) {
        //
        //                if (context.getBbox().getCrs().getType() != null) {
        //                    gcsDict.put(PdfName.TYPE, new PdfName(context.getBbox().getCrs().getType()));
        //                } else {
        //                    LOGGER.warn("No type of crs : the pdf cannot be georeferenced");
        //                    return;
        //                }
        //
        //                if (context.getBbox().getCrs().getEpsg() != 0) {
        //                    gcsDict.put(new PdfName("EPSG"), new PdfNumber(context.getBbox().getCrs().getEpsg()));
        //                } else {
        //                    LOGGER.warn("No epsg : the pdf cannot be georeferenced");
        //                    return;
        //                }
        //
        //
        //            } else {
        //                LOGGER.warn("No crs :  the pdf cannot be georeferenced");
        //
        //            }
        //
        //            measureDict.put(new PdfName("GCS"), gcsDict);
    } else {
        LOGGER.warn("Envelope of bbox null : the pdf cannot be georeferenced");

    }

    //PDU : array of units
    PdfArray pdu = new PdfArray();
    pdu.add(new PdfName("KM"));
    pdu.add(new PdfName("SQKM"));
    pdu.add(new PdfName("DEG"));

    measureDict.put(new PdfName("PDU"), pdu);

    //LPTS
    PdfArray lptsTable = new PdfArray();
    lptsTable.add(new PdfNumber(0));
    lptsTable.add(new PdfNumber(0));
    lptsTable.add(new PdfNumber(1));
    lptsTable.add(new PdfNumber(0));
    lptsTable.add(new PdfNumber(1));
    lptsTable.add(new PdfNumber(1));
    lptsTable.add(new PdfNumber(0));
    lptsTable.add(new PdfNumber(1));

    measureDict.put(new PdfName("LPTS"), lptsTable);

    viewPortDict.put(new PdfName("Measure"), measureDict);

    PdfStructureElement top = new PdfStructureElement(tree, new PdfName("VP"));
    top.putAll(viewPortDict);
}

From source file:org.sejda.impl.itext5.component.PdfUnpacker.java

License:Open Source License

private void unpack(Set<PdfDictionary> dictionaries) throws TaskIOException {
    for (PdfDictionary dictionary : dictionaries) {
        PdfName type = dictionary.getAsName(PdfName.TYPE);
        if (PdfName.F.equals(type) || PdfName.FILESPEC.equals(type)) {
            PdfDictionary ef = dictionary.getAsDict(PdfName.EF);
            PdfString fn = dictionary.getAsString(PdfName.F);
            if (fn != null && ef != null) {
                PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
                if (prs != null) {
                    File tmpFile = copyToTemporaryFile(prs);
                    outputWriter.addOutput(file(tmpFile).name(fn.toUnicodeString()));
                }/* w w w  .  j a va2 s  .  co  m*/
            }
        }
    }
}