Folks'es,

I just started using PDFBox after having to abondon iText (due to licensing issues...). I already successfully integrated the generation of a PDF document from a Java Swing drawing in iText and now I would like to accomplish the same with PDFBox. Unfortunately it doesn't quite work the same/in a similar way. Here is what I have:

 * an Swing GUI that draws a chart
 * the user hits an "Export" button and the chart is saved as a PDF file
 * if the chart is bigger than the selected page size I need to
   paginate the image
 * at the end of the document I want to add table with some explanatory
   remarks

So far I have been able to to draw the whole chart onto one page (based on the current size of the canvas). With the following code all I get is n pages each the size of 210x1mm (or thereabouts), see attached result. Can any kind soul tell me, what I'm doing wrong?

BufferedImage img = new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_INT_RGB);
canvas.export((Graphics2D)img.getGraphics());

pageSize = canvas.pageSize();
if (pageSize == null)
    pageSize = new Rectangle(canvas.getWidth(), canvas.getHeight());
float pageWidth = pageSize.getWidth();
float pageHeight = pageSize.getHeight();
PDRectangle mediaBox = new PDRectangle(pageWidth * canvas.columns(), pageHeight * canvas.rows());
PDRectangle cropBox = new PDRectangle(pageWidth, pageHeight);

PDDocument doc = null;
try {
    doc = new PDDocument();
    PDXObjectImage ximage = new PDJpeg(doc, img);

    while (true) {
        PDPage page = new PDPage(mediaBox);
        page.setCropBox(cropBox);
        doc.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
        contentStream.drawImage(ximage, 0, 0);
        contentStream.close();
        cropBox.move(pageWidth, 0);
        if (cropBox.getUpperRightX() > mediaBox.getUpperRightX()) {
            cropBox.setLowerLeftX(0);
            cropBox.setUpperRightX(pageWidth);
            cropBox.move(0, pageHeight);
        }
        if (cropBox.getUpperRightY() > mediaBox.getHeight())
            break;
    }
    doc.save(fileName);
} finally {
     if (doc != null) {
        doc.close();
     }
}

Thanks,

Thomas

-- Intelligent Communication Software Vertriebs GmbH Firmensitz: Kistlerhof Str. 111, 81379 München Registergericht: Amtsgericht München, HRB 88283 Geschäftsführer: Albert Fuss

Reply via email to