如何使用iText将图形绘制为PDF? [英] How do I draw graphics to PDF using iText?

查看:182
本文介绍了如何使用iText将图形绘制为PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试完成绘制图形并将其写入PDF的示例,但我不断收到PDF没有页面的错误。如果我在打开后用document.add()添加一些简单的东西就可以正常工作,我只是看不到图形。这是我的代码:

I am trying to complete an example that draws graphics and writes them to PDF, but I keep getting errors that the PDF has no pages. if I add something simple with document.add() after opening it works fine, I just never see the graphics. Here is my code:

Document document = new Document();
PdfWriter writer = new PdfWriter();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
    " attachment; filename=\"Design.pdf\"");

writer = PdfWriter.getInstance(document, response.getOutputStream());

document.open();    
PdfContentByte cb = writer.getDirectContent();
Graphics2D graphics2D = cb.createGraphics(36, 54);
graphics2D.drawString("Hello World", 36, 54);
graphics2D.dispose();   
document.close();

我是否必须做其他事情才能将图形添加到文档中,或者我的语法不正确?

Do I have to do something else to add the graphic to the document or is my syntax incorrect?

推荐答案

文档doc =新文档(PageSize.A4);
有什么不同吗?

Does Document doc = new Document(PageSize.A4); make any difference?

我不知道你是否需要像这样添加段落

I don't know if you need to add a Paragraph like this:

doc.add(new Paragraph(...));

我们还使用 doc.add(ImgRaw); 添加图像。

这篇关于如何使用iText将图形绘制为PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆