Example usage for com.lowagie.text Document addCreator

List of usage examples for com.lowagie.text Document addCreator

Introduction

In this page you can find the example usage for com.lowagie.text Document addCreator.

Prototype


public boolean addCreator(String creator) 

Source Link

Document

Adds the creator to a Document.

Usage

From source file:songscribe.publisher.publisheractions.ExportPDFAction.java

License:Open Source License

public void actionPerformed(ActionEvent e) {
    if (publisher.isBookNull()) {
        return;/*from   w  w  w .  j a  va2s .c  om*/
    }

    if (pfd.showDialog()) {
        File saveFile = pfd.getFile();

        if (!saveFile.getName().toLowerCase().endsWith(".pdf")) {
            saveFile = new File(saveFile.getAbsolutePath() + ".pdf");
        }

        if (saveFile.exists()) {
            int answ = JOptionPane.showConfirmDialog(publisher,
                    "The file " + saveFile.getName() + " already exists. Do you want to overwrite it?",
                    publisher.PROG_NAME, JOptionPane.YES_NO_OPTION);
            if (answ == JOptionPane.NO_OPTION) {
                return;
            }
        }

        float resolution = 72f / MusicSheet.RESOLUTION;
        Book book = publisher.getBook();
        Document document = new Document(new com.lowagie.text.Rectangle(book.getPageSize().x * resolution,
                book.getPageSize().y * resolution, book.getPageSize().width * resolution,
                book.getPageSize().height * resolution), 0, 0, 0, 0);

        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(saveFile));
            document.addCreator(publisher.PROG_NAME);
            document.open();
            PdfContentByte cb = writer.getDirectContent();

            for (ListIterator<Page> it = book.pageIterator(); it.hasNext();) {
                Graphics2D g2 = cb.createGraphicsShapes(book.getPageSize().width * resolution,
                        book.getPageSize().height * resolution);
                g2.scale(resolution, resolution);
                it.next().paint(g2, it.nextIndex() - 1, false, 0, book.getPageSize().height);
                g2.dispose();

                if (it.hasNext()) {
                    document.newPage();
                }
            }

            document.close();
            Utilities.openExportFile(publisher, saveFile);
        } catch (DocumentException e1) {
            publisher.showErrorMessage("An unexprected error occured and could not export into PDF.");
            logger.error("PDF save", e1);
        } catch (FileNotFoundException e1) {
            publisher.showErrorMessage(MainFrame.COULD_NOT_SAVE_MESSAGE);
            logger.error("PDF save", e1);
        }
    }
}

From source file:songscribe.ui.mainframeactions.ExportPDFAction.java

License:Open Source License

public static void createPDF(Data data, File outputFile, Boolean isGUI) {
    float resolution = 72f / MusicSheet.RESOLUTION;
    float paperWidth = data.paperWidth * resolution;
    float paperHeight = data.paperHeight * resolution;
    MainFrame mainFrame = data.mainFrame;
    Document document = new Document(new Rectangle(0, 0, paperWidth, paperHeight), 0, 0, 0, 0);
    document.addCreator(mainFrame.PROG_NAME);
    document.addTitle(mainFrame.getMusicSheet().getComposition().getSongTitle());

    // Scale to fit
    int sheetWidth = mainFrame.getMusicSheet().getSheetWidth();
    int sheetHeight = mainFrame.getMusicSheet().getSheetHeight();
    double horizontalMargin = (data.leftInnerMargin + data.rightOuterMargin) * resolution;
    double horizontalScale = (paperWidth - horizontalMargin) / sheetWidth;
    double verticalMargin = (data.topMargin + data.bottomMargin) * resolution;
    double verticalScale = (paperHeight - verticalMargin) / sheetHeight;
    double scale;
    double leftMargin = data.leftInnerMargin * resolution;

    if (horizontalScale < verticalScale) {
        scale = horizontalScale;//from  ww w. jav a2  s.c  o  m
    } else {
        // If scaling vertically, the horizontal margin will be larger than
        // what is specified in Data. So we calculate the total margin available,
        // then give the left margin the same fraction of the total margin
        // it would have had before scaling.
        scale = verticalScale;
        double scaledMargin = paperWidth - (sheetWidth * scale);
        double leftMarginFactor = (double) data.leftInnerMargin
                / (double) (data.leftInnerMargin + data.rightOuterMargin);
        leftMargin = scaledMargin * leftMarginFactor;
    }

    try {
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Graphics2D g2 = cb.createGraphicsShapes(paperWidth, paperHeight);
        g2.translate(leftMargin, data.topMargin * resolution);
        mainFrame.getMusicSheet().getBestDrawer().drawMusicSheet(g2, false, scale);
        g2.dispose();
        document.close();

        if (isGUI) {
            Utilities.openExportFile(mainFrame, outputFile);
        }
    } catch (DocumentException e1) {
        if (isGUI) {
            mainFrame.showErrorMessage("An unexpected error occurred and could not export as PDF.");
        }

        logger.error("PDF save", e1);
    } catch (FileNotFoundException e1) {
        if (isGUI) {
            mainFrame.showErrorMessage(MainFrame.COULD_NOT_SAVE_MESSAGE);
        }

        logger.error("PDF save", e1);
    }
}