有没有一种方法可以使用 itext 将大型 jgraph 图形 2d 文档拆分为 pdf 中的多个页面。我有一个使用 jgraph 创建的大型流程图。我需要把这个流程图写成一个多页的pdf。我需要确保每个 pdf 页面高度限制为 5000.SO 如果图形对象高度超过 5000,它将跨越 pdf 中的多个页面。有没有办法我可以读取块中的图形对象(每次迭代的高度为 5000),继续将其写入新的 pdf 页面并迭代直到我完全读取对象。任何输入/指示都会有所帮助。
以下是我到目前为止所拥有的 -
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();