Hello,
I have got a problem with using PdfContentByte to add lines and ColumnTexts on
multiple pages.
I am trying to display the content of my program on a PDF. But I need some
Images , lines and text(adress for example) on top and bottom of each page.
So I created 2 methods.
The first adds the images with RandomAccessFile, the lines and the text with
the PdfContentByte.
The other inserts the content to the page, if the content has too many lines, I
create a new Page, start the first method again and after I insert the rest of
the content.
On the first page all looks well, but on all following pages the lines and
ColumnTextŽs doesnŽt exist.
Images and content are placed like they should.
I added the 2 methods to my mail
I hope you can help me.
____________________________________________________________________
Psssst! Schon vom neuen WEB.DE MultiMessenger gehört?
Der kann`s mit allen: http://www.produkte.web.de/messenger/?did=3123
public String createTemplate(Document document, int index, int n)
{
System.out.println(n);
String vorgang = ((JTextField)Felder.get(index+1)).getText();
String dateiname = null;
try {
PdfWriter writer;
if(new File(vorgang+".pdf").exists())
{
int next = getNextdateiname(vorgang);
if(next != 0)
{
writer = PdfWriter.getInstance(document, new
FileOutputStream(vorgang+next+".pdf"));
dateiname = vorgang+next+".pdf";
}
else{
return dateiname;
}
}
else{
writer = PdfWriter.getInstance(document, new
FileOutputStream(vorgang+".pdf"));
dateiname = vorgang+".pdf";
}
document.open();
RandomAccessFile punkte = new RandomAccessFile("Punkte.JPG", "r");
int size = (int)punkte.length();
byte imext2[] = new byte[size];
punkte.readFully(imext2);
punkte.close();
Image img2 = Image.getInstance(imext2);
img2.setAbsolutePosition(document.left(), 744);
img2.scaleAbsolute(74, 16);
document.add(img2);
RandomAccessFile logo = new RandomAccessFile("Firmenlogo.JPG", "r");
size = (int)logo.length();
byte imext1[] = new byte[size];
logo.readFully(imext1);
logo.close();
Image img1 = Image.getInstance(imext1);
img1.setAbsolutePosition(400, 744);
img1.scaleAbsolute(162, 80);
document.add(img1);
RandomAccessFile punkt1 = new RandomAccessFile("Punkt1.JPG", "r");
size = (int)punkt1.length();
byte imext3[] = new byte[size];
punkt1.readFully(imext3);
punkt1.close();
Image img3 = Image.getInstance(imext3);
img3.setAbsolutePosition(document.left(), 92);
img3.scaleAbsolute(15, 15);
document.add(img3);
RandomAccessFile punkt2 = new RandomAccessFile("Punkt2.JPG", "r");
size = (int)punkt2.length();
byte imext4[] = new byte[size];
punkt2.readFully(imext4);
punkt2.close();
Image img4 = Image.getInstance(imext4);
img4.setAbsolutePosition(document.left()+170, 92);
img4.scaleAbsolute(15, 15);
document.add(img4);
RandomAccessFile punkt3 = new RandomAccessFile("Punkt3.JPG", "r");
size = (int)punkt3.length();
byte imext5[] = new byte[size];
punkt3.readFully(imext5);
punkt3.close();
Image img5 = Image.getInstance(imext5);
img5.setAbsolutePosition(document.left()+345, 92);
img5.scaleAbsolute(15, 15);
document.add(img5);
PdfContentByte cb = writer.getDirectContent(); //Linien zeichnen
if(n > 1)
{
PdfContentByte cb2 = writer.getDirectContent(); //Linien
zeichnen
cb2.moveTo(40, 40);
cb2.lineTo(80, 80);
cb2.stroke();
}
else
{
cb.moveTo(document.left(), 123);
cb.lineTo(document.right(), 123);
cb.stroke();
cb.moveTo(document.left(), 122);
cb.lineTo(document.right(), 122);
cb.stroke();
cb.moveTo(document.left(), 737);
cb.lineTo(document.right(), 737);
cb.stroke();
}
Signatur signatur = new Signatur(signaturfilename);
Font font = new Font();
font.setSize(10);
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(document.left()+20, 20, document.left()+170,
115, 15, Element.ALIGN_LEFT);
for(int i = 0; i < 6; i++)
{
ct.addText(new Paragraph(signatur.getLine(0, i), font));
ct.go();
}
ct.setSimpleColumn(document.left()+190, 20, document.left()+340,
115, 15, Element.ALIGN_LEFT);
for(int i = 0; i < 6; i++)
{
ct.addText(new Paragraph(signatur.getLine(1, i), font));
ct.go();
}
ct.setSimpleColumn(document.left()+365, 20, document.right(), 115,
15, Element.ALIGN_LEFT);
for(int i = 0; i < 6; i++)
{
ct.addText(new Paragraph(signatur.getLine(2, i), font));
ct.go();
}
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
return dateiname;
}public void insertContent(Document document, int index, int pages)
{
String[] details = loadDetails(index);
int bewertung = getBewertung(index);
String vorgang = getVorgang(index+1);
String bearbeiter = getBearbeiter(index+2);
String faelligkeit = getFaelligkeit(index+3);
String anmerkung = getAnmerkung(index+4);
String maßnahmen = getMaßnahmen(details[0]);
String beschreibung = getBeschreibung(details[1]);
String[] paragraphen = getParagraphen(details[2]);
String geprueft = getGeprueft(details[3]);
try{
for(int i = 0; i < 6; i++)
document.add(new Paragraph(" "));
float[] widths = {0.25f, 0.75f};
PdfPTable table = new PdfPTable(widths);
setCurrenttable(table);
getCurrenttable().addCell(createCell(new Phrase ("Vorgang:")));
getCurrenttable().addCell(createCell(new Phrase (vorgang)));
addCellsBewertung(getCurrenttable(), bewertung);
getCurrenttable().addCell(createCell(new Phrase ("Bearbeiter:")));
getCurrenttable().addCell(createCell(new Phrase ( bearbeiter)));
if(faelligkeit != null && faelligkeit.replace(" ", "").equals("") ==
false)
{
getCurrenttable().addCell(createCell(new
Phrase("Fälligkeit:")));
getCurrenttable().addCell(createCell(new Phrase(faelligkeit)));
}
if(anmerkung != null && anmerkung.replace(" ", "").equals("") == false)
{
getCurrenttable().addCell(createCell(new Phrase("Anmerkung:")));
getCurrenttable().addCell(createCell(new Phrase(anmerkung)));
}
if(geprueft != null && geprueft.replace(" ", "").equals("") == false)
{
getCurrenttable().addCell(createCell(new Phrase("geprüft
am:")));
getCurrenttable().addCell(createCell(new Phrase(geprueft)));
}
if(checkPages("Anmerkung", index))
{
document.add(getCurrenttable());
PdfPTable table2 = new PdfPTable(widths);
setCurrenttable(table2);
document.newPage();
createTemplate(document, index, 2);
for(int i = 0; i < 6; i++)
document.add(new Paragraph(" "));
}
if(maßnahmen != null && maßnahmen.replace(" ", "").equals("") == false)
{
getCurrenttable().addCell(createCell(new Phrase("Maßnahmen:")));
getCurrenttable().addCell(createCell(new Phrase(maßnahmen)));
}
if(checkPages("Maßnahmen", index))
{
document.add(getCurrenttable());
PdfPTable table3 = new PdfPTable(widths);
setCurrenttable(table3);
document.newPage();
createTemplate(document, index, 3);
for(int i = 0; i < 6; i++)
document.add(new Paragraph(" "));
}
addCellsParagraphen(getCurrenttable(), paragraphen);
if(checkPages("Paragraphen", index))
{
document.add(getCurrenttable());
PdfPTable table4 = new PdfPTable(widths);
setCurrenttable(table4);
document.newPage();
createTemplate(document, index, 4);
for(int i = 0; i < 6; i++)
document.add(new Paragraph(" "));
}
if(beschreibung != null && beschreibung.replace(" ", "").equals("") ==
false)
{
getCurrenttable().addCell(createCell(new
Phrase("Beschreibung:")));
getCurrenttable().addCell(createCell(new Phrase(beschreibung)));
}
document.add(getCurrenttable());
} catch (DocumentException e) {
e.printStackTrace();}
}------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php