[英]Insert a csv file as attachment in a pdf file JAVA
I need to generate PDF which contains inside a CSV file as an attachment that can be opened (double clicking on it). 我需要生成包含CSV文件的PDF,并将其作为可以打开的附件(双击它)。 Is there a way to do this programmaticaly in JAVA?
有没有办法在Java中以编程方式执行此操作? Thanks.
谢谢。
Since you don't show any code, it's hard to know whether you are using the new iText 7 or the old (and no longer supported) iText 5. 由于您没有显示任何代码,因此很难知道您使用的是新的iText 7还是旧的(不再受支持的)iText 5。
It is also unclear whether you want to attach as a document-level attachment (visible only in the attachment panel of the PDF) or a file as an annotation (also visible on a page). 还不清楚要附加为文档级附件(仅在PDF的附件面板中可见)还是文件作为批注(在页面上也可见)。
Let's assume that you're new at iText, and that you started with the most recent version. 假设您是iText的新手,并且您使用的是最新版本。 In that case, you can add a document-level attachment like this:
在这种情况下,您可以像这样添加文档级附件:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
PdfFileSpec fs = PdfFileSpec.createEmbeddedFileSpec(pdfDoc,
PATH, null, "test.csv", null, null, false);
pdfDoc.addFileAttachment("specificname", fs);
In this case PATH
is a file path to your CSV file. 在这种情况下,
PATH
是CSV文件的文件路径。
The iText 7 code to add a file attachment annotation looks like this: 用于添加文件附件批注的iText 7代码如下所示:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
Rectangle rect = new Rectangle(36, 700, 100, 100);
PdfFileSpec fs = PdfFileSpec.createEmbeddedFileSpec(pdfDoc,
PATH, null, "test.csv", null, null, false);
PdfAnnotation attachment = new PdfFileAttachmentAnnotation(rect, fs)
.setContents("Click me");
pdfDoc.addNewPage().addAnnotation(attachment);
The rectangle rect
defines the position and dimensions of the annotation. 矩形
rect
定义注释的位置和尺寸。 If you want to add a file attachment annotation to an existing document, you can create a PdfDocument
that also takes a PdfReader
instance as parameter. 如果要向现有文档添加文件附件批注,则可以创建一个
PdfDocument
,它也将PdfReader
实例作为参数。
If for some reason you still want to use the old iText, then you can add a document-level attachment like this: 如果由于某种原因您仍要使用旧的iText,则可以添加如下文档级附件:
PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(
writer, null, fileName, csv_stream;
fs.AddDescription("specificname", false);
writer.AddFileAttachment(fs);
Or like this: 或像这样:
PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(
writer, null, fileName, csv_stream);
fs.AddDescription("specificname", false);
stamper.AddFileAttachment(fs);
The former snippet is for when you have a PdfWriter
instance because you're creating a document from scratch; 前一个代码段适用于拥有
PdfWriter
实例的情况,因为您是从头开始创建文档的。 the latter snippet is for when you have a PdfStamper
instance because you're adding an attachment to an existing document. 后一个代码段适用于拥有
PdfStamper
实例的情况,因为您要将附件添加到现有文档中。
For a file attachment annotation, you need this code: 对于文件附件注释,您需要以下代码:
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
writer, null, fileName, csv_stream);
PdfAnnotation attachment =
PdfAnnotation.createFileAttachment(writer, rect, fileName, fs);
writer.addAnnotation(attachment);
Or, if you don't have a writer
, but a stamper
: 或者,如果您没有
writer
,但没有stamper
:
stamper.addAnnotation(attachment, p);
where p
is a page number. 其中
p
是页码。
Obviously, it's recommended to use iText 7 as that's more future-proof than iText 5. 显然,建议使用iText 7,因为它比iText 5更适合未来使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.