Example usage for com.itextpdf.text Document newPage

List of usage examples for com.itextpdf.text Document newPage

Introduction

In this page you can find the example usage for com.itextpdf.text Document newPage.

Prototype


public boolean newPage() 

Source Link

Document

Signals that an new page has to be started.

Usage

From source file:com.devox.GUI.PDF.CrearReporteTarimas.java

@Override
public void crear(File file) {
    try {//  w w  w.  j  ava2 s.  c o m
        Document document = new Document(PageSize.A4.rotate());
        file.createNewFile();
        PdfWriter w = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        setLogo();
        CabeceraPieDePagina event = new CabeceraPieDePagina();
        w.setPageEvent(event);
        document.setMargins(50, 50, 100, 50);
        document.newPage();
        document.add(configurarInformacion());
        PdfPTable table = crearTabla();
        agregarProductos(table);
        document.add(table);
        document.close();
    } catch (Exception ex) {
        Log.print(ex);
    }

    Object[] options = { "Abrir PDF", "No" };
    int open = JOptionPane.showOptionDialog(null, "Desea abrir el reporte?", "Reporte guardado",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
            new javax.swing.ImageIcon(getClass().getResource("/com/devox/GUI/images/print.png")), options,
            options[0]);
    if (open == JOptionPane.YES_OPTION) {
        try {
            Desktop.getDesktop().open(file);
        } catch (IOException ex) {
            Log.print("No se encontr la ruta");
            JOptionPane.showMessageDialog(null, "Error en el archivo");
        }
    }
}

From source file:com.devox.GUI.PDF.ExportarAPDF.java

public void crearDestruccion(File file) {
    try {/*from   w w w  .j ava2 s .com*/
        Document document = new Document(PageSize.A4);
        file.createNewFile();
        PdfWriter w = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        setLogo();
        CabeceraPieDePagina event = new CabeceraPieDePagina();
        w.setPageEvent(event);
        document.setMargins(50, 50, 100, 220);
        document.newPage();
        document.add(setUpInformation());
        PdfPTable t = createTable();
        addProductos(model, t);
        document.add(t);
        //
        document.close();
    } catch (Exception ex) {
        Log.print(ex);
        Log.print(ex);
    }
}

From source file:com.devox.GUI.PDF.ExportarAPDF.java

public void crearApto(File file) {
    try {//from w w  w . ja v a2 s .c o  m
        Document document = new Document(PageSize.A4);
        file.createNewFile();
        PdfWriter w = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        setLogo();
        CabeceraPieDePagina event = new CabeceraPieDePagina();
        w.setPageEvent(event);
        document.setMargins(50, 50, 100, 220);
        document.newPage();
        document.add(setUpInformationTarimas());
        //            PdfPTable t = createTable();
        //            addProductos(model, t);
        //            document.add(t);
        //
        document.close();
    } catch (Exception ex) {
        Log.print(ex);
        Log.print(ex);
    }
}

From source file:com.devox.GUI.PDF.ExportarAPDF.java

public void crearTarimas(File file) {
    try {/*from w w  w . j  a  v  a 2  s  . c  o  m*/
        Document document = new Document(PageSize.A4.rotate());
        file.createNewFile();
        PdfWriter w = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        setLogo();
        CabeceraPieDePagina2 event = new CabeceraPieDePagina2();
        w.setPageEvent(event);
        document.setMargins(50, 50, 100, 50);
        document.newPage();
        document.add(setUpInformationTarimas());
        PdfPTable t = createTableTarimas();
        addProductosTarimas(t);
        document.add(t);
        //
        document.close();
    } catch (Exception ex) {
        Log.print(ex);
        Log.print(ex);
    }
}

From source file:com.elsys.generator.NewPdfGenerator.java

private static void addTitlePage(Document document) throws DocumentException {
    Paragraph preface = new Paragraph();
    // We add one empty line
    addEmptyLine(preface, 1);/* www.j ava 2  s. com*/
    // Lets write a big header
    preface.add(new Paragraph("Bitwise operations", catFont));

    addEmptyLine(preface, 1);
    // Will create: Report generated by: _name, _date
    preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            smallBold));
    addEmptyLine(preface, 3);
    preface.add(new Paragraph("This document describes something which is very important ", smallBold));

    addEmptyLine(preface, 8);

    preface.add(new Paragraph(
            "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
            redFont));

    document.add(preface);
    // Start a new page
    document.newPage();
}

From source file:com.ephesoft.dcma.imagemagick.MultiPageExecutor.java

License:Open Source License

/**
 * This method creates multi page pdf using IText.
 * //ww  w . j a  va 2  s .  c o  m
 * @param batchInstanceThread {@link BatchInstanceThread}
 * @param pages11 {@link String}
 * @param widthOfPdfPage int
 * @param heightOfPdfPage int
 */
public MultiPageExecutor(BatchInstanceThread batchInstanceThread, final String[] pages11,
        final int widthOfPdfPage, final int heightOfPdfPage) {
    if (pages11 != null && pages11.length > 0) {
        this.pages = new String[pages11.length];
        this.pages = pages11.clone();
        batchInstanceThread.add(new AbstractRunnable() {

            @Override
            public void run() {
                String pdf = pages[pages.length - 1];
                Document document = null;
                PdfWriter writer = null;
                RandomAccessFileOrArray randomAccessArray = null;
                try {
                    document = new Document(PageSize.LETTER, 0, 0, 0, 0);
                    writer = PdfWriter.getInstance(document, new FileOutputStream(pdf));
                    document.open();
                    int comps = 1;
                    int totalTiffImages = pages.length - 1;
                    int index = 0;
                    while (index < totalTiffImages) {
                        randomAccessArray = new RandomAccessFileOrArray(pages[index]);
                        comps = TiffImage.getNumberOfPages(randomAccessArray);
                        // Conversion statement
                        for (int tiffPageNumber = 0; tiffPageNumber < comps; ++tiffPageNumber) {
                            Image img = TiffImage.getTiffImage(randomAccessArray, tiffPageNumber + 1);
                            img.scaleToFit(widthOfPdfPage, heightOfPdfPage);
                            document.add(img);
                            document.newPage();
                        }
                        index++;
                    }
                } catch (Exception e) {
                    LOGGER.error("Error while creating pdf using iText" + e.getMessage(), e);
                    //pdf = null;
                } finally {
                    try {
                        if (document != null) {
                            document.close();
                        }
                        if (writer != null) {
                            writer.close();
                        }
                        if (randomAccessArray != null) {
                            randomAccessArray.close();
                        }
                    } catch (Exception e) {
                        LOGGER.error("Error while closing I/O streams for write PDF. " + e.getMessage());
                    }
                }
            }
        });
    }
}

From source file:com.ephesoft.dcma.imagemagick.MultiPageExecutor.java

License:Open Source License

/**
 * The <code>MultiPageExecutor</code> method creates multi page searchable pdf using IText.
 * //from  w w  w  .j  a v a  2  s  . co  m
 * @param batchInstanceThread {@link BatchInstanceThread} thread instance of batch
 * @param imageHtmlMap {@link Map} map containing image url with corresponding hocr
 * @param isColoredPDF true for colored image pdf else otherwise
 * @param isSearchablePDF true for searchable pdf else otherwise
 * @param pdfFilePath {@link String} path where new pdf has to be created
 * @param widthOfLine Integer for line width to be used
 */
public MultiPageExecutor(BatchInstanceThread batchInstanceThread, final Map<String, HocrPage> imageHtmlMap,
        final boolean isColoredPDF, final boolean isSearchablePDF, final String pdfFilePath,
        final int widthOfLine, final String[] pages) {
    if (imageHtmlMap != null && !imageHtmlMap.isEmpty()) {
        this.pages = new String[pages.length];
        this.pages = pages.clone();
        batchInstanceThread.add(new AbstractRunnable() {

            @Override
            public void run() {
                String pdf = pdfFilePath;
                Document document = null;
                PdfWriter writer = null;
                FileOutputStream fileOutputStream = null;
                boolean isPdfSearchable = isSearchablePDF;
                boolean isColoredImage = isColoredPDF;
                LOGGER.info("is searchable pdf: " + isPdfSearchable + ", is Colored Image: " + isColoredImage);
                try {
                    document = new Document(PageSize.LETTER, 0, 0, 0, 0);
                    fileOutputStream = new FileOutputStream(pdf);
                    writer = PdfWriter.getInstance(document, fileOutputStream);
                    document.open();
                    Set<String> imageSet = imageHtmlMap.keySet();
                    for (String imageUrl : imageSet) {
                        HocrPage hocrPage = imageHtmlMap.get(imageUrl);
                        String newImageUrl = getCompressedImage(isColoredImage, imageUrl);
                        LOGGER.info("New Image URL: " + newImageUrl);
                        addImageToPdf(writer, hocrPage, newImageUrl, isPdfSearchable, widthOfLine);
                        document.newPage();
                        (new File(newImageUrl)).delete();
                    }
                } catch (FileNotFoundException fileNotFoundException) {
                    LOGGER.error("Error occurred while creating pdf " + pdf + " : "
                            + fileNotFoundException.toString());
                } catch (DocumentException documentException) {
                    LOGGER.error(
                            "Error occurred while creating pdf " + pdf + " : " + documentException.toString());
                } finally {
                    if (document != null && document.isOpen()) {
                        document.close();
                    }
                    // Closing pdf writer
                    if (null != writer) {
                        writer.close();
                    }

                    // Closing file output stream of pdf
                    if (null != fileOutputStream) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException ioException) {
                            LOGGER.error("Error occurred while closing stream for pdf " + pdf + " : "
                                    + ioException.toString());
                        }
                    }
                }
            }
        });
    }
}

From source file:com.ephesoft.dcma.util.PDFUtil.java

License:Open Source License

/**
 * The <code>getSelectedPdfFile</code> method is used to limit the file
 * to the page limit given./*from  w  w w  . j av  a2 s .  c om*/
 * 
 * @param pdfFile {@link File} pdf file from which limit has to be applied
 * @param pageLimit int
 * @throws IOException if file is not found
 * @throws DocumentException if document cannot be created
 */
public static void getSelectedPdfFile(final File pdfFile, final int pageLimit)
        throws IOException, DocumentException {
    PdfReader reader = null;
    Document document = null;
    PdfContentByte contentByte = null;
    PdfWriter writer = null;
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    File newFile = null;
    if (null != pdfFile && pdfFile.exists()) {
        try {
            document = new Document();
            fileInputStream = new FileInputStream(pdfFile);

            String name = pdfFile.getName();
            final int indexOf = name.lastIndexOf(IUtilCommonConstants.DOT);
            name = name.substring(0, indexOf);
            final String finalPath = pdfFile.getParent() + File.separator + name + System.currentTimeMillis()
                    + IUtilCommonConstants.EXTENSION_PDF;
            newFile = new File(finalPath);
            fileOutputStream = new FileOutputStream(finalPath);
            writer = PdfWriter.getInstance(document, fileOutputStream);
            document.open();
            contentByte = writer.getDirectContent();

            reader = new PdfReader(fileInputStream);
            for (int i = 1; i <= pageLimit; i++) {
                document.newPage();

                // import the page from source pdf
                final PdfImportedPage page = writer.getImportedPage(reader, i);

                // add the page to the destination pdf
                contentByte.addTemplate(page, 0, 0);
                page.closePath();
            }
        } finally {
            closePassedStream(reader, document, contentByte, writer, fileInputStream, fileOutputStream);
        }
        if (pdfFile.delete() && null != newFile) {
            newFile.renameTo(pdfFile);
        } else {
            if (null != newFile) {
                newFile.delete();
            }
        }
    }
}

From source file:com.equiworx.util.Main.java

private static void addTitlePage(Document document) throws DocumentException {
    Paragraph preface = new Paragraph();
    // We add one empty line
    addEmptyLine(preface, 1);/*from  w w w  .j av a 2  s  . co  m*/
    // Lets write a big header
    preface.add(new Paragraph("Title of the document", catFont));

    addEmptyLine(preface, 1);
    // Will create: Report generated by: _name, _date//System.getProperty("user.name")
    preface.add(new Paragraph("Report generated by: " + "Balwinder Singh" + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            smallBold));
    addEmptyLine(preface, 3);
    preface.add(new Paragraph("This document describes something which is very important ", smallBold));

    addEmptyLine(preface, 30);

    preface.add(new Paragraph(
            "This document is a preliminary version and not subject to your license agreement or anycvcxvcxvcxvcxvcxvvcxvvcxcxv"
                    + "vcx" + "cxv" + "cxv" + "vcx" + "vcx" + "vcx" + "vcx" + "vcx" + "vcx" + "vcx" + "vcx"
                    + "vcxsdasdsadasdsadsaffdsafds                                                               "
                    + "vcx                                                                          "
                    + "vcx                                                          "
                    + "vcx                                                                                       "
                    + "vcx                                                                           "
                    + "vcx                                                            "
                    + "vcx                                                                                   "
                    + "vcx                                                       "
                    + "vcx                                                                       "
                    + "vcx                                                   "
                    + "vcx                                                              "
                    + "vcx                                " + "                                  "
                    + "                                 "
                    + "ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
                    + "ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" + ""
                    + "                   " + "vcx" + "vcx" + "vcx" + "vcx" + "vcx" + "vcx" + "vcvc" + "vcx"
                    + "vcx" + "vcxvcxvccvcxvcxvcxvxcvcxvcxvcx"
                    + "        vbclbclkbfgkkfgfgkfgk other agreement with vogella.de ;-).",
            redFont));

    document.add(preface);
    // Start a new page
    document.newPage();
}

From source file:com.etest.pdfgenerator.TQCoveragePDF.java

public TQCoveragePDF(int tqCoverageId) {
    this.tqCoverageId = tqCoverageId;

    Document document = null;

    try {/*from   www  .jav a 2s.c  o m*/
        document = new Document(PageSize.A4, 50, 50, 50, 50);
        PdfWriter.getInstance(document, outputStream);
        document.open();

        Font header1 = FontFactory.getFont("Times-Roman", 14, Font.BOLD);
        Font header2 = FontFactory.getFont("Times-Roman", 12, Font.BOLD);
        Font content = FontFactory.getFont("Times-Roman", 10);

        Image img = null;
        try {
            img = Image.getInstance("C:\\eTest-images\\SUCN_seal.png");
            img.scaleToFit(60, 60);
            img.setAbsolutePosition(100, 720);
        } catch (BadElementException | IOException ex) {
            Logger.getLogger(TQCoveragePDF.class.getName()).log(Level.SEVERE, null, ex);
        }
        document.add(img);

        Paragraph title = new Paragraph();
        title.setAlignment(Element.ALIGN_CENTER);
        title.add(new Phrase("COLLEGE OF NURSING", header2));
        document.add(title);

        Paragraph school = new Paragraph();
        school.setAlignment(Element.ALIGN_CENTER);
        school.add(new Phrase("Siliman University", header2));
        document.add(school);

        Paragraph location = new Paragraph();
        location.setSpacingAfter(10f);
        location.setAlignment(Element.ALIGN_CENTER);
        location.add(new Phrase("Dumaguete City", header2));
        document.add(location);

        Paragraph examTitle = new Paragraph();
        examTitle.setSpacingAfter(20f);
        examTitle.setAlignment(Element.ALIGN_CENTER);
        examTitle
                .add(new Phrase(
                        cs.getCurriculumById(tq.getTQCoverageById(getTQCoverageId()).getCurriculumId())
                                .getSubject() + " " + tq.getTQCoverageById(getTQCoverageId()).getExamTitle(),
                        header2));
        document.add(examTitle);

        Paragraph instruction = new Paragraph();
        instruction.setSpacingAfter(5f);
        instruction.setAlignment(Element.ALIGN_LEFT);
        instruction.add(
                new Phrase("INSTRUCTIONS: Read the cases carefully. Choose the letter of the correct answer. "
                        + "Use an answer sheet and follow instruction for its use.", content));
        document.add(instruction);

        int itemNo = 1;
        Map<Integer, Map<Integer, Integer>> tqCoverage = tq.getTQCoverage(getTQCoverageId());
        for (Map.Entry<Integer, Map<Integer, Integer>> tqCases : tqCoverage.entrySet()) {
            Integer tqCaseId = tqCases.getKey();

            Label caseTopic = new Label();
            caseTopic.setValue(ccs.getCellCaseById(tqCaseId).getCaseTopic());
            caseTopic.setContentMode(ContentMode.HTML);
            document.add(new Paragraph(caseTopic.getValue().replaceAll("(?i)<p.*?>.*?</p>", ""), content));

            Map<Integer, Integer> value = tqCases.getValue();
            for (Map.Entry<Integer, Integer> itemIds : value.entrySet()) {
                Integer itemId = itemIds.getKey();
                Integer itemKeyId = itemIds.getValue();

                List<String> keyList = k.getAllItemKey(itemId);
                if (keyList.isEmpty()) {
                    ShowErrorNotification.error(
                            "No Item Key was found for STEM: \n" + cis.getCellItemById(itemId).getItem());
                    return;
                }

                Label stem = new Label();
                //                    stem.setValue(itemNo+". "+cis.getCellItemById(itemId).getItem().replace("{key}", keyList.get(0)));
                stem.setValue(itemNo + ". "
                        + cis.getCellItemById(itemId).getItem().replace("{key}", k.getItemKeyById(itemKeyId)));
                stem.setContentMode(ContentMode.HTML);
                document.add(new Paragraph(stem.getValue(), content));

                PdfPTable table = new PdfPTable(2);
                table.setWidthPercentage(100);
                table.setSpacingBefore(10f);
                table.setSpacingAfter(10f);

                //Set Column widths
                float[] columnWidths = { 1f, 1f };
                table.setWidths(columnWidths);

                PdfPCell cell1 = new PdfPCell(
                        new Paragraph("A) " + cis.getCellItemById(itemId).getOptionA(), content));
                //                cell1.setBorderColor(BaseColor.BLUE);
                cell1.setBorder(0);
                cell1.setPaddingLeft(10);
                cell1.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
                cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);

                PdfPCell cell2 = new PdfPCell(
                        new Paragraph("C) " + cis.getCellItemById(itemId).getOptionC(), content));
                //                cell2.setBorderColor(BaseColor.GREEN);
                cell2.setBorder(0);
                cell2.setPaddingLeft(10);
                cell2.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
                cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);

                PdfPCell cell3 = new PdfPCell(
                        new Paragraph("B) " + cis.getCellItemById(itemId).getOptionB(), content));
                //                cell3.setBorderColor(BaseColor.RED);
                cell3.setBorder(0);
                cell3.setPaddingLeft(10);
                cell3.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
                cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);

                PdfPCell cell4 = new PdfPCell(
                        new Paragraph("D) " + cis.getCellItemById(itemId).getOptionD(), content));
                //                cell4.setBorderColor(BaseColor.RED);
                cell4.setBorder(0);
                cell4.setPaddingLeft(10);
                cell4.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
                cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);

                table.addCell(cell1);
                table.addCell(cell2);
                table.addCell(cell3);
                table.addCell(cell4);

                document.add(table);

                itemNo++;
            }
        }

        document.newPage();

        Paragraph ticketNo = new Paragraph();
        ticketNo.setSpacingAfter(30f);
        ticketNo.setAlignment(Element.ALIGN_LEFT);
        ticketNo.add(new Phrase("TQ Ticket #: " + tq.getTqCoverageTicketNo(getTQCoverageId()), content));
        document.add(ticketNo);

        document.add(new Paragraph("Answer Key: "));

        itemNo = 1;
        List<TQAnswerKey> answerKey = tq.getTQCoverageAnswerKey(getTQCoverageId());
        for (TQAnswerKey t : answerKey) {
            document.add(new Paragraph(
                    t.getItemNo() + ": " + cis.getOptionAnswer(t.getCellItemId()).get(t.getAnswer())));
        }

    } catch (DocumentException ex) {
        Logger.getLogger(TQCoveragePDF.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (document != null) {
            document.close();
        }
    }
}