将大型图形2d文档写入多个pdf

将大型图形2d文档写入多个pdf,pdf,itext,graphics2d,jgraphx,jgraph,Pdf,Itext,Graphics2d,Jgraphx,Jgraph,是否有一种方法可以使用itext将大型jgraph graphics 2d文档拆分为pdf中的多个页面。 我有一个使用jgraph创建的大型流程图。我需要将此流程图写入多页pdf格式。我需要确保每个pdf页面高度限制为5000。因此,如果图形对象高度超过5000,它将跨越pdf中的多个页面。 有没有一种方法可以让我以块的形式读取图形对象(每次迭代的高度高达5000),继续将其写入新的pdf页面并进行迭代,直到我完全读取对象为止。 任何输入/指示都会有所帮助 以下是我到目前为止所拥有的- floa

是否有一种方法可以使用itext将大型jgraph graphics 2d文档拆分为pdf中的多个页面。 我有一个使用jgraph创建的大型流程图。我需要将此流程图写入多页pdf格式。我需要确保每个pdf页面高度限制为5000。因此,如果图形对象高度超过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();

您需要一个包含5000×5000个用户单位的页面的PDF。这意味着您将创建一个具有如下自定义矩形的文档:

Rectangle rect = new Rectangle(5000, 5000);
Document document = new Document(rect);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("my_file.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();
现在,您已经创建了一个大型表单XObject,其内容与页面不匹配。此内容在PDF文件中仅作为外部对象出现一次(因此称为XObject)。您所要做的就是根据需要多次添加文档

例如:

canvas.addTemplate(template, 0, 0);
document.newPage();
我希望你能对坐标系有一个概念:(0,-5000),(-5000,-5000),(0,0)和(-5000,0)是在4页大小仅为5000乘5000的页面上除以10000乘10000对象所需的x,y偏移量

剩下的唯一一行代码是:

document.close();
你完成了

有关完整示例,请参见:

我还介绍了最大高度:

float maxHeight = 5000;
我创建了一个尺寸为宽度x最大高度的页面和一个尺寸为宽度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();
}
for(int p=0;p

我不知道您使用的是哪种数学,但生成的PDF看起来与我期望的完全一样:

谢谢Bruno。我尝试了上面的方法,但在我的pdf的第一页中,我只看到了我的图像的最后一块(5000个用户单位),而所有其他页面都是空白的。下面是我的代码片段,请勿使用注释添加代码。更新您的问题。另外:在你提问之前再做一些实验。如果使用
addTemplate()
,表单XObject将被添加到页面中。现在为
x
y
尝试一些不同的值。谢谢Bruno。我试过各种xy科迪酸盐。我打印了我的图像的实际尺寸,尺寸是602x15872。我尝试在x,y坐标以下。所有这些坐标都只给我pdf第一页中图像的最后一块,其余所有页面均为空白。很抱歉,我没有添加我在早期评论中尝试的坐标。我尝试了(0,-15000),(0,-10000),(0,-5000),(0,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();