使用java创建复杂的pdf [英] Creating complex pdf using java

查看:819
本文介绍了使用java创建复杂的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Java / Java EE的应用程序,其中我需要为将提供给用户的各种服务创建PDF证书。我正在寻找一种创建PDF的方法(暂时不需要数字证书)。

I have an Java/Java EE based application wherein I have a requirement to create PDF certificates for various services that will be provided to the users. I am looking for a way to create PDF (no need for digital certificates for now).

最简单方便的方法是什么?我已经尝试了

What is the easiest and convenient way of doing that? I have tried


  1. XSL到pdf的转换

  2. 使用itext进行HTML到PDF的转换。

  3. 粗糙java方式(使用PDFWriter,PdfPCell等)

这些是最好的方法还是有其他方法更容易和方便吗?

What is the best way out of these or is there any other way which is easier and convenient?

推荐答案

当你谈到证书时,我想到了标准的表格对于证书的每个接收者都是相同的,除了:

When you talk about Certificates, I think of standard sheets that look identical for every receiver of the certificate, except for:


  • 接收者的姓名

  • 当然接收者接着

  • 一个日期

如果是这样的话,我将使用任何工具,允许您创建一个花哨的证书(Acrobat,Open Office,Adobe InDesign,...),并创建一个包含三个字段的静态表单(有时称为AcroForm):名称,课程,日期。

If this is the case, I would use any tool that allows you to create a fancy certificate (Acrobat, Open Office, Adobe InDesign,...) and create a static form (sometimes referred to as an AcroForm) containing three fields: name, course, date.

然后我会用iText填写以下字段:

I would then use iText to fill in the fields like this:

PdfReader reader = new PdfReader(pathToCertificateTemplate);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(pathToCertificate));
AcroFields form = stamper.getAcroFields();
form.setField("name", name);
form.setField("course", course);
form.setField("date", date);
stamper.setFormFlattening(true);
stamper.close();
reader.close();

从代码创建这样的证书是艰难的方式;从XML创建这样的证书是痛苦(因为XML不适合定义布局),使用iText的XML Worker可以从(HTML + CSS)创建证书,但所有这些解决方案都有缺点要正确定位每个项目,确保所有内容都适合同一页面等等,这是很难的工作......

Creating such a certificate from code is "the hard way"; creating such a certificate from XML is "a pain" (because XML isn't well-suited for defining a layout), creating a certificate from (HTML + CSS) is possible with iText's XML Worker, but all of these solutions have the disadvantage that it's hard work to position every item correctly, to make sure everything fits on the same page, etc...

使用固定字段维护模板要容易得多。这样,您只需编写一次代码。如果由于某种原因你想将字段移动到另一个地方,你只需要更改模板,你不必担心在代码,XML,HTML或CSS中乱搞。

It's much easier to maintain a template with fixed fields. This way, you only have to code once. If for some reason you want to move the fields to another place, you only have to change the template, you don't have to worry about messing around in code, XML, HTML or CSS.

请参阅 http://www.manning.com/lowagie2/samplechapter6.pdf 获取更多信息(第6.3.5节)。

See http://www.manning.com/lowagie2/samplechapter6.pdf for some more info (section 6.3.5).

这篇关于使用java创建复杂的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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