Example usage for org.apache.pdfbox.pdmodel.graphics.image LosslessFactory createFromImage

List of usage examples for org.apache.pdfbox.pdmodel.graphics.image LosslessFactory createFromImage

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.graphics.image LosslessFactory createFromImage.

Prototype

public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException 

Source Link

Document

Creates a new lossless encoded image XObject from a BufferedImage.

Usage

From source file:de.redsix.pdfcompare.CompareResult.java

License:Apache License

protected void addPageToDocument(final PDDocument document, final ImageWithDimension actualImage,
        final ImageWithDimension expectedImage) throws IOException {
    PDPage page = new PDPage(new PDRectangle(actualImage.width, actualImage.height));
    document.addPage(page);//from www.  j a  va 2  s  . com
    //final PDImageXObject diffXImage = LosslessFactory.createFromImage(document, diffImage.bufferedImage);
    final PDImageXObject actualXObject = LosslessFactory.createFromImage(document, actualImage.bufferedImage);
    final PDImageXObject expectedXObject = LosslessFactory.createFromImage(document,
            expectedImage.bufferedImage);
    try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
        int x = (int) page.getCropBox().getWidth() / 2;
        //System.out.println("X value "+x);
        int y = (int) page.getCropBox().getHeight();
        //System.out.println("Y value "+y);
        contentStream.drawImage(actualXObject, 0, 0, x, y);
        contentStream.setLineWidth(0.5F);
        contentStream.moveTo(x, 2);
        contentStream.lineTo(x, y);
        contentStream.drawImage(expectedXObject, x + 2, 0, x, y);
        /*contentStream.moveTo(x+x, 2);
        contentStream.lineTo(x+x, y+y);
        contentStream.drawImage(expectedXObject, x+x+2, 0, x, y);*/
        contentStream.stroke();
    }
}

From source file:de.redsix.pdfcompare.CompareResult.java

License:Apache License

protected void addPageToDocument(final PDDocument document, final ImageWithDimension diffImage,
        final ImageWithDimension actualImage, final ImageWithDimension expectedImage) throws IOException {
    PDPage page = new PDPage(new PDRectangle(diffImage.width, diffImage.height));
    document.addPage(page);// w w w .jav a2  s .c  o m
    final PDImageXObject diffXImage = LosslessFactory.createFromImage(document, diffImage.bufferedImage);
    final PDImageXObject actualXObject = LosslessFactory.createFromImage(document, actualImage.bufferedImage);
    final PDImageXObject expectedXObject = LosslessFactory.createFromImage(document,
            expectedImage.bufferedImage);
    try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
        int x = (int) page.getCropBox().getWidth() / 3;
        System.out.println("X value " + x);
        int y = (int) page.getCropBox().getHeight();
        System.out.println("Y value " + y);
        contentStream.drawImage(diffXImage, 0, 0, x, y);
        contentStream.setLineWidth(0.5F);
        contentStream.moveTo(x, 2);
        contentStream.lineTo(x, y);
        contentStream.drawImage(actualXObject, x + 2, 0, x, y);
        contentStream.moveTo(x + x, 2);
        contentStream.lineTo(x + x, y + y);
        contentStream.drawImage(expectedXObject, x + x + 2, 0, x, y);
        contentStream.stroke();
    }
}

From source file:de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DLosslessImageEncoder.java

License:Apache License

@Override
public PDImageXObject encodeImage(PDDocument document, PDPageContentStream contentStream, Image image) {
    final BufferedImage bi;

    if (image instanceof BufferedImage) {
        bi = (BufferedImage) image;
    } else {//from w  w  w  . jav a  2  s .co m
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics graphics = bi.getGraphics();
        if (!graphics.drawImage(image, 0, 0, null, null))
            throw new IllegalStateException("Not fully loaded images are not supported.");
        graphics.dispose();
    }

    try {
        if (doc == null || doc.get() != document) {
            imageMap = new HashMap<ImageSoftReference, SoftReference<PDImageXObject>>();
            profileMap = new HashMap<ProfileSoftReference, SoftReference<PDColorSpace>>();
            doc = new SoftReference<PDDocument>(document);
        }
        SoftReference<PDImageXObject> pdImageXObjectSoftReference = imageMap.get(new ImageSoftReference(image));
        PDImageXObject imageXObject = pdImageXObjectSoftReference == null ? null
                : pdImageXObjectSoftReference.get();
        if (imageXObject == null) {
            imageXObject = LosslessFactory.createFromImage(document, bi);

            /*
             * Do we have a color profile we need to embed?
             */
            if (bi.getColorModel().getColorSpace() instanceof ICC_ColorSpace) {
                ICC_Profile profile = ((ICC_ColorSpace) bi.getColorModel().getColorSpace()).getProfile();
                /*
                 * Only tag a profile if it is not the default sRGB profile.
                 */
                if (((ICC_ColorSpace) bi.getColorModel().getColorSpace()).getProfile() != ICC_Profile
                        .getInstance(ColorSpace.CS_sRGB)) {

                    SoftReference<PDColorSpace> pdProfileRef = profileMap
                            .get(new ProfileSoftReference(profile));

                    /*
                     * We try to reduce the copies of the same ICC profile in the PDF file. If the
                     * image already has a profile, it will be the right one. Otherwise we must
                     * assume that the image is now in sRGB.
                     */
                    PDColorSpace pdProfile = pdProfileRef == null ? null : pdProfileRef.get();
                    if (pdProfile == null) {
                        pdProfile = imageXObject.getColorSpace();
                        if (pdProfile instanceof PDICCBased) {
                            profileMap.put(new ProfileSoftReference(profile),
                                    new SoftReference<PDColorSpace>(pdProfile));
                        }
                    }
                    imageXObject.setColorSpace(pdProfile);
                }
            }
            imageMap.put(new ImageSoftReference(image), new SoftReference<PDImageXObject>(imageXObject));
        }

        return imageXObject;
    } catch (IOException e) {
        throw new RuntimeException("Could not encode Image", e);
    }
}

From source file:es.rickyepoderi.pdfimages.Converter.java

License:Open Source License

/**
 * Method that converts the images provides to pdf and writes to the target 
 * path specified./*from   w  ww  .  j  a va2 s  .c  om*/
 *
 * @param output The file to write
 * @param bimages The buffered images to put in the pdf
 * @throws IOException Some error generating the PDF
 */
public void images2Pdf(File output, File... files) throws IOException {
    try (PDDocument pdDocument = new PDDocument()) {
        for (File file : files) {
            PDPage page = new PDPage();
            pdDocument.addPage(page);
            try (PDPageContentStream pageStream = new PDPageContentStream(pdDocument, page,
                    PDPageContentStream.AppendMode.APPEND, true, true)) {
                BufferedImage bimage = ImageIO.read(file);
                PDImageXObject img = LosslessFactory.createFromImage(pdDocument, bimage);
                Dimension scaledDim = getScaledDimension(new Dimension(img.getWidth(), img.getHeight()),
                        new Dimension((int) page.getMediaBox().getWidth(),
                                (int) page.getMediaBox().getHeight()));
                pageStream.drawImage(img, 0, 0, scaledDim.width, scaledDim.height);
            }
        }
        pdDocument.save(output);
    }
}

From source file:GUI.Helper.PDFIOHelper.java

public static void writeSummaryReport(MainController mc) {

    mc.selectStep(6);/*from   w  w  w.java2  s.  co m*/
    FileChooser fc = new FileChooser();
    fc.setTitle("Save WZITS Tool Project");
    fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF File (.pdf)", "*.pdf"));
    if (mc.getProject().getSaveFile() != null) {
        File initDir = mc.getProject().getSaveFile().getParentFile();
        if (initDir.isDirectory()) {
            fc.setInitialDirectory(initDir);
        }
    }
    File saveFile = fc.showSaveDialog(MainController.getWindow()); //mc.getMainWindow()
    if (saveFile != null) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                boolean exportSuccess = true;
                try {
                    PDDocument doc = new PDDocument();
                    for (int factSheetIdx = 1; factSheetIdx <= 8; factSheetIdx++) {

                        Node n = mc.goToFactSheet(factSheetIdx, true);
                        MainController.getStage().show();
                        BorderPane bp = (BorderPane) ((ScrollPane) n).getContent();

                        PDPage page = new PDPage();
                        doc.addPage(page);

                        PDPageContentStream content = new PDPageContentStream(doc, page);

                        WritableImage wi = bp.snapshot(new SnapshotParameters(), null);

                        double prefHeight = wi.getHeight();
                        double prefWidth = wi.getWidth();
                        BufferedImage bi = SwingFXUtils.fromFXImage(wi, null);

                        PDImageXObject ximage = LosslessFactory.createFromImage(doc, bi);
                        int drawWidth = (int) Math.min(MAX_DRAW_WIDTH, Math.round(WIDTH_FACTOR * prefWidth));
                        int drawHeight = (int) Math.round(HEIGHT_FACTOR * prefHeight);
                        int numPages = (int) Math.ceil(drawHeight / MAX_DRAW_HEIGHT);
                        drawHeight = (int) Math.min(MAX_DRAW_HEIGHT, drawHeight);
                        content.drawImage(ximage, MARGIN_LEFT_X, MARGIN_TOP_Y - drawHeight, drawWidth,
                                drawHeight);
                        content.fillAndStroke();
                        content.close();
                    }
                    drawReportHeaderFooter(doc, mc.getProject(), true);
                    doc.save(saveFile);
                    doc.close();
                } catch (IOException ie) {
                    System.out.println("ERROR");
                    exportSuccess = false;
                }
                if (exportSuccess) {
                    Alert al = new Alert(Alert.AlertType.CONFIRMATION);
                    al.setTitle("WZITS Tool");
                    al.setHeaderText("Fact sheet export successful");
                    al.showAndWait();
                } else {
                    Alert al = new Alert(Alert.AlertType.WARNING);
                    al.setTitle("WZITS Tool");
                    al.setHeaderText("Fact sheet export failed");
                    al.showAndWait();
                }
                mc.selectStep(6);
            }
        });
    }
}

From source file:GUI.Helper.PDFIOHelper.java

public static void writeStepSummary(MainController mc, int factSheetIdx) {
    Node n = mc.goToFactSheet(factSheetIdx, false);
    FileChooser fc = new FileChooser();
    fc.setTitle("Save WZITS Tool Project");
    fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF File (.pdf)", "*.pdf"));
    if (mc.getProject().getSaveFile() != null) {
        File initDir = mc.getProject().getSaveFile().getParentFile();
        if (initDir.isDirectory()) {
            fc.setInitialDirectory(initDir);
        }/*from   ww  w. ja  v  a2s . co  m*/
    }
    File saveFile = fc.showSaveDialog(MainController.getWindow()); //mc.getMainWindow()
    if (saveFile != null) {

        BorderPane bp = (BorderPane) ((ScrollPane) n).getContent();
        //Scene scene = new Scene(bp);
        //n.applyCss();
        //bp.applyCss();
        //WritableImage wi = scene.snapshot(null);

        WritableImage wi = bp.snapshot(new SnapshotParameters(), null);

        double prefHeight = wi.getHeight();
        double prefWidth = wi.getWidth();

        BufferedImage bi = SwingFXUtils.fromFXImage(wi, null);
        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);
        boolean exportSuccess = false;
        try {
            //ImageIO.write(bi, "png", new File("test.png"));
            PDPageContentStream content = new PDPageContentStream(doc, page);
            PDImageXObject ximage = LosslessFactory.createFromImage(doc, bi);
            int drawWidth = (int) Math.min(MAX_DRAW_WIDTH, Math.round(WIDTH_FACTOR * prefWidth));
            int drawHeight = (int) Math.round(HEIGHT_FACTOR * prefHeight);
            int numPages = (int) Math.ceil(drawHeight / MAX_DRAW_HEIGHT);
            drawHeight = (int) Math.min(MAX_DRAW_HEIGHT, drawHeight);
            content.drawImage(ximage, MARGIN_LEFT_X, MARGIN_TOP_Y - drawHeight, drawWidth, drawHeight);
            content.fillAndStroke();
            content.close();
            drawReportHeaderFooter(doc, mc.getProject(), true);
            doc.save(saveFile);
            doc.close();
            exportSuccess = true;
        } catch (IOException ie) {
            System.out.println("ERROR");
        }
        if (exportSuccess) {
            Alert al = new Alert(Alert.AlertType.CONFIRMATION);
            al.setTitle("WZITS Tool");
            al.setHeaderText("Fact sheet export successful");
            al.showAndWait();
        } else {
            Alert al = new Alert(Alert.AlertType.WARNING);
            al.setTitle("WZITS Tool");
            al.setHeaderText("Fact sheet export failed");
            al.showAndWait();
        }
    }
}

From source file:GUI.Helper.PDFIOHelper.java

private static void drawReportHeaderFooter(PDDocument report, Project proj, boolean headerOnFirstPage) {

    int pageIdx = headerOnFirstPage ? 0 : 1;
    int marginOffset = 10;
    try {/*from   w w  w. j  av  a 2  s  .co m*/
        PDPageContentStream cs;
        for (int p = pageIdx; p < report.getNumberOfPages(); p++) {
            cs = new PDPageContentStream(report, report.getPage(p), true, false);
            cs.setFont(PDType1Font.TIMES_ROMAN, 11);
            cs.setNonStrokingColor(Color.BLACK);
            cs.beginText();
            String dateString = DateFormat.getDateInstance(DateFormat.MEDIUM)
                    .format(Calendar.getInstance().getTime());
            cs.setTextMatrix(new Matrix(1, 0, 0, 1, MARGIN_LEFT_X, MARGIN_TOP_Y + marginOffset));
            cs.showText(dateString);
            String projectString = "WZ ITS Tool Report: " + proj.getName();
            cs.setTextMatrix(new Matrix(1, 0, 0, 1,
                    MARGIN_RIGHT_X - (PDType1Font.TIMES_ROMAN.getStringWidth(projectString) / 1000 * 11),
                    MARGIN_TOP_Y + marginOffset));
            cs.showText(projectString);
            String pageNumString = "Page " + String.valueOf(p + 1) + " of "
                    + String.valueOf(report.getNumberOfPages());
            cs.setTextMatrix(new Matrix(1, 0, 0, 1,
                    MARGIN_LEFT_X + (MARGIN_RIGHT_X - MARGIN_LEFT_X) / 2.0f
                            - (PDType1Font.TIMES_ROMAN.getStringWidth(pageNumString) / 1000 * 11) / 2.0f,
                    MARGIN_BOTTOM_Y - marginOffset));
            cs.showText(pageNumString);
            cs.setTextMatrix(new Matrix(1, 0, 0, 1, MARGIN_LEFT_X + 20, MARGIN_BOTTOM_Y - marginOffset));
            cs.showText("WZ ITS Tool V" + WZITS_FX.VERSION);
            String analystAgencyStr = (proj.getAnalyst() != null ? proj.getAnalyst() : "")
                    + (proj.getAnalyst() != null && proj.getAgency() != null ? " / " : "")
                    + (proj.getAgency() != null ? proj.getAgency() : "");
            cs.setTextMatrix(new Matrix(1, 0, 0, 1,
                    MARGIN_RIGHT_X - (PDType1Font.TIMES_ROMAN.getStringWidth(analystAgencyStr) / 1000 * 11),
                    MARGIN_BOTTOM_Y - marginOffset));
            cs.showText(analystAgencyStr);
            cs.endText();

            BufferedImage logoWZITS = ImageIO.read(WZITS_FX.class.getResource("/GUI/Icon/wzits_icon_64.png"));
            //ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
            //op.filter(logoWZITS, logoWZITS);
            cs.drawImage(LosslessFactory.createFromImage(report, logoWZITS), MARGIN_LEFT_X,
                    MARGIN_BOTTOM_Y - marginOffset - 3, 16, 16);
            cs.close();
        }
    } catch (IOException e) {
        System.out.println("Something went wrong");
    }

}

From source file:model.objects.Project.java

License:Apache License

/**
 * /*from w ww . ja v a 2s  .co m*/
 * @param _doc
 * @param _bi
 * @param _pageindex       index of page to which the BufferedImage is 
 *                      inserted.
 *                      If it is equal to -1, new page is created.
 *                   
 */
public void attatchToPDF(final PDDocument _doc, final BufferedImage _bi, final int _pageindex) {
    PDPage page = null;
    try {
        if (_pageindex == -1) {
            page = new PDPage(new PDRectangle(State.getImageSize().width, State.getImageSize().height));

            _doc.addPage(page);
        } else {
            page = _doc.getPage(_pageindex);
            //             page.setCropBox(new PDRectangle(State.getImageSize().width , 
            //                   State.getImageSize().height ));

        }

        int width = (int) page.getCropBox().getWidth();
        int height = (int) page.getCropBox().getHeight();

        PDImageXObject ximage = LosslessFactory.createFromImage(_doc,
                //                 _bi);
                Utils.resizeImage(width, height, _bi));

        PDPageContentStream content = new PDPageContentStream(_doc, page, true, true);

        // contentStream.drawImage(ximage, 20, 20 );
        // better method inspired by http://stackoverflow.com/a/22318681/535646
        // reduce this value if the image is too large
        float scale = 1f;
        content.drawImage(ximage, 20, 20, ximage.getWidth() * scale, ximage.getHeight() * scale);

        content.close();
        //  LosslessFactory.createFromImage(doc, bim)
        //              content.drawImage(ximage, 0, 0);
        //              content.close();
    } catch (IOException ie) {
        //handle exception
    }
}

From source file:org.apache.openmeetings.web.room.wb.WbPanel.java

License:Apache License

@Override
protected void processWbAction(WbAction a, JSONObject obj, AjaxRequestTarget target) throws IOException {
    Client c = rp.getClient();//from   www .ja va  2 s . c  om
    if (c == null) {
        return;
    }
    switch (a) {
    case createObj:
    case modifyObj: {
        JSONObject o = obj.optJSONObject("obj");
        if (o != null && "pointer".equals(o.getString(ATTR_TYPE))) {
            sendWbOthers(a, obj);
            return;
        }
    }
        break;
    case downloadPdf: {
        boolean moder = c.hasRight(Room.Right.moderator);
        Room r = rp.getRoom();
        if ((moder && !r.isHidden(RoomElement.ActionMenu)) || (!moder && r.isAllowUserQuestions())) {
            try (PDDocument doc = new PDDocument()) {
                JSONArray arr = obj.getJSONArray("slides");
                for (int i = 0; i < arr.length(); ++i) {
                    String base64Image = arr.getString(i).split(",")[1];
                    byte[] bb = Base64.decodeBase64(base64Image);
                    BufferedImage img = ImageIO.read(new ByteArrayInputStream(bb));
                    float width = img.getWidth();
                    float height = img.getHeight();
                    PDPage page = new PDPage(new PDRectangle(width, height));
                    PDImageXObject pdImageXObject = LosslessFactory.createFromImage(doc, img);
                    try (PDPageContentStream contentStream = new PDPageContentStream(doc, page,
                            AppendMode.APPEND, false)) {
                        contentStream.drawImage(pdImageXObject, 0, 0, width, height);
                    }
                    doc.addPage(page);
                }
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                doc.save(baos);
                rp.startDownload(target, baos.toByteArray());
            }
        }
        return;
    }
    case loadVideos: {
        StringBuilder sb = new StringBuilder("WbArea.initVideos(");
        JSONArray arr = new JSONArray();
        for (Entry<Long, Whiteboard> entry : wbm.list(roomId)) {
            Whiteboard wb = entry.getValue();
            for (JSONObject o : wb.list()) {
                String ft = o.optString(ATTR_FILE_TYPE);
                if (BaseFileItem.Type.Recording.name().equals(ft)
                        || BaseFileItem.Type.Video.name().equals(ft)) {
                    JSONObject _sts = o.optJSONObject(PARAM_STATUS);
                    if (_sts == null) {
                        continue;
                    }
                    JSONObject sts = new JSONObject(_sts.toString()); //copy
                    sts.put("pos", sts.getDouble("pos")
                            + (System.currentTimeMillis() - sts.getLong(PARAM_UPDATED)) * 1. / 1000);
                    arr.put(new JSONObject().put("wbId", wb.getId()).put("uid", o.getString("uid"))
                            .put(ATTR_SLIDE, o.getString(ATTR_SLIDE)).put(PARAM_STATUS, sts));
                }
            }
        }
        sb.append(arr.toString()).append(");");
        target.appendJavaScript(sb);
        return;
    }
    default:
        break;
    }

    //presenter-right
    if (c.hasRight(Right.presenter)) {
        switch (a) {
        case createWb: {
            Whiteboard wb = wbm.add(roomId, c.getUser().getLanguageId());
            sendWbAll(WbAction.createWb, getAddWbJson(wb));
        }
            break;
        case removeWb: {
            long id = obj.optLong("wbId", -1);
            if (id > -1) {
                wbm.remove(roomId, id);
                sendWbAll(WbAction.removeWb, obj);
            }
        }
            break;
        case activateWb: {
            long _id = obj.optLong("wbId", -1);
            if (_id > -1) {
                wbm.activate(roomId, _id);
                sendWbAll(WbAction.activateWb, obj);
            }
        }
            break;
        case renameWb: {
            Whiteboard wb = wbm.get(roomId).get(obj.optLong("wbId", -1));
            if (wb != null) {
                wbm.update(roomId, wb.setName(obj.getString("name")));
                sendWbAll(WbAction.renameWb, obj);
            }
        }
            break;
        case setSlide: {
            Whiteboard wb = wbm.get(roomId).get(obj.optLong("wbId", -1));
            if (wb != null) {
                wb.setSlide(obj.optInt(ATTR_SLIDE, 0));
                wbm.update(roomId, wb);
                sendWbOthers(WbAction.setSlide, obj);
            }
        }
            break;
        case clearAll: {
            clearAll(roomId, obj.getLong("wbId"));
        }
            break;
        case setSize: {
            Whiteboard wb = wbm.get(roomId).get(obj.getLong("wbId"));
            wb.setWidth(obj.getInt(ATTR_WIDTH));
            wb.setHeight(obj.getInt(ATTR_HEIGHT));
            wb.setZoom(obj.getDouble(ATTR_ZOOM));
            wb.setZoomMode(ZoomMode.valueOf(obj.getString("zoomMode")));
            wbm.update(roomId, wb);
            sendWbOthers(WbAction.setSize, getAddWbJson(wb));
        }
            break;
        default:
            break;
        }
    }
    //wb-right
    if (c.hasRight(Right.presenter) || c.hasRight(Right.whiteBoard)) {
        switch (a) {
        case createObj: {
            Whiteboard wb = wbm.get(roomId).get(obj.getLong("wbId"));
            JSONObject o = obj.getJSONObject("obj");
            wb.put(o.getString("uid"), o);
            wbm.update(roomId, wb);
            addUndo(wb.getId(), new UndoObject(UndoObject.Type.add, o));
            sendWbOthers(WbAction.createObj, obj);
        }
            break;
        case modifyObj: {
            Whiteboard wb = wbm.get(roomId).get(obj.getLong("wbId"));
            JSONArray arr = obj.getJSONArray("obj");
            JSONArray undo = new JSONArray();
            for (int i = 0; i < arr.length(); ++i) {
                JSONObject _o = arr.getJSONObject(i);
                String uid = _o.getString("uid");
                JSONObject po = wb.get(uid);
                if (po != null) {
                    undo.put(po);
                    wb.put(uid, _o);
                }
            }
            if (arr.length() != 0) {
                wbm.update(roomId, wb);
                addUndo(wb.getId(), new UndoObject(UndoObject.Type.modify, undo));
            }
            sendWbOthers(WbAction.modifyObj, obj);
        }
            break;
        case deleteObj: {
            Whiteboard wb = wbm.get(roomId).get(obj.getLong("wbId"));
            JSONArray arr = obj.getJSONArray("obj");
            JSONArray undo = new JSONArray();
            for (int i = 0; i < arr.length(); ++i) {
                JSONObject _o = arr.getJSONObject(i);
                JSONObject u = wb.remove(_o.getString("uid"));
                if (u != null) {
                    undo.put(u);
                }
            }
            if (undo.length() != 0) {
                wbm.update(roomId, wb);
                addUndo(wb.getId(), new UndoObject(UndoObject.Type.remove, undo));
            }
            sendWbAll(WbAction.deleteObj, obj);
        }
            break;
        case clearSlide: {
            Whiteboard wb = wbm.get(roomId).get(obj.getLong("wbId"));
            JSONArray arr = wb.clearSlide(obj.getInt(ATTR_SLIDE));
            if (arr.length() != 0) {
                wbm.update(roomId, wb);
                addUndo(wb.getId(), new UndoObject(UndoObject.Type.remove, arr));
            }
            sendWbAll(WbAction.clearSlide, obj);
        }
            break;
        case save:
            wb2save = obj.getLong("wbId");
            fileName.open(target);
            break;
        case undo: {
            Long wbId = obj.getLong("wbId");
            UndoObject uo = getUndo(wbId);
            if (uo != null) {
                Whiteboard wb = wbm.get(roomId).get(wbId);
                switch (uo.getType()) {
                case add: {
                    JSONObject o = new JSONObject(uo.getObject());
                    wb.remove(o.getString("uid"));
                    wbm.update(roomId, wb);
                    sendWbAll(WbAction.deleteObj, obj.put("obj", new JSONArray().put(o)));
                }
                    break;
                case remove: {
                    JSONArray arr = new JSONArray(uo.getObject());
                    for (int i = 0; i < arr.length(); ++i) {
                        JSONObject o = arr.getJSONObject(i);
                        wb.put(o.getString("uid"), o);
                    }
                    wbm.update(roomId, wb);
                    sendWbAll(WbAction.createObj, obj.put("obj", new JSONArray(uo.getObject())));
                }
                    break;
                case modify: {
                    JSONArray arr = new JSONArray(uo.getObject());
                    for (int i = 0; i < arr.length(); ++i) {
                        JSONObject o = arr.getJSONObject(i);
                        wb.put(o.getString("uid"), o);
                    }
                    wbm.update(roomId, wb);
                    sendWbAll(WbAction.modifyObj, obj.put("obj", arr));
                }
                    break;
                }
            }
        }
            break;
        case videoStatus: {
            Whiteboard wb = wbm.get(roomId).get(obj.getLong("wbId"));
            String uid = obj.getString("uid");
            JSONObject po = wb.get(uid);
            if (po != null && "video".equals(po.getString(ATTR_TYPE))) {
                JSONObject ns = obj.getJSONObject(PARAM_STATUS);
                po.put(PARAM_STATUS, ns.put(PARAM_UPDATED, System.currentTimeMillis()));
                wbm.update(roomId, wb.put(uid, po));
                obj.put(ATTR_SLIDE, po.getInt(ATTR_SLIDE));
                sendWbAll(WbAction.videoStatus, obj);
            }
        }
            break;
        default:
            break;
        }
    }
}

From source file:org.fit.cssbox.render.PDFRenderer.java

License:Open Source License

/**
 * Inserts image to recent PDF page using PDFBox
 *///from w  w w .ja  v a 2s .c o m
private int insertImagePDFBox(BufferedImage img, float x, float y, float width, float height) {
    // transform X,Y coordinates to Apache PDFBox format
    y = pageFormat.getHeight() - height - y;

    try {
        //PDXObjectImage ximage = new PDPixelMap(doc, img);
        //content.drawXObject(ximage, x, y, width, height);
        PDImageXObject ximage = LosslessFactory.createFromImage(doc, img);
        content.drawImage(ximage, x, y, width, height);
    } catch (IOException e) {
        e.printStackTrace();
        return -1;
    }
    return 0;
}