Hi all,
Has anyone used PFDBox before?
I'm having trouble with makeing a pdf with mulitple lines:

contentStream.drawString( patient.toString() );

patient.toString() has newline characters in it(\n) and drawString dosn't recognize them.

Sorry but the link below did not provides any information related to the toppic; this is just for downloading the software.

sounds like you need to read the manual of the tool you're using rather than expect someone else to do that for you.

Thanks everyone for replying, I have read the manual, was just hoping to get some expert advise.

Can you please provide more information about your class?

what im having trouble with is when I pass a string with \n, into the method drawString(String string) in the class PDPageContentStream in the PDFBox library,the text in the pdf will print only on one line.
http://www.pdfbox.org/javadoc/org/pdfbox/pdmodel/edit/PDPageContentStream.html#drawString%28java.lang.String%29
Could this have something to do with the graphics? I've heard people have had trouble makeing a newline in a JLable.

public void exportPatient(Patients patient) throws IOException, COSVisitorException{
        //export their deitails
        PDDocument document = null;
        try{
            document = new PDDocument();
            PDPage page = new PDPage();
            document.addPage( page );

            PDFont font = PDType1Font.TIMES_ROMAN;
            PDPageContentStream contentStream = new  PDPageContentStream(document, page);
            contentStream.beginText();
            contentStream.setFont( font, 12 );
            contentStream.moveTextPositionByAmount( 100, 700 );
            contentStream.drawString( patient.toString() );
            contentStream.endText();
            contentStream.close();
            document.save( new String(patient.getPatientId().toString()) + ".pdf");
            

        }
        finally
        {
            if (document != null)
            {
                document.close();
            }
        }
}

This topic had no thing to do with ths escape character.

patient.toString() has newline characters in it(\n) and drawString dosn't recognize them.

you should not put the "\n" inside the String; this will not work.
for instance:

@Override
    public String toString() {
        return firstName + "\n " + lastName;
    }

this will not give you the write output avoid it please
Instead :

@Override
    public String toString() {
        return firstName + " "+ lastName;
    }

The code bellow is an exemple:
The Test calss is a simple one that have a firstName and a lastName with toString function:

package pdf.pdfbox;

/**
 *
 * @author moutanna
 */
public class Test {
    private String firstName;
    private String lastName;

    public Test(String firstName, String lastName) {
        this.firstName=firstName;
        this.lastName=lastName;
    }


    @Override
    public String toString() {
        return firstName + " " + lastName;
    }

}

the TestTextToPdf class is a simple test that take a list of Test class and put the to a nice PDF file (test.pdf) one instance in line.
If you wish to put every attribute in a single line you should split the retun of the toString function and loop over the resulting array. the principle is the same

package pdf.pdfbox;

/**
 *
 * @author moutanna
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDSimpleFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class TestTextToPdf {

    private int fontSize = 12;
    private PDSimpleFont font = PDType1Font.HELVETICA;

    private PDDocument createPDFFromText(List<Test> listObject) throws IOException {
        PDDocument doc = null;
        int margin = 40;
        float height = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000;

        //increase the height by 5 percent.
        height = height * fontSize * 1.05f;
        doc = new PDDocument();
        PDPage page = new PDPage();
        PDPageContentStream contentStream = null;
        float y = -1;

        for (int i = 0; i < listObject.size(); ++i) {


            if (y < margin) {
                page = new PDPage();
                doc.addPage(page);
                if (contentStream != null) {
                    contentStream.endText();
                    contentStream.close();
                }
                contentStream = new PDPageContentStream(doc,page);
                contentStream.setFont(font, fontSize);
                contentStream.beginText();
                y = page.getMediaBox().getHeight() - margin + height;
                contentStream.moveTextPositionByAmount(margin,y);

            }
            if (contentStream != null) {
                contentStream.moveTextPositionByAmount(0, -height);
                y -= height;
                contentStream.drawString(listObject.get(i).toString());

            } else {
                throw new IOException("Error: Expected non-null content stream.");
            }


        }
        if (contentStream != null) {
                contentStream.endText();
                contentStream.close();
            }

        return doc;
    }

    public static void main(String[] args) throws IOException, COSVisitorException {
        TestTextToPdf app = new TestTextToPdf();
        PDDocument doc = null;
        List listObject = new ArrayList<Test>();
        listObject.add(new Test("first", "last"));
        listObject.add(new Test("another first", "another last"));
        doc = app.createPDFFromText(listObject);

        doc.save("test.pdf");
    }
}

Just copy past and run.

Hope it helps.

that helped alot, thanks heaps. my code only needs to print one object to pdf, not a list, but I can use that logic of printing one thing at a time, until it needs to go to the next line, then change the position.
thanks again,
danny.

This question has already been answered. Start a new discussion instead.