iText5:创建PDF时,标题内容将出现两次

时间:2017-02-13 11:24:58

标签: java pdf itext pdf-generation

我正在尝试生成PDF。所有其他的东西都运行正常,唯一的问题是当我在标题中添加一个表时它会被添加两次。

以下是我得到的PDF截图: -

enter image description here

背景,我可以稍后修复它,帮助我修复履行部分。

这是我的GeneratePDFUtil课程: -

package genetatePDF;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePDFUtil {

    private static float topRightTextSpace = -2f;
    private static float signatureWidth = 70f;
    private static float signatureHeight = 20f;

    static String LogoUrl = "https://www.stockfreeimages.com/Company-Logo-Ending-Y-thumb4456521.jpg";
    private static String SIGNATURE = "Signature";

    public static void main(String[] args) throws MalformedURLException, DocumentException, IOException {

        generatePrescriptionPdf();
    }

    public static void generatePrescriptionPdf() throws DocumentException, MalformedURLException, IOException {

        Document document = new Document(PageSize.A4);
        document.setMargins(36, 36, 6, 36);

        String name = "try_" + System.currentTimeMillis() + ".pdf";
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(name));

        Rectangle rect = new Rectangle(36, 0, 550, 780);
        writer.setBoxSize("art", rect);
        HeaderFooterPageEvents event = new HeaderFooterPageEvents();
        event.setHeader(LogoUrl);
        writer.setPageEvent(event);

        document.open();
        document.add(spaceTable(8));

        document.add(topSpaceTable(4));
        float[] threeColumnWidth = { 1, 1, 1 };
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100);
        table.setWidths(threeColumnWidth);
        table.addCell(leftBorderColumn("Id: ", "12345"));
        table.addCell(noBorderColumn("Date: ", "13/02/2017"));
        table.addCell(rightBorderColumn("ID: ", "Abc35"));
        document.add(table);

        table.deleteLastRow();
        table.addCell(leftBorderColumn("Name: ", "Manshavi"));
        table.addCell(noBorderColumn("Age: ", "23"));
        table.addCell(rightBorderColumn("Gender: ", "Male"));
        document.add(table);

        table.deleteLastRow();
        table.addCell(leftBorderColumn("Name: ", "Kumar"));
        table.addCell(noBorderColumn("Number: ", "1234"));
        table.addCell(rightBorderColumn("Specialization: ", "Java"));
        document.add(table);

        for (int i = 0; i < 5; i++) {
            PdfPCell cell1 = new PdfPCell(new Paragraph("cell 1"));
            cell1.setBorderColor(BaseColor.BLUE);
            cell1.setPaddingLeft(10);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);

            PdfPCell cell2 = new PdfPCell(new Paragraph("cell 2"));
            cell2.setBorderColor(BaseColor.GREEN);
            cell2.setPaddingLeft(50);
            cell2.setHorizontalAlignment(Element.ALIGN_BASELINE);
            cell2.setVerticalAlignment(Element.ALIGN_RIGHT);

            PdfPCell cell3 = new PdfPCell(new Paragraph("cell 3"));
            cell3.setBorderColor(BaseColor.GRAY);
            cell3.setPaddingLeft(10);
            cell3.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
            cell3.setVerticalAlignment(Element.ALIGN_JUSTIFIED_ALL);

            table.deleteLastRow();
            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            document.add(table);
        }

        document.add(spaceTable(8));

        Paragraph paragraph = addParagraphContent(SIGNATURE, Font.NORMAL, topRightTextSpace);
        Image signImage = Image.getInstance(
                "https://thumbs.dreamstime.com/z/selfie-dogs-couple-dog-taking-together-smartphone-40248448.jpg");
        signImage.scaleToFit(signatureWidth, signatureHeight);
        signImage.setAlignment(Element.ALIGN_RIGHT);
        document.add(signImage);

        paragraph.setSpacingAfter(2);
        paragraph.setAlignment(Element.ALIGN_RIGHT);
        document.add(paragraph);

        paragraph = addParagraphContent("Name ", Font.BOLD, topRightTextSpace);
        paragraph.setAlignment(Element.ALIGN_RIGHT);
        document.add(paragraph);

        document.close();
        writer.close();

    }

    private static PdfPTable spaceTable(int height) {

        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(100);
        PdfPCell cell = new PdfPCell();
        cell.setFixedHeight(height);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        return table;
    }

    private static PdfPTable topSpaceTable(int height) {

        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(100);
        PdfPCell cell = new PdfPCell();
        cell.setFixedHeight(height);
        cell.setBorder(Rectangle.LEFT | Rectangle.TOP | Rectangle.RIGHT);
        table.addCell(cell);
        return table;
    }

    // alphabetType is like BOLD or NORMAL type
    private static Paragraph addParagraphContent(String content, int alphabetType, float spacingAfter) {

        Paragraph paragraph = new Paragraph(content, new Font(Font.FontFamily.TIMES_ROMAN, 10, alphabetType));
        paragraph.setSpacingAfter(spacingAfter);
        return paragraph;
    }

    private static PdfPCell leftBorderColumn(String boldContent, String normalContent) {

        Paragraph resultField = new Paragraph(boldContent, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
        resultField.setFont(new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL));
        resultField.add(normalContent);
        PdfPCell cell = new PdfPCell(resultField);
        cell.setBorder(Rectangle.LEFT);
        cell.setPaddingLeft(10);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
        return cell;
    }

    private static PdfPCell rightBorderColumn(String boldContent, String normalContent) {

        Paragraph resultField = new Paragraph(boldContent, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
        resultField.setFont(new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL));
        resultField.add(normalContent);
        PdfPCell cell = new PdfPCell(resultField);
        cell.setBorder(Rectangle.RIGHT);
        cell.setPaddingLeft(10);
        cell.setHorizontalAlignment(Element.ALIGN_MIDDLE);
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
        return cell;
    }

    private static PdfPCell noBorderColumn(String boldContent, String normalContent) {

        Paragraph resultField = new Paragraph(boldContent, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
        resultField.setFont(new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL));
        resultField.add(normalContent);
        PdfPCell cell = new PdfPCell(resultField);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setPaddingLeft(10);
        cell.setHorizontalAlignment(Element.ALIGN_MIDDLE);
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
        return cell;
    }
}

class HeaderFooterPageEvents extends PdfPageEventHelper {

    private static String FULFILLED_BY = "Fulfilled By";
    protected String imageUrl;

    public void setHeader(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public void onStartPage(PdfWriter writer, Document document) {
        Rectangle rect = writer.getBoxSize("art");
        try {
            Image image = Image.getInstance(imageUrl);
            image.scaleToFit(150, 300);
            image.setAbsolutePosition(rect.getLeft(), rect.getTop() - 20);
            image.setSpacingAfter(10);
            image.setSpacingBefore(10);
            document.add(image);
            PdfPTable table = new PdfPTable(1);
            table.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.setWidthPercentage(100);
            table.setTotalWidth(180f);
            PdfPCell cell = new PdfPCell(new Phrase(FULFILLED_BY, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("Shope Name",new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("Address1",new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("Address2, City Name",new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("Cont. no.:1234454",new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("Email:manshavikumar1993@gmail.com",new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL)));
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);

            table.writeSelectedRows(0, -1, rect.getRight() - table.getTotalWidth(), document.top(), writer.getDirectContent());

            document.add(table);
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试删除位于HeaderFooterPageEvents类中的以下代码行之一(取决于您的要求):

如果要将表格添加到您的身体,请删除以下行:

table.writeSelectedRows(0, -1, rect.getRight() - table.getTotalWidth(), document.top(), writer.getDirectContent());

如果要将表格添加到页面标题中,则应删除以下行:

document.add(table)

但是,可能需要一些格式化。