Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用iText消除PDF中的所有空白_Pdf_Whitespace_Itext - Fatal编程技术网

使用iText消除PDF中的所有空白

使用iText消除PDF中的所有空白,pdf,whitespace,itext,Pdf,Whitespace,Itext,我正在测试iText以生成一个PDF,它由8个平铺格式的图像组成。我使用JFreeChart创建一个图形,然后通过iText将其转换为图像。PDF生成得很好,但是当我打开输出文件时,左、右和底部仍然有大约一英寸的空白。我想在打印时利用合法尺寸页面上的所有空间 我知道PDF中没有“边距”的概念,也不是可编辑的格式。必须创建没有空白的图像。文档构造函数中的额外参数实际上做了什么 我认为,通过向Document对象(LEGAL和1f参数)提供必要的参数,可以消除空白,我的表将占用打印页面上的所有8.5

我正在测试iText以生成一个PDF,它由8个平铺格式的图像组成。我使用JFreeChart创建一个图形,然后通过iText将其转换为图像。PDF生成得很好,但是当我打开输出文件时,左、右和底部仍然有大约一英寸的空白。我想在打印时利用合法尺寸页面上的所有空间

我知道PDF中没有“边距”的概念,也不是可编辑的格式。必须创建没有空白的图像。文档构造函数中的额外参数实际上做了什么

我认为,通过向Document对象(LEGAL和1f参数)提供必要的参数,可以消除空白,我的表将占用打印页面上的所有8.5x14,但运气不好

有什么建议吗?提前谢谢

原始代码:

// Setup document 
        Document doc = new Document(PageSize.LEGAL, 1f, 1f, 1f, 1f); 
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("c:\\temp\\image_in_chunk.pdf")); 

        doc.open(); 

        //create the chart, save to file system, and create an iText Image object
        ChartUtilities.saveChartAsPNG(new File("C:\\temp\\img.png"), createChart(createDataset()), 240, 240);
        Image img1 = Image.getInstance("C:\\temp\\img.png"); 

        PdfPCell cell1 = null;
        Paragraph paragraph = new Paragraph(); 
        paragraph.add(new Chunk(img1, 0, 0, true)); 

        PdfPTable table = new PdfPTable(2);
        for (int i = 0; i < 8; i++) 
        { 
            cell1 = new PdfPCell(paragraph); 
            table.addCell(cell1);
        } 

        doc.add(table);
        doc.close();
//安装文档
单据单据=新单据(PageSize.LEGAL,1f,1f,1f,1f);
PdfWriter writer=PdfWriter.getInstance(doc,新文件输出流(“c:\\temp\\image\in_chunk.pdf”);
doc.open();
//创建图表,保存到文件系统,并创建iText图像对象
saveChartAsPNG(新文件(“C:\\temp\\img.png”)、createChart(createDataset()),240240;
Image img1=Image.getInstance(“C:\\temp\\img.png”);
PdfPCell cell1=null;
段落=新段落();
添加(新块(img1,0,0,true));
PdfPTable=新的PdfPTable(2);
对于(int i=0;i<8;i++)
{ 
cell1=新的PdfPCell(段落);
表1.addCell(cell1);
} 
单据新增(表);
doc.close();
更正和工作代码(当然,创建您自己的JFreeChart作为img1。我不能发布不是成员的示例图像输出):

//安装文档
文档文档=新文档(PageSize.LEGAL,0f,0f,0f,0f);
PdfWriter writer=PdfWriter.getInstance(doc,新文件输出流(“c:\\temp\\image\in_chunk.pdf”);
doc.open();
//创建图表,保存到文件系统,并创建iText图像对象
saveChartAsPNG(新文件(“C:\\temp\\img.png”)、createChart(createDataset()),305250;
Image img1=Image.getInstance(“C:\\temp\\img.png”);
//创建pdf文档
对于(int i=0;i<8;i++)
{ 
doc.add(新块(img1,0,0,true));
} 
doc.close();

好的,您正在将
文档中的页边距设置为1点。1点是1/72英寸。您可能应该改用
0f
,但这并不能解释1英寸的边距。小白条?当然但不是你所描述的

几乎可以肯定,问题源于将
图像
包装在
段落
中,而段落又包装在
PdfPTable

我建议您缩放图像以匹配页面大小,然后直接将图像添加到文档中,而不是将其包装到表格中:

Image img1 = Image.getInstance(path);
img1.scaleAbsoluteHeight(PageSize.LEGAL.getHeight());
img1.scaleAbsoluteWidth(PageSize.LEGAL.getWidth());

// you might need this, you might not.
img1.setAbsolutePosition(0, 0);

// and add it directly.
document.add(img1);

谢谢你的建议。现在一切看起来都很好。立即取出表格有助于减少左/右的空白,我可以利用整个可打印区域的宽度。将图像包装在段落中不允许平铺效果使用所有14个段落“长度,因此,将其更改为您直接将图像添加到文档中的建议也允许我使用整个长度。您能告诉我在grails中使用jFreeChart生成条形图时如何删除顶部空白吗?您能告诉我在grails中使用jFreeChart生成条形图时如何删除顶部空白吗?
Image img1 = Image.getInstance(path);
img1.scaleAbsoluteHeight(PageSize.LEGAL.getHeight());
img1.scaleAbsoluteWidth(PageSize.LEGAL.getWidth());

// you might need this, you might not.
img1.setAbsolutePosition(0, 0);

// and add it directly.
document.add(img1);