Example usage for com.lowagie.text.pdf PdfContentByte setDefaultColorspace

List of usage examples for com.lowagie.text.pdf PdfContentByte setDefaultColorspace

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfContentByte setDefaultColorspace.

Prototype

public void setDefaultColorspace(PdfName name, PdfObject obj) 

Source Link

Document

Sets the default colorspace.

Usage

From source file:org.alchemy.core.AlcSession.java

License:Open Source License

/** Save the canvas to a single paged PDF file
 * //from  ww  w.  j  av  a2  s.  co m
 * @param file  The file object to save the pdf to
 * @return      True if save worked, otherwise false
 */
boolean saveSinglePdf(File file) {
    // Get the current 'real' size of the canvas without margins/borders
    java.awt.Rectangle bounds = Alchemy.canvas.getVisibleRect();
    //int singlePdfWidth = Alchemy.window.getWindowSize().width;
    //int singlePdfHeight = Alchemy.window.getWindowSize().height;
    com.lowagie.text.Document document = new com.lowagie.text.Document(
            new com.lowagie.text.Rectangle(bounds.width, bounds.height), 0, 0, 0, 0);
    System.out.println("Save Single Pdf Called: " + file.toString());
    boolean noError = true;

    try {

        PdfWriter singleWriter = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.addTitle("Alchemy Session");
        document.addAuthor(USER_NAME);
        document.addCreator("Alchemy <http://al.chemy.org>");

        // Add metadata and open the document
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        XmpWriter xmp = new XmpWriter(os);
        PdfSchema pdf = new PdfSchema();
        pdf.setProperty(PdfSchema.KEYWORDS, "Alchemy <http://al.chemy.org>");
        //pdf.setProperty(PdfSchema.VERSION, "1.4");
        xmp.addRdfDescription(pdf);
        xmp.close();
        singleWriter.setXmpMetadata(os.toByteArray());

        // To avoid transparent colurs being converted from RGB>CMYK>RGB
        // We have to add everything to a transparency group
        PdfTransparencyGroup transGroup = new PdfTransparencyGroup();
        transGroup.put(PdfName.CS, PdfName.DEVICERGB);

        document.open();

        PdfContentByte cb = singleWriter.getDirectContent();
        PdfTemplate tp = cb.createTemplate(bounds.width, bounds.height);

        document.newPage();

        cb.getPdfWriter().setGroup(transGroup);
        // Make sure the color space is Device RGB
        cb.setDefaultColorspace(PdfName.CS, PdfName.DEVICERGB);

        // Draw into the template and add it to the PDF 
        Graphics2D g2pdf = tp.createGraphics(bounds.width, bounds.height);
        Alchemy.canvas.setGuide(false);
        Alchemy.canvas.vectorCanvas.paintComponent(g2pdf);
        Alchemy.canvas.setGuide(true);
        g2pdf.dispose();
        cb.addTemplate(tp, 0, 0);

    } catch (DocumentException ex) {
        System.err.println(ex);
        noError = false;
    } catch (IOException ex) {
        System.err.println(ex);
        noError = false;
    }

    document.close();

    return noError;
}

From source file:org.alchemy.core.AlcSession.java

License:Open Source License

/** Adds a pdfReadPage to an existing pdf file
 * //from w  ww .j av  a2 s .c om
 * @param mainPdf   The main pdf with multiple pages.
 *                  Also used as the destination file.
 * @param tempPdf   The 'new' pdf with one pdfReadPage to be added to the main pdf
 * @return
 */
boolean addPageToPdf(File mainPdf, File tempPdf) {
    try {
        // Destination file created in the temp dir then we will move it
        File dest = new File(DIR_TEMP, "Alchemy.pdf");
        OutputStream output = new FileOutputStream(dest);

        PdfReader reader = new PdfReader(mainPdf.getPath());
        PdfReader newPdf = new PdfReader(tempPdf.getPath());

        // See if the size of the canvas has increased
        // Size of the most recent temp PDF
        com.lowagie.text.Rectangle currentSize = newPdf.getPageSizeWithRotation(1);
        // Size of the session pdf at present
        com.lowagie.text.Rectangle oldSize = reader.getPageSizeWithRotation(1);
        // Sizes to be used from now on
        float pdfWidth = oldSize.getWidth();
        float pdfHeight = oldSize.getHeight();
        if (currentSize.getWidth() > pdfWidth) {
            pdfWidth = currentSize.getWidth();
        }
        if (currentSize.getHeight() > pdfHeight) {
            pdfHeight = currentSize.getHeight();
        }

        // Use the new bigger canvas size if required
        com.lowagie.text.Document document = new com.lowagie.text.Document(
                new com.lowagie.text.Rectangle(pdfWidth, pdfHeight), 0, 0, 0, 0);
        PdfCopy copy = new PdfCopy(document, output);

        // Copy the meta data
        document.addTitle("Alchemy Session");
        document.addAuthor(USER_NAME);
        document.addCreator("Alchemy <http://al.chemy.org>");
        copy.setXmpMetadata(reader.getMetadata());
        document.open();

        // Holds the PDF
        PdfContentByte cb = copy.getDirectContent();

        // Add each page from the main PDF
        for (int i = 0; i < reader.getNumberOfPages();) {
            ++i;
            document.newPage();
            cb.setDefaultColorspace(PdfName.CS, PdfName.DEVICERGB);
            PdfImportedPage page = copy.getImportedPage(reader, i);
            copy.addPage(page);
        }
        // Add the last (new) page
        document.newPage();
        PdfImportedPage lastPage = copy.getImportedPage(newPdf, 1);
        copy.addPage(lastPage);
        output.flush();
        document.close();
        output.close();

        if (dest.exists()) {
            // Save the location of the main pdf
            String mainPdfPath = mainPdf.getPath();
            // Delete the old file
            if (mainPdf.exists()) {
                mainPdf.delete();
            }
            // The final joined up pdf file
            File joinPdf = new File(mainPdfPath);
            // Rename the file
            boolean success = dest.renameTo(joinPdf);
            if (!success) {
                System.err.println("Error moving Pdf");
                return false;
            }

        } else {
            System.err.println("File does not exist?!: " + dest.getAbsolutePath());
            return false;
        }
        return true;

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}