Hello again,in attachment, you'll find your example with some corrections and the resulting PDF. Because I didn't see the cause of the problem immediately, I have moved part of the code to a separate method. I think you should do that too (including the image),
because it will save you time if you need to change your code.There are also some thing that I would have done in a slightly different way (working with a PdfPTable, or drawing the line with moveTo/lineTo instead of using a string,...),
but working with PdfContentByte may be the fastest way. best regards, Bruno Lowagie
PS: this was the message I received from the mailserver. I am now trying from another mailserver. The original message was received at Tue, 28 Jun 2005 17:12:35 +0200 from outmx014.isp.belgacom.be [195.238.2.69] ----- Transcript of session follows ----- <[EMAIL PROTECTED]>... Deferred: Name server: maxwell.af.mil.: host name lookup failure Warning: message still undelivered after 4 hours Will keep trying until message is 2 days old
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Image;
/**
* Adding text at an absolute position.
*/
public class Text {
/**
* Adding text at absolute positions.
* @param args no arguments needed
*/
public static void main(String[] args) {
System.out.println("trying to get this to work");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4.rotate());
boolean topRecord = true;
try
{
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream("text.pdf"));
// step 3: we open the document
document.open();
String url = "C:\\dodseal.jpg";
Image sealDod = Image.getInstance(url);
// step 4: we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA_BOLD,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
for (int i=0; i < 9; i++)
{
if(topRecord == true)
{
sealDod.setAbsolutePosition(25, 350);
sealDod.scaleAbsolute(200, 200);
document.add(sealDod);
writeInfo(cb, bf, i, 425);
topRecord = false;
}
else
{
sealDod.setAbsolutePosition(25, 50);
sealDod.scaleAbsolute(200, 200);
document.add(sealDod);
writeInfo(cb, bf, i, 125);
topRecord = true;
}
//Every 2 records create a new page
if(i % 2 == 1)
{
//Then create new page with a newPage method in our util
file
document.newPage();
}
}
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
private static void writeInfo(PdfContentByte cb, BaseFont bf, int i, float
y) {
String fname = "First Name " +i;
String lname = "Last Name " +i;
String service = "USAF " +i;
String borderTop =
"______________________________________________________";
String borderBottom = "YOYO D. SMITH COLLEGE FOR PROFESSIONAL
DEVELOPMENT";
cb.beginText();
cb.setFontAndSize(bf, 24);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, fname, 500, y, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, lname, 500, y - 50, 0);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, service, 25, y - 75, 0);
cb.setFontAndSize(bf, 18);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, borderTop, 475, y - 75,
0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, borderBottom, 475, y -
100, 0);
cb.endText();
}
}
text.pdf
Description: Adobe PDF document
