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

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

Introduction

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

Prototype

public void setPageEmpty(boolean pageEmpty) 

Source Link

Document

Use this method to make sure a page is added, even if it's empty.

Usage

From source file:at.tugraz.sss.serv.SSFileU.java

License:Apache License

public static void writePDFFromText(final String pdfFilePath, final String textFilePath) throws Exception {

    OutputStream out = null;/*w ww .  j a  va2 s  . co  m*/
    BufferedReader br = null;

    try {

        out = openOrCreateFileWithPathForWrite(pdfFilePath);

        final Document document = new Document();
        final PdfWriter writer = PdfWriter.getInstance(document, out);
        String line;

        document.open();
        writer.setPageEmpty(true);
        document.newPage();
        writer.setPageEmpty(true);

        br = new BufferedReader(new FileReader(new File(textFilePath)));

        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line));
        }

        document.close();

    } catch (Exception error) {

        if (out != null) {
            out.close();
        }

        if (br != null) {
            br.close();
        }
    }
}

From source file:at.tugraz.sss.serv.SSFileU.java

License:Apache License

public static void writePDFFromDoc(final String docFilePath, final String pdfFilePath) throws Exception {

    final Document document = new Document();
    final POIFSFileSystem fs = new POIFSFileSystem(openFileForRead(docFilePath));
    final HWPFDocument word = new HWPFDocument(fs);
    final WordExtractor we = new WordExtractor(word);
    final OutputStream out = openOrCreateFileWithPathForWrite(pdfFilePath);
    final PdfWriter writer = PdfWriter.getInstance(document, out);
    final Range range = word.getRange();

    document.open();/*from w  ww .  ja  v  a 2  s. c o  m*/
    writer.setPageEmpty(true);
    document.newPage();
    writer.setPageEmpty(true);

    String[] paragraphs = we.getParagraphText();

    for (int i = 0; i < paragraphs.length; i++) {

        org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
        // CharacterRun run = pr.getCharacterRun(i);
        // run.setBold(true);
        // run.setCapitalized(true);
        // run.setItalic(true);
        paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
        System.out.println("Length:" + paragraphs[i].length());
        System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());

        // add the paragraph to the document
        document.add(new Paragraph(paragraphs[i]));
    }

    document.close();
}

From source file:at.tugraz.sss.serv.util.SSFileU.java

License:Apache License

public static void writePDFFromText(final String pdfFilePath, final String textFilePath) throws SSErr {

    OutputStream out = null;/*ww  w .  j  a va  2 s. c om*/
    BufferedReader br = null;

    try {

        out = openOrCreateFileWithPathForWrite(pdfFilePath);

        final Document document = new Document();
        final PdfWriter writer = PdfWriter.getInstance(document, out);
        String line;

        document.open();
        writer.setPageEmpty(true);
        document.newPage();
        writer.setPageEmpty(true);

        br = new BufferedReader(new FileReader(new File(textFilePath)));

        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line));
        }

        document.close();

    } catch (Exception error) {
        SSServErrReg.regErrThrow(error);
    } finally {

        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                SSLogU.err(ex);
            }
        }

        if (br != null) {
            try {
                br.close();
            } catch (IOException ex) {
                SSLogU.err(ex);
            }
        }
    }
}

From source file:at.tugraz.sss.serv.util.SSFileU.java

License:Apache License

public static void writePDFFromDoc(final String docFilePath, final String pdfFilePath) throws SSErr {

    try {/*from   w  ww.  j  a v a 2s . co  m*/
        final Document document = new Document();
        final POIFSFileSystem fs = new POIFSFileSystem(openFileForRead(docFilePath));
        final HWPFDocument word = new HWPFDocument(fs);
        final WordExtractor we = new WordExtractor(word);
        final OutputStream out = openOrCreateFileWithPathForWrite(pdfFilePath);
        final PdfWriter writer = PdfWriter.getInstance(document, out);
        final Range range = word.getRange();

        document.open();
        writer.setPageEmpty(true);
        document.newPage();
        writer.setPageEmpty(true);

        String[] paragraphs = we.getParagraphText();

        for (int i = 0; i < paragraphs.length; i++) {

            org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
            // CharacterRun run = pr.getCharacterRun(i);
            // run.setBold(true);
            // run.setCapitalized(true);
            // run.setItalic(true);
            paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
            System.out.println("Length:" + paragraphs[i].length());
            System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());

            // add the paragraph to the document
            document.add(new Paragraph(paragraphs[i]));
        }

        document.close();
    } catch (Exception error) {
        SSServErrReg.regErrThrow(error);
    }
}

From source file:com.unsa.view.MainView.java

License:Creative Commons License

private void DocConverterPDF(File file1) {
    NPOIFSFileSystem fs = null;// w ww .  j  a v a 2 s  . co  m
    com.lowagie.text.Document document = new com.lowagie.text.Document();

    try {
        System.out.println(file1.getAbsolutePath());
        fs = new NPOIFSFileSystem(new FileInputStream(file1.getAbsolutePath()));
        HWPFDocument doc = new HWPFDocument(fs.getRoot());
        WordExtractor we = new WordExtractor(doc);
        String output = file1.getAbsolutePath().substring(0, file1.getAbsolutePath().length() - 3);
        OutputStream fileout = new FileOutputStream(new File(output + "pdf"));

        PdfWriter writer = PdfWriter.getInstance(document, fileout);

        Range range = doc.getRange();
        document.open();
        writer.setPageEmpty(true);
        document.newPage();
        writer.setPageEmpty(true);

        String[] paragraphs = we.getParagraphText();
        for (int i = 0; i < paragraphs.length; i++) {

            org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
            paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
            document.add(new Paragraph(paragraphs[i]));
        }

    } catch (Exception e) {

        e.printStackTrace();
    } finally {

        document.close();
    }

}

From source file:questions.ocg.AddOptionalContentToExistingPdf.java

public static void main(String[] args) throws IOException, DocumentException {
    // creating an empty document
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT01));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();//w  ww .  ja v  a2  s  . co m
    writer.setPageEmpty(false);
    document.close();

    PdfReader reader = new PdfReader(RESULT01);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT02));
    PdfContentByte cb = stamper.getOverContent(1);
    writer = stamper.getWriter();
    PdfLayer nested = new PdfLayer("Nested Layers", writer);
    PdfLayer nested_1 = new PdfLayer("Nested Layer 1", writer);
    PdfLayer nested_2 = new PdfLayer("Nested Layer 2", writer);
    PdfLayer layer21 = new PdfLayer("Layer 2", writer);
    PdfLayer layer22 = new PdfLayer("Layer 2", writer);
    nested_2.addChild(layer21);
    nested_2.addChild(layer22);
    nested.addChild(nested_1);
    nested.addChild(nested_2);
    cb.beginLayer(nested);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layers"), 50, 775, 0);
    cb.endLayer();
    cb.beginLayer(nested_1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 1"), 100, 800, 0);
    cb.endLayer();
    cb.beginLayer(nested_2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 2"), 100, 750, 0);
    cb.endLayer();
    cb.beginLayer(layer21);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2.1 in the group"), 150, 775, 0);
    cb.endLayer();
    cb.beginLayer(layer22);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2.2 in the group"), 150, 725, 0);
    cb.endLayer();
    PdfLayer group = PdfLayer.createTitle("Grouped layers", writer);
    PdfLayer layer1 = new PdfLayer("Group: layer 1", writer);
    PdfLayer layer2 = new PdfLayer("Group: layer 2", writer);
    group.addChild(layer1);
    group.addChild(layer2);
    cb.beginLayer(layer1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 1 in the group"), 50, 700, 0);
    cb.endLayer();
    cb.beginLayer(layer2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2 in the group"), 50, 675, 0);
    cb.endLayer();
    PdfLayer radiogroup = PdfLayer.createTitle("Radio Group", writer);
    PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", writer);
    radio1.setOn(true);
    PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", writer);
    radio2.setOn(false);
    PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", writer);
    radio3.setOn(false);
    radiogroup.addChild(radio1);
    radiogroup.addChild(radio2);
    radiogroup.addChild(radio3);
    ArrayList<PdfLayer> options = new ArrayList<PdfLayer>();
    options.add(radio1);
    options.add(radio2);
    options.add(radio3);
    writer.addOCGRadioGroup(options);
    cb.beginLayer(radio1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 1"), 50, 600, 0);
    cb.endLayer();
    cb.beginLayer(radio2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 2"), 50, 575, 0);
    cb.endLayer();
    cb.beginLayer(radio3);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 3"), 50, 550, 0);
    cb.endLayer();
    PdfLayer not_printed = new PdfLayer("not printed", writer);
    not_printed.setOnPanel(false);
    not_printed.setPrint("Print", false);
    cb.beginLayer(not_printed);
    ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("PRINT THIS PAGE"), 300, 700, 90);
    cb.endLayer();
    PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", writer);
    zoom.setOnPanel(false);
    zoom.setZoom(0.75f, 1.25f);
    cb.beginLayer(zoom);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,
            new Phrase("Only visible if the zoomfactor is between 75 and 125%"), 30, 530, 90);
    cb.endLayer();
    stamper.close();

    reader = new PdfReader(RESULT02);
    stamper = new PdfStamper(reader, new FileOutputStream(RESULT03));
    Map ocg = stamper.getPdfLayers();
    for (Iterator i = ocg.keySet().iterator(); i.hasNext();) {
        System.out.println(i.next());
    }
    PdfLayer layer = (PdfLayer) ocg.get("Layer 2");
    layer.setOn(false);
    cb = stamper.getOverContent(1);
    cb.beginLayer(layer);
    cb.moveTo(0, 0);
    cb.lineTo(500, 500);
    cb.stroke();
    cb.endLayer();
    PdfLayer parent = (PdfLayer) ocg.get("Layer 2(2)");
    PdfLayer newLayer = new PdfLayer("Child layer", stamper.getWriter());
    newLayer.setOn(false);
    parent.addChild(newLayer);
    cb.beginLayer(newLayer);
    cb.moveTo(0, 500);
    cb.lineTo(500, 0);
    cb.stroke();
    cb.endLayer();
    stamper.close();
}

From source file:test.itext.html.Main.java

License:Open Source License

public static void main(String args[]) throws Exception {
    Document document = new Document();
    StringBuffer sb = new StringBuffer();
    sb.append("<root><p><u><em><strong>Product Description</strong></em></u></p>");
    sb.append(/*  w  ww  .j  av a  2 s. co m*/
            "<p style=\"text-align: justify\">Face the biggest test in soccer and come out on top with FIFA 08. Bringing home the silverware is harder than ever before as you make critical selection decisions and battle bookings, injuries and fatigue to produce a winning team. Defeat the toughest opposition with your in-form players overpower teams with quick movement off the ball and varying attacks. Select from fully-licensed teams, raise your game with the help of the crowd and start your quest for glory.</p>");
    sb.append("<p style=\"text-align: justify\"><strong>Starting the game</strong></p>");
    sb.append(
            "<p style=\"text-align: justify\">Highlight EA SPORTS FIFA 08 in the games menu on your phone. Press Select (or OK on the D-Pad) to start the game. You are prompted to choose sound either on or off. After you make your selection, the EA Mobile splash screen appears followed by EA SPORTS FIFA 08 title sequence and the Main Menu.<br />");
    sb.append("</p>");
    sb.append("<p style=\"text-align: justify\"><strong>Main Menu</strong></p>");
    sb.append("<ul>");
    sb.append("<li>Play Start a game.</li>");
    sb.append(
            "<li>Options View the available game options: sound, vibration, match length, difficulty, etc</li>");
    sb.append("<li>Help View detailed instructions and information on how to play the game.</li>");
    sb.append("<li>About Display copyright information and customer service information.</li>");
    sb.append("<li>More Games Display link to more EA Mobile games.</li>");
    sb.append("<li>Exit Exit the game.</li>");
    sb.append("</ul></root>");

    /*Tidy tidy=new Tidy();
    tidy.setXmlOut(true);
    tidy.setXmlTags(true);      
    byte[] buff=sb.toString().getBytes("utf-8");
    ByteArrayOutputStream os=new ByteArrayOutputStream();      
    tidy.parse(new ByteArrayInputStream(buff), os);
    String temp=os.toString("utf-8");*/

    try {
        Reader reader = new StringReader(sb.toString());
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("html1.pdf"));
        document.open();
        writer.setPageEmpty(false);
        HtmlParser.parse(document, reader);

    } catch (Exception e) {
        e.printStackTrace();
    }
    if (document.isOpen())
        document.close();
}