Yep, that's true sorry 'bout that.
The first method adds PdfPTables (with cells) to a list, the 2nd is a
convenience method to create the main and dummy table.
The last adds the common params to the PdfPCells.
All I know is when I stopped using tables as element in a table that my
problem went away completely. Eventhough I use the same method to create my
main PdfPTable as my subTables. Which is most importantly the width.
I need to build a sort of ladder that holds job openings which is supposed
to be printed in a newspaper.
The ladder starts with an image, then the type of opening and then a
subtype.
Here's the complete code. There's no value object of course, that's because
there's custom functionality which extracts the data and adds it into an
object. It doesn't sound very custom, but it is. Just expect one header,
several subheaders and lots of elements. All in String format.
I opened the history in IntelliJ so the whole class should be something like
this (give or take a few attempts)
Oh and I realize I might be using an older iText version, but that's beyond
my control.
public class BanenLadder implements Advertisement
{
private static Log LOG = LogFactory.getLog(BanenLadder.class);
private float WIDTH = Convert.mm2pt(85f); //TODO juiste (exact) pagina
afmetingen
private float HEIGHT = Convert.mm2pt(382f);
private IModuleItem moduleItem;
private float headerCellHeight = Convert.mm2pt(8f);
private float subHeaderCellHeight = Convert.mm2pt(7.5f);
private float bodyCellHeight = Convert.mm2pt(20f);
private Color colour;
/*
85x94 R22
85x142 R23
85x190 R24
85x238 R25
85x382 R28
*/
private final float[] pieceLengtes = new
float[]{Convert.mm2pt(382f),Convert.mm2pt(238f),Convert.mm2pt(190f),Convert.mm2pt(142f),Convert.mm2pt(94f)};
//in mm
private PdfContentByte canvas;
private float som;
// private PdfPTable table = new PdfPTable(1); //elke table heeft 1 rij.
nieuwe table per main header
private Map<String, List<PdfPTable>> elementen;
public void createPdf(IModuleItem pModuleItem)
{
try {
String type = pModuleItem.getAdNo();
Image[] images = getImages(type);
elementen = new HashMap<String, List<PdfPTable>>();
Document document = new Document(new Rectangle(WIDTH, HEIGHT));
moduleItem = pModuleItem;
String testPath = "c:/tmp/test.pdf";
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(new java.io.File(testPath)));
document.setMargins(0f,0f,0f,0f); //eerst, daarna doc openen
document.open();
canvas = writer.getDirectContent();
som = 0f;
String writeHeader = "";
for(int h = 0; true; h++){
String headerSuf = ""+h;
String header =
pModuleItem.getITextField("header."+headerSuf);
if(header != null) {
writeHeader = header;
for(int sh = 0; true; sh++){
String subHeaderSuf = headerSuf+"."+sh;
String subHeader =
pModuleItem.getITextField("subheader."+subHeaderSuf);
if(subHeader != null){
List<PdfPTable> cells = new
ArrayList<PdfPTable>();
elementen.put(subHeader,cells);
for(int t = 0; true; t++){
String titelSuf = subHeaderSuf+"."+t;
String titel =
pModuleItem.getITextField("titel."+titelSuf);
if(titel != null){
createTextBody(subHeader,titel,titelSuf);
}else {
break;
}
}
}else {
break;
}
}
}else {
PdfPTable mainTable = createNewTable();
PdfPTable headerImageTable = createHeader(images[0]);
PdfPCell footerImageCell = createFooter(images[1]);
mainTable.addCell(headerImageTable);
som += headerImageTable.getTotalHeight();
Font titelFont = new
Font(Font.TIMES_ROMAN,12,Font.NORMAL,Color.WHITE);
Font subTitFont = new
Font(Font.TIMES_ROMAN,11,Font.NORMAL,colour);
PdfPCell headerCell = new PdfPCell(new Phrase(new
Chunk(writeHeader,titelFont)));
headerCell.setBackgroundColor(colour);
applyDefaultCellProps(headerCell);
som+=headerCell.getHeight();
//todo eerste check voor lengte
mainTable.addCell(headerCell);
for(String subHeader:elementen.keySet()){
PdfPCell subCell = new PdfPCell(new Phrase(new
Chunk(subHeader,subTitFont))); //todo zie chapter 10 voor streep in cel
applyDefaultCellProps(subCell);
mainTable.addCell(subCell);
//todo tweede check voor max lengte
for(PdfPTable bodyTable:elementen.get(subHeader)){
mainTable.addCell(bodyTable);
}
}
// table.addCell(footerImageCell);
for(float f:pieceLengtes){ //todo kleinste nr grootste
in declaratie
PdfPTable tempTable = createNewTable();
tempTable.addCell(footerImageCell);
float footerHeight = footerImageCell.getHeight();
float result = f - (som+footerHeight);
if(result >= 0){
footerImageCell.setPaddingTop(result);
mainTable.addCell(footerImageCell);
break;
}
}
document.add(mainTable);
//todo indien niet alles op 1 blad, meerdere documenten
maken
break; //einde document ook
}
}
document.close();
writer.close();
LOG.debug("body: "+pModuleItem.getITextField("titel.0"));
}catch (DocumentException de){
LOG.error(de);
de.printStackTrace();
} catch (IOException e) {
LOG.error(e);
e.printStackTrace(); //To change body of catch statement use
File | Settings | File Templates.
}
}
public PdfPTable createHeader(Image header) {
PdfPCell cell = new PdfPCell(header);
cell.setBorder(0);
cell.setPadding(0);
cell.setTop(0);
cell.setBottom(0);
PdfPTable table = createNewTable();
table.addCell(cell);
return table;
}
public PdfPCell createFooter(Image footer){
PdfPCell cell = new PdfPCell(footer);
applyDefaultCellProps(cell);
return cell;
}
public float createSubHeader(String subHeader) throws IOException,
DocumentException {
// return addText(subHeader,11,Font.NORMAL,HEIGHT - som);
return 0;
}
public float createTextBody(String key,String titel,String suffix)
throws IOException, DocumentException {
Font titelFont = new Font(Font.TIMES_ROMAN,8,Font.BOLD);
Font main = new Font(Font.TIMES_ROMAN,6,Font.BOLD);
Font bodyFont = new Font(Font.TIMES_ROMAN,6,Font.ITALIC);
Font mainBold = new Font(Font.TIMES_ROMAN,6,Font.BOLD);
PdfPCell cell = new PdfPCell();
applyDefaultCellProps(cell);
Phrase phrase = new Phrase(new Chunk(titel,titelFont));
cell.addElement(new Paragraph(phrase));
for(int i=1; true ; i++){
String regel = "lijn"+i+"."+suffix;
String lijn = moduleItem.getITextField(regel);
if(lijn != null){
cell.addElement(new Paragraph(new Phrase(new
Chunk(lijn,main))));
}else {
break;
}
}
String body = moduleItem.getITextField("body."+suffix);
cell.addElement(new Paragraph(new Phrase(new
Chunk(body,bodyFont))));
String foot = moduleItem.getITextField("footer."+suffix); //todo
parse want "862DC" en "vkbanen.nl" in bold de rest van de regel niet
cell.addElement(new Paragraph(new Phrase(new
Chunk(foot,mainBold))));
List<PdfPTable> list = elementen.get(key);
PdfPTable table = createNewTable();
table.addCell(cell);
list.add(table);
som+=table.getTotalHeight();
return 0;
}
/**
* Gaat ook de kleur zetten en de header en footer ophalen
* @param type 0 = header, 1 = footer
* @return
* @throws IOException
* @throws BadElementException
*/
private Image[] getImages(String type) throws IOException,
BadElementException {
String packageName = null;
for(BanenResources res:BanenResources.values()){
if(res.getName().equals(type)){
packageName = res.getResource();
this.colour = res.getColor();
}
}
if(packageName == null) throw new IOException("Ongekend type in
XML");
InputStream footerStream =
getClass().getResourceAsStream(packageName+"/footer_tab.jpg");
InputStream headerStream =
getClass().getResourceAsStream(packageName+"/header_tab.jpg");
byte[] header = new byte[headerStream.available()];
headerStream.read(header);
byte[] footer = new byte[footerStream.available()];
footerStream.read(footer);
Image headerJpg = new Jpeg(header);
Image footerJpg = new Jpeg(footer);
return new Image[]{headerJpg,footerJpg};
}
private PdfPTable createNewTable(){
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(WIDTH);
table.setLockedWidth(true);
return table;
}
private void applyDefaultCellProps(PdfPCell cell){
cell.setBorder(PdfPCell.NO_BORDER);
cell.enableBorderSide(Rectangle.LEFT);
cell.enableBorderSide(Rectangle.RIGHT);
cell.setPadding(5);
}
public enum BanenResources {
DIVERSEN("diversen",Color.RED,"diversen"),
GEZONDHEID("gezondheid",Color.CYAN,"Gezondheidszorg & Welzijn"),
ICT("ICT",Color.GREEN,"Commercieel & ICT"),
MANAGEMENT("management",Color.ORANGE,"Management & Staf"),
ONDWETTECH("OndwWetensTech",Color.MAGENTA,"Onderwijs, Wetenschap &
Techniek");
private String resource; //de naam van de resource package
private Color color; //de geassocieerde kleur
private String name; //zoals in de xml
BanenResources(String resource,Color color,String name){
this.resource = resource;
this.color = color;
this.name = name;
}
public String getResource() {
return resource;
}
public Color getColor() {
return color;
}
public String getName() {
return name;
}
}
}
--
View this message in context:
http://itext-general.2136553.n4.nabble.com/PdfPTable-in-PdfPTable-border-troubles-tp2239893p2240099.html
Sent from the iText - General mailing list archive at Nabble.com.
------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/