Search code examples
pdfitextgraphics2djgraphxjgraph

Writing a large graphics 2d document to multiple pdf


Is there a way i can split up a large jgraph graphics 2d document into multiple pages in a pdf using itext. I have a large flowchart created using jgraph. I need to write this flowchart into a pdf wit multiple pages. I need to make sure that each pdf page height is limited to 5000.SO if the graphics object height is more than 5000 it spans across multiple pages in the pdf. Is there a way i can read the graphics object in chunk (upto a height of 5000 in each iteration), keep writing it to a new pdf page and iterate till i completely read the object. Any inputs/directions will be helpful.

Below is what i have till now -

float imageIdealHeight = 5000;
float imageIdealWidth = 5000;
float imageActualWidth=0;
float imageActualHeight=0;
mxRectangle imageBounds = ((mxGraph) graph).getGraphBounds();
imageActualWidth= (float) imageBounds.getWidth();
imageActualHeight = (float) imageBounds.getHeight();

System.out.println("Actual Width = "+imageActualWidth);
System.out.println("Actual Height = "+imageActualHeight);

numPages = (int) Math.ceil(imageActualHeight/imageIdealHeight);

Rectangle actualRectangle = new Rectangle(imageActualWidth, imageActualHeight);

//Custom Rectangle
Rectangle idealRectangle = new Rectangle(imageIdealWidth, imageIdealHeight);
Document document = new Document(idealRectangle );

//Create Pdf Writer
PdfWriter writer = PdfWriter.getInstance(document, fos);

//Open Document
document.open();

//Create huge template with actual image dimensions 
PdfContentByte canvas = writer.getDirectContent();
PdfTemplate template = canvas.createTemplate(imageActualWidth, imageActualHeight);
PdfCanvasFactory pdfCanvasFactory = new PdfCanvasFactory(canvas);

//Draw graphics to this template 
Graphics2D g2=template.createGraphics(imageActualWidth, imageActualHeight);
mxGraphics2DCanvas mxcanvas = new mxGraphics2DCanvas( g2);

mxcanvas = (mxGraphics2DCanvas) mxCellRenderer.drawCells((mxGraph) graph, null, 1, null, pdfCanvasFactory);
mxcanvas.getGraphics().dispose();

g2.dispose();

//Add template now...      
canvas.addTemplate(template,0, -15000);
document.newPage();
canvas.addTemplate(template, 0, -10000);
document.newPage();
canvas.addTemplate(template,0 , -5000);
document.newPage();
canvas.addTemplate(template, 0, 0);
document.newPage();

document.close();

Solution

  •     int numPages; 
        float imageIdealHeight = 5000;
        float imageIdealWidth = 5000;
        float imageActualWidth=0;
        float imageActualHeight=0;
        mxRectangle imageBounds = ((mxGraph) graph).getGraphBounds();
        imageActualWidth= (float) imageBounds.getWidth();
        imageActualHeight = (float) imageBounds.getHeight();
    
        System.out.println("Actual Width = "+imageActualWidth);
        System.out.println("Actual Height = "+imageActualHeight);
    
        numPages = (int) Math.ceil(imageActualHeight/imageIdealHeight);
    
        Rectangle actualRectangle = new Rectangle(imageActualWidth, imageActualHeight);
    
        //Custom Rectangle
        Rectangle idealRectangle = new Rectangle(imageActualWidth, imageIdealHeight);
        Document document = new Document(idealRectangle);
    
        //Create Pdf Writer
        PdfWriter writer = PdfWriter.getInstance(document, fos);
    
        //Open Document
        document.open();
    
        //Create huge template with actual image dimensions 
        PdfContentByte canvas = writer.getDirectContent();
        PdfTemplate template = canvas.createTemplate(imageActualWidth, imageActualHeight);
    
    
        //Draw graphics to this template 
       Graphics2D g2=template.createGraphics(imageActualWidth, imageActualHeight);
       g2.drawImage(img, null, 0, 0);
       g2.dispose();  
       for (int p = numPages; p > 0; ) {
       p--;
            canvas.addTemplate(template, 0, -  ((p-1) *imageIdealHeight  + (imageActualHeight  % imageIdealHeight))  );            
            document.newPage();
        }
    
    
        document.close();