有没有办法可以使用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();
答案 0 :(得分:1)
您想要一个包含5000到5000个用户单位的PDF页面。这意味着您将使用如下自定义矩形创建文档:
Rectangle rect = new Rectangle(5000, 5000);
Document document = new Document(rect);
显然,你也会创建一个像这样的作家:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("my_file.pdf"));
你打算打开文件:
document.open();
让我们假设您的Graphics2D"图像"是10000乘10000点。这意味着您需要将该图像分发到4页。
诀窍是创建一个测量10000乘10000的PdfTemplate
对象。
PdfContentByte canvas = writer.getDirectContent();
PdfTemplate template = canvas.createTemplate(10000, 10000);
现在我们将图形绘制到此模板:
Graphics2D gd2d = new PdfGraphics2D(template, 10000, 10000);
// draw stuff to gd2d
gd2d.dispose();
您现在已经创建了一个包含不适合页面内容的大型Form XObject。此内容仅作为外部对象出现在PDF文件中(因此称为XObject)。您所要做的就是根据需要多次添加文档。
例如:
canvas.addTemplate(template, 0, 0);
document.newPage();
我希望你有一个坐标系的概念:(0,-5000),( - 5000,-5000),(0,0)和(-5000,0)是除法所需的x,y偏移量超过4页的10000乘10000对象,大小只有5000乘5000。
剩下的唯一代码行是:
document.close();
你已经完成了。
有关完整示例,请参阅LargeTemplate:
public void createPdf(String dest) throws IOException, DocumentException {
float width = 602;
float height = 15872;
float maxHeight = 5000;
Document document = new Document(new Rectangle(width, maxHeight));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
PdfContentByte canvas = writer.getDirectContent();
PdfTemplate template = canvas.createTemplate(width, height);
Graphics2D g2d = new PdfGraphics2D(template, width, height);
for (int x = 10; x < width; x += 100) {
for (int y = 10; y < height; y += 100) {
g2d.drawString(String.format("(%s,%s)", x, y), x, y);
}
}
g2d.dispose();
int pages = ((int)height / (int)maxHeight) + 1;
for (int p = 0; p < pages; ) {
p++;
canvas.addTemplate(template, 0, (p * maxHeight) - height);
document.newPage();
}
document.close();
}
如您所见,我使用了您在评论中提到的尺寸:
float width = 602;
float height = 15872;
我还介绍了最大高度:
float maxHeight = 5000;
我创建了一个尺寸宽度为x maxHeight的页面和一个尺寸宽度为x高度的Graphics2D模板。出于测试目的,我使用drawString()
每100个点绘制坐标信息。
我计算了我需要多少页面:
int pages = ((int)height / (int)maxHeight) + 1;
现在我添加模板的次数与页数一样多,使用一些基本数学来计算偏移量:
for (int p = 0; p < pages; ) {
p++;
canvas.addTemplate(template, 0, (p * maxHeight) - height);
document.newPage();
}
我不知道您正在使用哪种数学,但生成的PDF看起来完全符合我的预期:large_template.pdf
答案 1 :(得分:0)
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();