Example usage for com.lowagie.text.pdf PdfWriter open

List of usage examples for com.lowagie.text.pdf PdfWriter open

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter open.

Prototype

public void open() 

Source Link

Document

Signals that the Document has been opened and that Elements can be added.

Usage

From source file:org.dgl.imgstopdf.Main.java

public static void main(String[] args) {
    Document document = new Document();
    ArrayList<String> input;
    String output = ".\\out.pdf";
    try {// w ww  . jav a2 s  .  c  o m
        FileOutputStream fos = new FileOutputStream(output);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
        Image image;
        JFileChooser fc = new JFileChooser(".\\");
        fc.setMultiSelectionEnabled(true);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("IMAGES", "jpg", "jpeg", "gif", "png",
                "bmp");
        fc.setFileFilter(filter);
        int userOption = fc.showOpenDialog(null);
        if (userOption == JFileChooser.APPROVE_OPTION) {
            input = new ArrayList<String>();
            for (File f : fc.getSelectedFiles()) {
                input.add(f.getAbsolutePath());
            }
            if (fc.getSelectedFile() != null) {
                input.add(fc.getSelectedFile().getAbsolutePath());
            }
        } else {
            return;
        }
        if (input.size() == 0) {
            return;
        }
        pdfWriter.open();
        document.open();
        for (Object fileName : input.toArray()) {
            image = Image.getInstance((String) fileName);
            float scaler = ((document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin())
                    / image.getWidth()) * 100;
            image.scalePercent(scaler);
            document.add(image);
        }
        document.close();
        pdfWriter.close();
        JOptionPane.showMessageDialog(null, "OK", "", JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, ex.toString(), "", JOptionPane.ERROR_MESSAGE);
    }
}