标头被IText PDF中的主要内容覆盖了

时间:2019-09-03 10:38:01

标签: java itext

我正在使用iText 2.1.7,当表移到新页面时某些表的表头重叠

我试图改变利润率,但是没有运气。

我希望它会自动移到标题下方,但会与之重叠

这是一些图片:

它仅在某些页面上发生,由于空间限制,表格会自动移至新页面

Document document = new Document(PageSize.A4,50, 45, 50, 60);

ByteArrayOutputStream stream  = new ByteArrayOutputStream();
PdfWriter writer =  PdfWriter.getInstance(document, stream);

//  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/sukant.singhal/Desktop/ApplicationFormPdf/"+fileName));

BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", false);
Font headerFont = new Font(Font.TIMES_ROMAN, 14, Font.NORMAL);
Font abc = new Font(Font.TIMES_ROMAN, 6, Font.NORMAL);
abc.setColor(255, 0, 0);
Font headerFont1 = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);
BaseFont base = BaseFont.createFont("/arial.ttf", BaseFont.WINANSI, false);
Font footFont = new Font(base, 8);
Font headerBold =  new Font(base,7,Font.BOLD);
Font headerBoldBig =  new Font(base,10,Font.BOLD);
// headers and footers must be added before the document is opened

HeaderFooter footer = new HeaderFooter(new Phrase("App ID : "+id+" | Page No. : ", footFont), true);
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(Element.ALIGN_RIGHT);
document.setFooter(footer);

document.open();

PdfContentByte cb = writer.getDirectContent();
Phrase vHeaderphrase = new Phrase();
PdfPTable table = headerandborder(document,headerFont,headerFont1,writer,cb);
vHeaderphrase.add(table);
HeaderFooter head = new HeaderFooter(vHeaderphrase, false);
head.setBorder(Rectangle.NO_BORDER);
document.setHeader(head);

//Adding First table

List<ManagementDetails> managementDetails =  promoterdirectorDTO.getManagementDetails();
if(managementDetails!=null && managementDetails.size()>0) {
for(ManagementDetails a :managementDetails)
{
    document.add(new Paragraph("\n\n"));
    managementDetailsTable(footFont, document,lkpmap,a, clipServiceManager);
}

使用辅助方法

public static void managementDetailsTable(Font footFont,Document document, Map<Long, String> lkpmap, ManagementDetails a, ClipServiceManager clipServiceManager) {
    try {
        document.add(new Paragraph("\n"));
        PdfPTable  ManagementDetailTable = new PdfPTable(5);
        ManagementDetailTable.setWidthPercentage(95);
        ManagementDetailTable.setKeepTogether(true);

        float[] columnwidth = {1,1,1,1,1};
        ManagementDetailTable.setWidths(columnwidth);

        //Header 
        PdfPCell sectionHeaderCell = new PdfPCell(new Phrase("Management Details\r\n" + 
                " header",footFont));
        sectionHeaderCell.setBackgroundColor(new Color(91,155,253));
        sectionHeaderCell.setHorizontalAlignment(Element.ALIGN_LEFT);           
        sectionHeaderCell.setPadding(3);
        sectionHeaderCell.setBorderWidth(.5f);      
        sectionHeaderCell.setColspan(5);
        ManagementDetailTable.addCell(sectionHeaderCell);
        ManagementDetailTable.completeRow();

        //Row 1
        PdfPCell pdfcell;
        pdfcell = new PdfPCell(new Phrase("Type",footFont));
        pdfcell.setColspan(1);
        String s= null;
        ManagementDetailTable.addCell(pdfcell);
        /*if(a.getType()!=null && !a.getType().isEmpty()) {
             s= lkpmap.get(Long.parseLong(a.getType()));
        }*/

        pdfcell = new PdfPCell(new Phrase(nullCheck(a.getType()),footFont));
        s=null;
        pdfcell.setColspan(4);
        ManagementDetailTable.addCell(pdfcell);
        ManagementDetailTable.completeRow();

        //Row 2
        PdfPCell pdfcell1;
        pdfcell1 =  new PdfPCell(new Phrase("Title",footFont));
        ManagementDetailTable.addCell(pdfcell1);
        if(a.getTitle()!=null && !a.getTitle().isEmpty()) {
             s= lkpmap.get(Long.parseLong(a.getTitle()));
        }

        pdfcell1 =  new PdfPCell(new Phrase(nullCheck(s),footFont));
        s=null;
        ManagementDetailTable.addCell(pdfcell1);
        pdfcell1 =  new PdfPCell(new Phrase("First Name",footFont));
        ManagementDetailTable.addCell(pdfcell1);
        pdfcell1 =  new PdfPCell(new Phrase(nullCheck(a.getFirstName()),footFont));
        ManagementDetailTable.addCell(pdfcell1);

        /*Image image;
        InputStream is = a.getImage().getInputStream();
        byte[] bytes = IOUtils.toByteArray(is);
        image = Image.getInstance(bytes);
        */

        pdfcell1 =  new PdfPCell(new Phrase("Affix recent Photo\r\n" + 
                "Cross sign",footFont));
        pdfcell1.setRowspan(5);
        pdfcell1.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfcell1.setVerticalAlignment(Element.ALIGN_CENTER);
        ManagementDetailTable.addCell(pdfcell1);
        ManagementDetailTable.completeRow();

        //Row 3 
        PdfPCell pdfcell2;
        pdfcell2 =  new PdfPCell(new Phrase("Middle Name",footFont));
        ManagementDetailTable.addCell(pdfcell2);
        pdfcell2 =  new PdfPCell(new Phrase(nullCheck(a.getMiddleName()),footFont));
        ManagementDetailTable.addCell(pdfcell2);
        pdfcell2 =  new PdfPCell(new Phrase("Last Name",footFont));
        ManagementDetailTable.addCell(pdfcell2);
        pdfcell2 =  new PdfPCell(new Phrase(nullCheck(a.getLastName()),footFont));
        ManagementDetailTable.addCell(pdfcell2);

        ManagementDetailTable.completeRow();

        //Row 4
        PdfPCell pdfcell3;
        pdfcell3 = new PdfPCell(new Phrase("Pan No",footFont));
        pdfcell3.setColspan(1);
        ManagementDetailTable.addCell(pdfcell3);
        pdfcell3 = new PdfPCell(new Phrase(nullCheck(a.getPanNo()),footFont));
        pdfcell3.setColspan(3);
        ManagementDetailTable.addCell(pdfcell3);
        ManagementDetailTable.completeRow();

        //Row 5
        PdfPCell pdfcell4;
        pdfcell4 = new PdfPCell(new Phrase("Date of Birth",footFont));
        pdfcell4.setColspan(1);
        ManagementDetailTable.addCell(pdfcell4);
        pdfcell4 = new PdfPCell(new Phrase(nullCheck(a.getDob()),footFont));
        pdfcell4.setColspan(3);
        ManagementDetailTable.addCell(pdfcell4);
        ManagementDetailTable.completeRow();

        //Row 6
        PdfPCell pdfcell5;
        pdfcell5 = new PdfPCell(new Phrase("Din / DPIN No. (if applicable)",footFont));
        pdfcell5.setColspan(1);
        ManagementDetailTable.addCell(pdfcell5);
        pdfcell5 = new PdfPCell(new Phrase(nullCheck(a.getDin()),footFont));
        pdfcell5.setColspan(3);
        ManagementDetailTable.addCell(pdfcell5);
        ManagementDetailTable.completeRow();

        //Row 7
        PdfPCell pdfcell6;
        pdfcell6 = new PdfPCell(new Phrase("Father’s / Spouse Name",footFont));
        pdfcell6.setColspan(1);
        ManagementDetailTable.addCell(pdfcell6);
        pdfcell6 = new PdfPCell(new Phrase(nullCheck(a.getFatherOrSpouse()),footFont));
        pdfcell6.setColspan(4);
        ManagementDetailTable.addCell(pdfcell6);
        ManagementDetailTable.completeRow();

        //Row 8
        PdfPCell pdfcell7;
        pdfcell7 = new PdfPCell(new Phrase("Email ID",footFont));
        pdfcell7.setColspan(1);
        ManagementDetailTable.addCell(pdfcell7);
        pdfcell7 = new PdfPCell(new Phrase(nullCheck(a.getEmail()),footFont));
        pdfcell7.setColspan(4);
        ManagementDetailTable.addCell(pdfcell7);
        ManagementDetailTable.completeRow();

        //Row 9
        PdfPCell pdfcell8;
        pdfcell8 = new PdfPCell(new Phrase("Residential Status",footFont));
        pdfcell8.setColspan(1);
        ManagementDetailTable.addCell(pdfcell8);
        if(a.getResidentialStatus()!=null && !a.getResidentialStatus().isEmpty()) {
             s= lkpmap.get(Long.parseLong(a.getResidentialStatus()));
        }

        pdfcell8 = new PdfPCell(new Phrase(nullCheck(s),footFont));
        s=null;
        pdfcell8.setColspan(4);
        ManagementDetailTable.addCell(pdfcell8);
        ManagementDetailTable.completeRow();

        //Row 10
        PdfPCell pdfcell9;
        pdfcell9 = new PdfPCell(new Phrase("Nationality",footFont));
        pdfcell9.setColspan(1);
        ManagementDetailTable.addCell(pdfcell9);
        if(a.getNationality()!=null && !a.getNationality().isEmpty()) {
             s= lkpmap.get(Long.parseLong(a.getNationality()));
        }

        pdfcell9 = new PdfPCell(new Phrase(nullCheck(s),footFont));
        s=null;
        pdfcell9.setColspan(4);
        ManagementDetailTable.addCell(pdfcell9);
        ManagementDetailTable.completeRow();

        //Row 11
        PdfPCell pdfcell10;
        pdfcell10 =  new PdfPCell(new Phrase("Telephone No",footFont));
        pdfcell10.setColspan(1);
        ManagementDetailTable.addCell(pdfcell10);
        pdfcell10 =  new PdfPCell(new Phrase(nullCheck(a.getTelephoneNo()),footFont));
        ManagementDetailTable.addCell(pdfcell10);
        pdfcell10 =  new PdfPCell(new Phrase("Mobile No",footFont));
        ManagementDetailTable.addCell(pdfcell10);
        pdfcell10 =  new PdfPCell(new Phrase(nullCheck(a.getMobileNo()),footFont));
        pdfcell10.setColspan(2);
        ManagementDetailTable.addCell(pdfcell10);
        ManagementDetailTable.completeRow();

        //Row 12
        PdfPCell pdfcell11;
        pdfcell11 =  new PdfPCell(new Phrase("Address for Correspondence",footFont));
        pdfcell11.setColspan(1);
        pdfcell11.setRowspan(3);
        ManagementDetailTable.addCell(pdfcell11);
        pdfcell11 =  new PdfPCell(new Phrase(nullCheck(a.getAddressLine1()),footFont));
        pdfcell11.setColspan(2);
        ManagementDetailTable.addCell(pdfcell11);
        pdfcell11 =  new PdfPCell(new Phrase("Zip Code",footFont));
        ManagementDetailTable.addCell(pdfcell11);
        pdfcell11 =  new PdfPCell(new Phrase(nullCheck(a.getZipCode()),footFont));
        ManagementDetailTable.addCell(pdfcell11);
        ManagementDetailTable.completeRow();

        //Row 13
        pdfcell11 =  new PdfPCell(new Phrase(nullCheck(a.getAddressLine2()),footFont));
        pdfcell11.setColspan(2);
        ManagementDetailTable.addCell(pdfcell11);
        pdfcell11 =  new PdfPCell(new Phrase("City",footFont));
        ManagementDetailTable.addCell(pdfcell11);
        City c= new City();
        c.setState(new State());
        Country country = new Country();
        if(a.getCity()!=null && !a.getCity().isEmpty()) {
            c =clipServiceManager.getCityService().getCityById(Short.valueOf(a.getCity()));
        }
        pdfcell11 =  new PdfPCell(new Phrase(nullCheck(c.getCityName()),footFont));

        ManagementDetailTable.addCell(pdfcell11);
        ManagementDetailTable.completeRow();

        //Row 14
        pdfcell11 =  new PdfPCell(new Phrase(" ",footFont));
        pdfcell11.setColspan(2);
        ManagementDetailTable.addCell(pdfcell11);
        pdfcell11 =  new PdfPCell(new Phrase("District",footFont));
        ManagementDetailTable.addCell(pdfcell11);
        pdfcell11 =  new PdfPCell(new Phrase(nullCheck(a.getDistrict()),footFont));
        ManagementDetailTable.addCell(pdfcell11);
        ManagementDetailTable.completeRow();

        //Row 15
        ManagementDetailTable.addCell(new PdfPCell(new Phrase("State",footFont)));
        ManagementDetailTable.addCell(new PdfPCell(new Phrase(nullCheck(c.getState().getStateName()),footFont)));
        ManagementDetailTable.addCell(new PdfPCell(new Phrase("Country",footFont)));
        if(a.getState()!=null && c.getState().getCountry()!=null) {
        country= clipServiceManager.getCountryService().getCountryById(Short.valueOf(c.getState().getCountry().getCountryId().toString()));
        }
        PdfPCell  cell = new PdfPCell(new Phrase(nullCheck(country.getCountryName()),footFont));
        cell.setColspan(2);
        c= new City();
        c.setState(new State());
        country = new Country();

        ManagementDetailTable.addCell(cell);
        //Row 16
        if(a.getAddressProof()!=null && !a.getAddressProof().isEmpty()) {
             s= lkpmap.get(Long.parseLong(a.getAddressProof()));
        }
        ManagementDetailTable.addCell(new PdfPCell(new Phrase("Address Proof Submitted*",footFont)));
        ManagementDetailTable.addCell(new PdfPCell(new Phrase(nullCheck(s),footFont)));
        s= null;
        ManagementDetailTable.addCell(new PdfPCell(new Phrase("ID Proof Submitted*",footFont)));
        cell = new PdfPCell(new Phrase(nullCheck(a.getIdProof()),footFont));
        cell.setColspan(2);
        ManagementDetailTable.addCell(cell);

        //Row 17
        if(a.getRoleAssignedbyTreds()!=null && !a.getRoleAssignedbyTreds().isEmpty()) {
             s= lkpmap.get(Long.parseLong(a.getRoleAssignedbyTreds()));
        }
        ManagementDetailTable.addCell(new PdfPCell(new Phrase("Role Assigned under TReDS",footFont)));
        ManagementDetailTable.addCell(new PdfPCell(new Phrase(nullCheck(s),footFont)));
        s= null;
        ManagementDetailTable.addCell(new PdfPCell(new Phrase("Date of Authorisation for TReDS",footFont)));
        cell = new PdfPCell(new Phrase(nullCheck(a.getTredsAuthorizationDate()),footFont));
        cell.setColspan(2);
        ManagementDetailTable.addCell(cell);

        //Row 18
        if(a.getCategory()!=null && !a.getCategory().isEmpty()) {
             s= lkpmap.get(Long.parseLong(a.getCategory()));
        }
        ManagementDetailTable.addCell(new PdfPCell(new Phrase("Category",footFont)));
        ManagementDetailTable.addCell(new PdfPCell(new Phrase(nullCheck(s),footFont)));
        s=null;
        if(a.getWomenEntrepreneur()!=null && !a.getWomenEntrepreneur().isEmpty()) {
             s= lkpmap.get(Long.parseLong(a.getWomenEntrepreneur()));
        }
        ManagementDetailTable.addCell(new PdfPCell(new Phrase("Women Entrepreneur",footFont)));
        cell = new PdfPCell(new Phrase(nullCheck(s),footFont));
        cell.setColspan(2);
        ManagementDetailTable.addCell(cell);

        s= null;

        ManagementDetailTable.setKeepTogether(true);
        document.add(ManagementDetailTable);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static PdfPTable headerandborder(Document document, Font headerFont,Font headerFont1, PdfWriter writer,PdfContentByte cb ) {
    try {
        PdfPTable fileHeader = new PdfPTable(2);
        fileHeader.getDefaultCell().setBorderColor(Color.WHITE);
        fileHeader.setWidthPercentage(100);
        float[] fileHeaderColumnWidth = { 10, 60 };
        fileHeader.setWidths(fileHeaderColumnWidth);

        Paragraph footerPara = new Paragraph("cba ", headerFont);
        footerPara.setAlignment(Element.ALIGN_RIGHT); //ALIGN Right

        Paragraph footerHeadOfficeAdd = new Paragraph("APPLICATION FORM FOR SELLERS", headerFont1); // Phrase 1
        footerHeadOfficeAdd.setAlignment(Element.ALIGN_MIDDLE);

        Paragraph headerPara = new Paragraph();
        headerPara.add( footerPara );  // Company Name
        headerPara.add( footerHeadOfficeAdd ); // Form Title

        PdfPCell fileHeaderCell = new PdfPCell(new Phrase("REdskvbkjds   ", headerFont));
        fileHeaderCell.setBackgroundColor(Color.WHITE);
        fileHeaderCell.setHorizontalAlignment(Element.ALIGN_LEFT);
        fileHeaderCell.setVerticalAlignment(Element.ALIGN_TOP);
        //fileHeaderCell.setPadding(20);
        fileHeaderCell.setPaddingLeft(40);
        fileHeaderCell.setBorderColor(Color.WHITE);
        fileHeaderCell.addElement(headerPara);

        Image image;
        InputStream is = new ClassPathResource("/logo.png").getInputStream();
        byte[] bytes = IOUtils.toByteArray(is);
        image = Image.getInstance(bytes);

        fileHeader.addCell(image);
        fileHeader.addCell(fileHeaderCell);
        document.add(fileHeader);

        return fileHeader;

    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

0 个答案:

没有答案