Search in sources :

Example 11 with PdfNumber

use of com.lowagie.text.pdf.PdfNumber in project OpenPDF by LibrePDF.

the class Add3D method execute.

/**
 * Executes the tool (in most cases this generates a PDF file).
 */
public void execute() {
    try {
        if (getValue("srcfile") == null) {
            throw new InstantiationException("You need to choose a sourcefile");
        }
        if (getValue("srcu3dfile") == null) {
            throw new InstantiationException("You need to choose a u3d file");
        }
        if (getValue("destfile") == null) {
            throw new InstantiationException("You need to choose a destination file");
        }
        int pagenumber = Integer.parseInt((String) getValue("pagenumber"));
        // Create 3D annotation
        // Required definitions
        PdfIndirectReference streamRef;
        PdfIndirectObject objRef;
        PdfReader reader = new PdfReader(((File) getValue("srcfile")).getAbsolutePath());
        String u3dFileName = ((File) getValue("srcu3dfile")).getAbsolutePath();
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream((File) getValue("destfile")));
        PdfWriter wr = stamp.getWriter();
        PdfContentByte cb = stamp.getUnderContent(pagenumber);
        Rectangle rectori = reader.getCropBox(pagenumber);
        Rectangle rect = new Rectangle(new Rectangle(100, rectori.getHeight() - 550, rectori.getWidth() - 100, rectori.getHeight() - 150));
        PdfStream oni = new PdfStream(PdfEncodings.convertToBytes("runtime.setCurrentTool(\"Rotate\");", null));
        oni.flateCompress();
        // Create stream to carry attachment
        PdfStream stream = new PdfStream(new FileInputStream(u3dFileName), wr);
        stream.put(new PdfName("OnInstantiate"), wr.addToBody(oni).getIndirectReference());
        // Mandatory keys
        stream.put(PdfName.TYPE, new PdfName(PDF_NAME_3D));
        stream.put(PdfName.SUBTYPE, new PdfName(PDF_NAME_U3D));
        stream.flateCompress();
        // Write stream contents, get reference to stream object, write actual stream length
        streamRef = wr.addToBody(stream).getIndirectReference();
        stream.writeLength();
        // Create 3D view dictionary
        // PDF documentation states that this can be left out, but without normally we will just get a blank 3D image because of wrong coordinate space transformations, etc.
        // Instead of providing camera-to-world transformation here, we could also reference view in U3D file itself (would be U3DPath key instead of C2W key, U3D value instead of M value for MS key), but i haven't tried up to now
        // We could also provide an activation dictionary (defining activation behavior), and field-of-view for P entry if needed
        PdfDictionary dict = new PdfDictionary(new PdfName(PDF_NAME_3DVIEW));
        dict.put(new PdfName(PDF_NAME_XN), new PdfString("Default"));
        dict.put(new PdfName(PDF_NAME_IN), new PdfString("Unnamed"));
        // States that we have to provide camera-to-world coordinate transformation
        dict.put(new PdfName(PDF_NAME_MS), PdfName.M);
        dict.put(new PdfName(PDF_NAME_C2W), new PdfArray(new float[] { 1, 0, 0, 0, 0, -1, 0, 1, 0, 3, -235, // 3d transformation matrix (demo for teapot)
        28F }));
        // Camera distance along z-axis (demo for teapot)
        dict.put(PdfName.CO, new PdfNumber(235));
        // Write view dictionary, get reference
        objRef = wr.addToBody(dict);
        // Create appearance
        PdfAppearance ap = cb.createAppearance(rect.getRight() - rect.getLeft(), rect.getTop() - rect.getBottom());
        ap.setBoundingBox(rect);
        // Create annotation with reference to stream
        PdfAnnotation annot = new PdfAnnotation(wr, rect);
        annot.put(PdfName.CONTENTS, new PdfString("3D Model"));
        // Mandatory keys
        annot.put(PdfName.SUBTYPE, new PdfName(PDF_NAME_3D));
        annot.put(PdfName.TYPE, PdfName.ANNOT);
        // Reference to stream object
        annot.put(new PdfName(PDF_NAME_3DD), streamRef);
        // Reference to view dictionary object
        annot.put(new PdfName(PDF_NAME_3DV), objRef.getIndirectReference());
        annot.put(new PdfName("3DI"), PdfBoolean.PDFFALSE);
        PdfDictionary adi = new PdfDictionary();
        adi.put(PdfName.A, new PdfName("PO"));
        adi.put(new PdfName("DIS"), PdfName.I);
        annot.put(new PdfName("3DA"), adi);
        // Assign appearance and page
        annot.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
        annot.setPage();
        // Actually write annotation
        stamp.addAnnotation(annot, pagenumber);
        AddButton(100, 100, "Rotate", "im = this.getAnnots3D(0)[0].context3D;\rim.runtime.setCurrentTool(\"Rotate\");", "rotate.png", wr);
        AddButton(150, 100, "Pan", "im = this.getAnnots3D(0)[0].context3D;\rim.runtime.setCurrentTool(\"Pan\");", "translate.png", wr);
        AddButton(200, 100, "Zoom", "im = this.getAnnots3D(0)[0].context3D;\rim.runtime.setCurrentTool(\"Zoom\");", "zoom.png", wr);
        stamp.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(internalFrame, e.getMessage(), e.getClass().getName(), JOptionPane.ERROR_MESSAGE);
        System.err.println(e.getMessage());
    }
}
Also used : PdfArray(com.lowagie.text.pdf.PdfArray) PdfWriter(com.lowagie.text.pdf.PdfWriter) PdfName(com.lowagie.text.pdf.PdfName) PdfIndirectObject(com.lowagie.text.pdf.PdfIndirectObject) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) Rectangle(com.lowagie.text.Rectangle) PdfAppearance(com.lowagie.text.pdf.PdfAppearance) PdfIndirectReference(com.lowagie.text.pdf.PdfIndirectReference) PdfNumber(com.lowagie.text.pdf.PdfNumber) PdfReader(com.lowagie.text.pdf.PdfReader) PdfString(com.lowagie.text.pdf.PdfString) PdfString(com.lowagie.text.pdf.PdfString) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) PdfStamper(com.lowagie.text.pdf.PdfStamper) PdfAnnotation(com.lowagie.text.pdf.PdfAnnotation) FileOutputStream(java.io.FileOutputStream) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) PdfStream(com.lowagie.text.pdf.PdfStream) File(java.io.File)

Example 12 with PdfNumber

use of com.lowagie.text.pdf.PdfNumber in project OpenPDF by LibrePDF.

the class Image method getInstance.

/**
 * Reuses an existing image.
 * @param ref the reference to the image dictionary
 * @throws BadElementException on error
 * @return the image
 */
public static Image getInstance(PRIndirectReference ref) throws BadElementException {
    PdfDictionary dic = (PdfDictionary) PdfReader.getPdfObjectRelease(ref);
    int width = ((PdfNumber) PdfReader.getPdfObjectRelease(dic.get(PdfName.WIDTH))).intValue();
    int height = ((PdfNumber) PdfReader.getPdfObjectRelease(dic.get(PdfName.HEIGHT))).intValue();
    Image imask = null;
    PdfObject obj = dic.get(PdfName.SMASK);
    if (obj != null && obj.isIndirect()) {
        imask = getInstance((PRIndirectReference) obj);
    } else {
        obj = dic.get(PdfName.MASK);
        if (obj != null && obj.isIndirect()) {
            PdfObject obj2 = PdfReader.getPdfObjectRelease(obj);
            if (obj2 instanceof PdfDictionary)
                imask = getInstance((PRIndirectReference) obj);
        }
    }
    Image img = new ImgRaw(width, height, 1, 1, null);
    img.imageMask = imask;
    img.directReference = ref;
    return img;
}
Also used : PRIndirectReference(com.lowagie.text.pdf.PRIndirectReference) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) PdfObject(com.lowagie.text.pdf.PdfObject) PdfNumber(com.lowagie.text.pdf.PdfNumber) BufferedImage(java.awt.image.BufferedImage)

Example 13 with PdfNumber

use of com.lowagie.text.pdf.PdfNumber in project JavaClasses by genexuslabs.

the class PDFReportItext method GxEndDocument.

public void GxEndDocument() {
    if (document.getPageNumber() == 0) {
        // Si no hay ninguna página en el documento, agrego una vacia}
        writer.setPageEmpty(false);
    }
    // Ahora proceso los comandos GeneXus {{Pages}}
    if (template != null) {
        template.beginText();
        template.setFontAndSize(templateFont, templateFontSize);
        template.setTextMatrix(0, 0);
        template.setColorFill(templateColorFill);
        template.showText(String.valueOf(pages));
        template.endText();
    }
    int copies = 1;
    try {
        copies = Integer.parseInt(printerSettings.getProperty(form, Const.COPIES));
        if (DEBUG)
            DEBUG_STREAM.println("Setting number of copies to " + copies);
        writer.addViewerPreference(PdfName.NUMCOPIES, new PdfNumber(copies));
        int duplex = Integer.parseInt(printerSettings.getProperty(form, Const.DUPLEX));
        PdfName duplexValue;
        switch(duplex) {
            case 1:
                duplexValue = PdfName.SIMPLEX;
                break;
            case 2:
                duplexValue = PdfName.DUPLEX;
                break;
            case 3:
                duplexValue = PdfName.DUPLEXFLIPSHORTEDGE;
                break;
            case 4:
                duplexValue = PdfName.DUPLEXFLIPLONGEDGE;
                break;
            default:
                duplexValue = PdfName.NONE;
        }
        if (DEBUG)
            DEBUG_STREAM.println("Setting duplex to " + duplexValue.toString());
        writer.addViewerPreference(PdfName.DUPLEX, duplexValue);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
    String serverPrinting = props.getGeneralProperty(Const.SERVER_PRINTING);
    boolean fit = props.getGeneralProperty(Const.ADJUST_TO_PAPER).equals("true");
    if ((outputType == Const.OUTPUT_PRINTER || outputType == Const.OUTPUT_STREAM_PRINTER) && (httpContext instanceof HttpContextWeb && serverPrinting.equals("false"))) {
        // writer.addJavaScript("if (this.external)\n");//Specifies whether the current document is being viewed in the Acrobat application or in an external window (such as a web browser).
        // writer.addJavaScript("app.alert('SI es externa' + this.external);");
        writer.addJavaScript("var pp = this.getPrintParams();\n");
        // writer.addJavaScript("pp.interactive = pp.constants.interactionLevel.automatic;\n");
        String printerAux = printerSettings.getProperty(form, Const.PRINTER);
        String printer = replace(printerAux, "\\", "\\\\");
        if (printer != null && !printer.equals("")) {
            writer.addJavaScript("pp.printerName = \"" + printer + "\";\n");
        }
        if (fit) {
            writer.addJavaScript("pp.pageHandling = pp.constants.handling.fit;\n");
        } else {
            writer.addJavaScript("pp.pageHandling = pp.constants.handling.none;\n");
        }
        if (// Show printer dialog Never
        printerSettings.getProperty(form, Const.MODE, "3").startsWith("0")) {
            writer.addJavaScript("pp.interactive = pp.constants.interactionLevel.automatic;\n");
            // dialog is displayed and removed automatically when printing is complete.
            for (int i = 0; i < copies; i++) {
                writer.addJavaScript("this.print(pp);\n");
            }
        } else // Show printer dialog is sent directly to printer | always
        {
            writer.addJavaScript("pp.interactive = pp.constants.interactionLevel.full;\n");
            // Displays the print dialog allowing the user to change print settings and requiring
            // the user to press OK to continue. During printing a progress monitor and cancel
            // dialog is displayed and removed automatically when printing is complete.
            writer.addJavaScript("this.print(pp);\n");
        }
    }
    document.close();
    if (DEBUG)
        DEBUG_STREAM.println("GxEndDocument!");
    try {
        props.save();
    } catch (IOException e) {
        ;
    }
    // OK, ahora que ya terminamos el PDF, vemos si tenemos que mostrarlo en pantalla
    switch(outputType) {
        case Const.OUTPUT_SCREEN:
            // Cierro el archivo
            try {
                outputStream.close();
            } catch (IOException e) {
                ;
            }
            try {
                showReport(docName, modal);
            } catch (Exception e) {
                // Si no se puede mostrar el reporte
                e.printStackTrace();
            }
            // if(modal)if(new File(docName).delete())TemporaryFiles.getInstance().removeFileFromList(docName); // Intento eliminar el docName aqu\uFFFDE
            break;
        case Const.OUTPUT_PRINTER:
            // Cierro el archivo
            try {
                outputStream.close();
            } catch (IOException e) {
                ;
            }
            try {
                if (!(httpContext instanceof HttpContextWeb) || !serverPrinting.equals("false")) {
                    printReport(docName, this.printerOutputMode == 1);
                }
            } catch (Exception e) {
                // Si no se puede mostrar el reporte
                e.printStackTrace();
            }
            break;
        case Const.OUTPUT_FILE:
            // Cierro el archivo
            try {
                outputStream.close();
            } catch (IOException e) {
                ;
            }
            break;
        case Const.OUTPUT_STREAM:
        case Const.OUTPUT_STREAM_PRINTER:
        default:
            break;
    }
    outputStream = null;
}
Also used : PdfName(com.lowagie.text.pdf.PdfName) PdfNumber(com.lowagie.text.pdf.PdfNumber) IOException(java.io.IOException) HttpContextWeb(com.genexus.webpanels.HttpContextWeb) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException)

Example 14 with PdfNumber

use of com.lowagie.text.pdf.PdfNumber in project itext2 by albfernandez.

the class Image method getInstance.

/**
 * Reuses an existing image.
 * @param ref the reference to the image dictionary
 * @throws BadElementException on error
 * @return the image
 */
public static Image getInstance(PRIndirectReference ref) throws BadElementException {
    PdfDictionary dic = (PdfDictionary) PdfReader.getPdfObjectRelease(ref);
    int width = ((PdfNumber) PdfReader.getPdfObjectRelease(dic.get(PdfName.WIDTH))).intValue();
    int height = ((PdfNumber) PdfReader.getPdfObjectRelease(dic.get(PdfName.HEIGHT))).intValue();
    Image imask = null;
    PdfObject obj = dic.get(PdfName.SMASK);
    if (obj != null && obj.isIndirect()) {
        imask = getInstance((PRIndirectReference) obj);
    } else {
        obj = dic.get(PdfName.MASK);
        if (obj != null && obj.isIndirect()) {
            PdfObject obj2 = PdfReader.getPdfObjectRelease(obj);
            if (obj2 instanceof PdfDictionary)
                imask = getInstance((PRIndirectReference) obj);
        }
    }
    Image img = new ImgRaw(width, height, 1, 1, null);
    img.imageMask = imask;
    img.directReference = ref;
    return img;
}
Also used : PRIndirectReference(com.lowagie.text.pdf.PRIndirectReference) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) PdfObject(com.lowagie.text.pdf.PdfObject) PdfNumber(com.lowagie.text.pdf.PdfNumber) TiffImage(com.lowagie.text.pdf.codec.TiffImage) JBIG2Image(com.lowagie.text.pdf.codec.JBIG2Image) GifImage(com.lowagie.text.pdf.codec.GifImage) BufferedImage(java.awt.image.BufferedImage) PngImage(com.lowagie.text.pdf.codec.PngImage) BmpImage(com.lowagie.text.pdf.codec.BmpImage)

Example 15 with PdfNumber

use of com.lowagie.text.pdf.PdfNumber in project itext2 by albfernandez.

the class PngImage method getColorspace.

PdfObject getColorspace() {
    if (icc_profile != null) {
        if ((colorType & 2) == 0)
            return PdfName.DEVICEGRAY;
        else
            return PdfName.DEVICERGB;
    }
    if (gamma == 1f && !hasCHRM) {
        if ((colorType & 2) == 0)
            return PdfName.DEVICEGRAY;
        else
            return PdfName.DEVICERGB;
    } else {
        PdfArray array = new PdfArray();
        PdfDictionary dic = new PdfDictionary();
        if ((colorType & 2) == 0) {
            if (gamma == 1f)
                return PdfName.DEVICEGRAY;
            array.add(PdfName.CALGRAY);
            dic.put(PdfName.GAMMA, new PdfNumber(gamma));
            dic.put(PdfName.WHITEPOINT, new PdfLiteral("[1 1 1]"));
            array.add(dic);
        } else {
            PdfObject wp = new PdfLiteral("[1 1 1]");
            array.add(PdfName.CALRGB);
            if (gamma != 1f) {
                PdfArray gm = new PdfArray();
                PdfNumber n = new PdfNumber(gamma);
                gm.add(n);
                gm.add(n);
                gm.add(n);
                dic.put(PdfName.GAMMA, gm);
            }
            if (hasCHRM) {
                float z = yW * ((xG - xB) * yR - (xR - xB) * yG + (xR - xG) * yB);
                float YA = yR * ((xG - xB) * yW - (xW - xB) * yG + (xW - xG) * yB) / z;
                float XA = YA * xR / yR;
                float ZA = YA * ((1 - xR) / yR - 1);
                float YB = -yG * ((xR - xB) * yW - (xW - xB) * yR + (xW - xR) * yB) / z;
                float XB = YB * xG / yG;
                float ZB = YB * ((1 - xG) / yG - 1);
                float YC = yB * ((xR - xG) * yW - (xW - xG) * yW + (xW - xR) * yG) / z;
                float XC = YC * xB / yB;
                float ZC = YC * ((1 - xB) / yB - 1);
                float XW = XA + XB + XC;
                // YA+YB+YC;
                float YW = 1;
                float ZW = ZA + ZB + ZC;
                PdfArray wpa = new PdfArray();
                wpa.add(new PdfNumber(XW));
                wpa.add(new PdfNumber(YW));
                wpa.add(new PdfNumber(ZW));
                wp = wpa;
                PdfArray matrix = new PdfArray();
                matrix.add(new PdfNumber(XA));
                matrix.add(new PdfNumber(YA));
                matrix.add(new PdfNumber(ZA));
                matrix.add(new PdfNumber(XB));
                matrix.add(new PdfNumber(YB));
                matrix.add(new PdfNumber(ZB));
                matrix.add(new PdfNumber(XC));
                matrix.add(new PdfNumber(YC));
                matrix.add(new PdfNumber(ZC));
                dic.put(PdfName.MATRIX, matrix);
            }
            dic.put(PdfName.WHITEPOINT, wp);
            array.add(dic);
        }
        return array;
    }
}
Also used : PdfLiteral(com.lowagie.text.pdf.PdfLiteral) PdfArray(com.lowagie.text.pdf.PdfArray) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) PdfObject(com.lowagie.text.pdf.PdfObject) PdfNumber(com.lowagie.text.pdf.PdfNumber)

Aggregations

PdfNumber (com.lowagie.text.pdf.PdfNumber)15 PdfDictionary (com.lowagie.text.pdf.PdfDictionary)13 PdfArray (com.lowagie.text.pdf.PdfArray)11 IOException (java.io.IOException)6 PdfObject (com.lowagie.text.pdf.PdfObject)5 PdfString (com.lowagie.text.pdf.PdfString)5 Image (com.lowagie.text.Image)4 ExceptionConverter (com.lowagie.text.ExceptionConverter)3 ImgRaw (com.lowagie.text.ImgRaw)3 PdfIndirectReference (com.lowagie.text.pdf.PdfIndirectReference)3 PdfLiteral (com.lowagie.text.pdf.PdfLiteral)3 DocumentException (com.lowagie.text.DocumentException)2 Rectangle (com.lowagie.text.Rectangle)2 ExtendedColor (com.lowagie.text.pdf.ExtendedColor)2 PRIndirectReference (com.lowagie.text.pdf.PRIndirectReference)2 PatternColor (com.lowagie.text.pdf.PatternColor)2 PdfImage (com.lowagie.text.pdf.PdfImage)2 PdfName (com.lowagie.text.pdf.PdfName)2 PdfXConformanceException (com.lowagie.text.pdf.PdfXConformanceException)2 ShadingColor (com.lowagie.text.pdf.ShadingColor)2