如何在使用iText生成的PDF文档中添加空白页?

时间:2015-04-21 13:00:03

标签: java java-ee pdf pdf-generation itext

我正在研究过去由其他人编写的一些iText代码,我遇到了以下问题。

这是一种创建包含某些信息的页面的方法。我只需要在此页面后添加一个新的空页面,但它似乎不起作用(我仍然获得原始的信息页面)。

所以我有这段代码:

public ByteArrayOutputStream stampaTestataFatturaPdf(Fattura fattura) {

    ByteArrayOutputStream currentFatturaHeaderPdf = new ByteArrayOutputStream();

    // STEP 1 Creazione del documento in formato A4 e senza margini:
    com.itextpdf.text.Document document = new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4, 0, 0, 0, 0);

    float fntSize = 10f;

    try {
        /* STEP 2 Constructs a PdfWriter.
                  document: The PdfDocument that has to be written.
                  os: The OutputStream the writer has to write to
         */

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

        // STEP 3:
        document.open();

        // STEP 4:

        // TABELLA MITTENTE:
        // La lunghezza e la larghezza sono invertite perchè la cella della tabella è ruotata di 90°:
        float lunghezza_mit = com.itextpdf.text.Utilities.millimetersToPoints(35);      // 3,5 cm
        float altezza_mit = com.itextpdf.text.Utilities.millimetersToPoints(70);        // 7 cm

        PdfPTable mittenteTable = new PdfPTable(1); // 1 columns.
        mittenteTable.setTotalWidth(lunghezza_mit);

        PdfContentByte canvasMittente = writer.getDirectContent();

        canvasMittente.setColorStroke(BaseColor.WHITE);

        PdfPCell cell1Mittente = new PdfPCell();

        Phrase mittente = new Phrase(fattura.getMittente(), FontFactory.getFont(FontFactory.COURIER, fntSize));
        Phrase indirizzoMittenteSection1 = new Phrase(fattura.getIndirizzoMittente(), FontFactory.getFont(FontFactory.COURIER, fntSize));
        Phrase indirizzoMittenteSection2 = new Phrase(fattura.getComuneMittente() + " " + fattura.getCapMittente() + " (" + fattura.getProvinciaMittente() + ")", FontFactory.getFont(FontFactory.COURIER, fntSize));
        Phrase partitaIvaMittente = new Phrase(fattura.getPartivaIVAMittente(), FontFactory.getFont(FontFactory.COURIER, fntSize));
        Phrase telFax = new Phrase("Tel: " + fattura.getTelMittente() + " Fax:" + fattura.getFaxMittente(), FontFactory.getFont(FontFactory.COURIER, fntSize));

        cell1Mittente.addElement(mittente);
        cell1Mittente.setBorderColor(BaseColor.WHITE);
        cell1Mittente.addElement(indirizzoMittenteSection1);
        cell1Mittente.addElement(indirizzoMittenteSection2);
        cell1Mittente.addElement(partitaIvaMittente);
        cell1Mittente.addElement(telFax);

        cell1Mittente.setRotation(90);
        cell1Mittente.setFixedHeight(altezza_mit);

        mittenteTable.addCell(cell1Mittente);

        float shift_x1_mitt = com.itextpdf.text.Utilities.millimetersToPoints(15);     // 1,5 cm
        float shift_y1_mitt = altezza_mit + com.itextpdf.text.Utilities.millimetersToPoints(85);     // 8,5 cm

        mittenteTable.writeSelectedRows(0, -1, shift_x1_mitt, shift_y1_mitt, canvasMittente);


        // TABELLA DESTINATARIO:
        // La lunghezza e la larghezza sono invertite perchè la cella della tabella è ruotata di 90°:
        float lunghezza_dest = com.itextpdf.text.Utilities.millimetersToPoints(45);      // 4,5 cm
        float altezza_dest = com.itextpdf.text.Utilities.millimetersToPoints(95);        // 9,5 cm

        PdfPTable destinatarioTable = new PdfPTable(1); // 1 columns.
        destinatarioTable.setTotalWidth(lunghezza_dest);

        PdfContentByte canvasDestinatario = writer.getDirectContent();

        //PdfPCell cell1Destinatario = new PdfPCell(new Paragraph("DESTINATARIO"));
        PdfPCell cell1Destinatario = new PdfPCell();

        Phrase spettabile = new Phrase("Spett.le", FontFactory.getFont(FontFactory.COURIER, fntSize));
        Phrase destinatario = new Phrase(commonUtility.gestioneCaratteriSpeciali(fattura.getDestinatario()), FontFactory.getFont(FontFactory.COURIER, fntSize));
        Phrase indirizzoDestinatarioSection1 = new Phrase(commonUtility.gestioneCaratteriSpeciali(fattura.getIndirizzoDestinatario()), FontFactory.getFont(FontFactory.COURIER, fntSize));

        Phrase indirizzoDestinatarioSection2;
        if (fattura.getProvinciaDestinatario() != null) {

            if (nazionalitaFatturaCorrente.equals("E")) {  // Formattazione dell'indirizzo per le fatture estere
                indirizzoDestinatarioSection2 = new Phrase(commonUtility.gestioneCaratteriSpeciali((fattura.getCapDestinatario() != null ? fattura.getCapDestinatario() : "") + fattura.getComuneDestinatario()) + " (" + fattura.getProvinciaDestinatario() + ")", FontFactory.getFont(FontFactory.COURIER, fntSize));
            } else {   // Formattazione dell'indirizzo per le fatture italiane
                indirizzoDestinatarioSection2 = new Phrase(commonUtility.gestioneCaratteriSpeciali(fattura.getComuneDestinatario()) + " " + (fattura.getCapDestinatario() != null ? fattura.getCapDestinatario() : "") + " (" + fattura.getProvinciaDestinatario() + ")", FontFactory.getFont(FontFactory.COURIER, fntSize));
            }

        } else {
            if (nazionalitaFatturaCorrente.equals("E")) {    // Formattazione dell'indirizzo per le fatture estere
                indirizzoDestinatarioSection2 = new Phrase(commonUtility.gestioneCaratteriSpeciali((fattura.getCapDestinatario() != null ? fattura.getCapDestinatario() : "") + " " + fattura.getComuneDestinatario()), FontFactory.getFont(FontFactory.COURIER, fntSize));
            } else {      // Formattazione dell'indirizzo per le fatture italiane
                indirizzoDestinatarioSection2 = new Phrase(commonUtility.gestioneCaratteriSpeciali(fattura.getComuneDestinatario()) + " " + (fattura.getCapDestinatario() != null ? fattura.getCapDestinatario() : ""), FontFactory.getFont(FontFactory.COURIER, fntSize));
            }
        }

        //Phrase pIvaDestinatario = new Phrase("P.I.: " + fattura.getPartivaIVADestinatario(), FontFactory.getFont(FontFactory.COURIER, fntSize));
        Phrase codiceNazione = new Phrase(fattura.getCodiceNazione(), FontFactory.getFont(FontFactory.COURIER, fntSize));

        cell1Destinatario.addElement(spettabile);
        cell1Destinatario.addElement(destinatario);
        cell1Destinatario.setBorderColor(BaseColor.WHITE);
        cell1Destinatario.addElement(indirizzoDestinatarioSection1);
        cell1Destinatario.addElement(indirizzoDestinatarioSection2);

        if (fattura.getCodiceNazione() != null) {
            cell1Destinatario.addElement(codiceNazione);
        }

        cell1Destinatario.setRotation(90);
        cell1Destinatario.setFixedHeight(altezza_dest);

        destinatarioTable.addCell(cell1Destinatario);

        float shift_x1_dest = com.itextpdf.text.Utilities.millimetersToPoints(47);     // 4,7 cm
        float shift_y1_dest = altezza_dest + com.itextpdf.text.Utilities.millimetersToPoints(171);     // 17,1 cm

        destinatarioTable.writeSelectedRows(0, -1, shift_x1_dest, shift_y1_dest, canvasDestinatario);


        // TEST:
        document.newPage();
        Phrase testPhrase = new Phrase("TEST");
        document.add(testPhrase);


        // STEP 5:
        document.close();

    } catch (DocumentException ex) {
        ex.printStackTrace();
    }


    return currentFatturaHeaderPdf;


}

这很长,但有趣的部分是在这个方法结束时,事实上,在关闭我的文档之前,我尝试通过这段代码添加一个新页面:

// TEST:
document.newPage();
Phrase testPhrase = new Phrase("TEST");
document.add(testPhrase);

正如您所看到的,我尝试在文档中创建新页面,但它不会出现在我最终生成的PDF中。

为什么呢?我错过了什么?如何修复并添加此页面?

谢谢

0 个答案:

没有答案