Search in sources :

Example 1 with Rectangle

use of com.itextpdf.text.Rectangle in project gephi by gephi.

the class PDFExporter method execute.

@Override
public boolean execute() {
    Progress.start(progress);
    PreviewController controller = Lookup.getDefault().lookup(PreviewController.class);
    controller.getModel(workspace).getProperties().putValue(PreviewProperty.VISIBILITY_RATIO, 1.0);
    controller.refreshPreview(workspace);
    PreviewProperties props = controller.getModel(workspace).getProperties();
    Rectangle size = new Rectangle(pageSize);
    if (landscape) {
        size = new Rectangle(pageSize.rotate());
    }
    Color col = props.getColorValue(PreviewProperty.BACKGROUND_COLOR);
    size.setBackgroundColor(new BaseColor(col.getRed(), col.getGreen(), col.getBlue()));
    Document document = new Document(size);
    PdfWriter pdfWriter = null;
    try {
        pdfWriter = PdfWriter.getInstance(document, stream);
        pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_5);
        pdfWriter.setFullCompression();
    } catch (DocumentException ex) {
        Exceptions.printStackTrace(ex);
    }
    document.open();
    PdfContentByte cb = pdfWriter.getDirectContent();
    cb.saveState();
    props.putValue(PDFTarget.LANDSCAPE, landscape);
    props.putValue(PDFTarget.PAGESIZE, size);
    props.putValue(PDFTarget.MARGIN_TOP, new Float((float) marginTop));
    props.putValue(PDFTarget.MARGIN_LEFT, new Float((float) marginLeft));
    props.putValue(PDFTarget.MARGIN_BOTTOM, new Float((float) marginBottom));
    props.putValue(PDFTarget.MARGIN_RIGHT, new Float((float) marginRight));
    props.putValue(PDFTarget.PDF_CONTENT_BYTE, cb);
    target = (PDFTarget) controller.getRenderTarget(RenderTarget.PDF_TARGET, workspace);
    if (target instanceof LongTask) {
        ((LongTask) target).setProgressTicket(progress);
    }
    try {
        controller.render(target, workspace);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    cb.restoreState();
    document.close();
    Progress.finish(progress);
    props.putValue(PDFTarget.PDF_CONTENT_BYTE, null);
    props.putValue(PDFTarget.PAGESIZE, null);
    return !cancel;
}
Also used : PreviewProperties(org.gephi.preview.api.PreviewProperties) PdfWriter(com.itextpdf.text.pdf.PdfWriter) Color(java.awt.Color) BaseColor(com.itextpdf.text.BaseColor) Rectangle(com.itextpdf.text.Rectangle) Document(com.itextpdf.text.Document) PreviewController(org.gephi.preview.api.PreviewController) DocumentException(com.itextpdf.text.DocumentException) BaseColor(com.itextpdf.text.BaseColor) LongTask(org.gephi.utils.longtask.spi.LongTask) DocumentException(com.itextpdf.text.DocumentException) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte)

Example 2 with Rectangle

use of com.itextpdf.text.Rectangle in project gephi by gephi.

the class UIExporterPDFPanel method unsetup.

public void unsetup(PDFExporter pdfExporter) {
    if (pageSizeCombo.getSelectedItem() == customSizeString) {
        double width = pdfExporter.getPageSize().getWidth();
        double height = pdfExporter.getPageSize().getHeight();
        try {
            width = sizeFormatter.parse(widthTextField.getText()).doubleValue();
        } catch (ParseException ex) {
        }
        try {
            height = sizeFormatter.parse(heightTextField.getText()).doubleValue();
        } catch (ParseException ex) {
        }
        if (millimeter) {
            width *= MM;
            height *= MM;
        } else {
            width *= INCH;
            height *= INCH;
        }
        float w = (float) width;
        float h = (float) height;
        Rectangle rect = new Rectangle(w, h);
        pdfExporter.setPageSize(rect);
    } else {
        pdfExporter.setPageSize(((PageSizeItem) pageSizeCombo.getSelectedItem()).getPageSize());
    }
    pdfExporter.setLandscape(landscapeRadio.isSelected());
    double top = pdfExporter.getMarginTop();
    double bottom = pdfExporter.getMarginBottom();
    double left = pdfExporter.getMarginLeft();
    double right = pdfExporter.getMarginRight();
    try {
        top = marginFormatter.parse(topMarginTextField.getText()).doubleValue();
    } catch (ParseException ex) {
    }
    try {
        bottom = marginFormatter.parse(bottomMarginTextField.getText()).doubleValue();
    } catch (ParseException ex) {
    }
    try {
        left = marginFormatter.parse(leftMarginTextField.getText()).doubleValue();
    } catch (ParseException ex) {
    }
    try {
        right = marginFormatter.parse(rightMargintextField.getText()).doubleValue();
    } catch (ParseException ex) {
    }
    if (millimeter) {
        top *= MM;
        bottom *= MM;
        left *= MM;
        right *= MM;
    } else {
        top *= INCH;
        bottom *= INCH;
        left *= INCH;
        right *= INCH;
    }
    pdfExporter.setMarginTop((float) top);
    pdfExporter.setMarginBottom((float) bottom);
    pdfExporter.setMarginLeft((float) left);
    pdfExporter.setMarginRight((float) right);
    savePreferences();
}
Also used : Rectangle(com.itextpdf.text.Rectangle) ParseException(java.text.ParseException)

Aggregations

Rectangle (com.itextpdf.text.Rectangle)2 BaseColor (com.itextpdf.text.BaseColor)1 Document (com.itextpdf.text.Document)1 DocumentException (com.itextpdf.text.DocumentException)1 PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)1 PdfWriter (com.itextpdf.text.pdf.PdfWriter)1 Color (java.awt.Color)1 ParseException (java.text.ParseException)1 PreviewController (org.gephi.preview.api.PreviewController)1 PreviewProperties (org.gephi.preview.api.PreviewProperties)1 LongTask (org.gephi.utils.longtask.spi.LongTask)1