在 pdf 中添加附件注释并在 Web 应用程序 jar 中执行
Add attachment annotation in pdf and executed within web application jar
我正在使用itext注释在pdf中实现添加附件注释,但非常有趣的是,由于web应用程序框架的限制,我必须将实现代码和附件文件放在jar文件中。以下是实施的简要层次结构:
- 套餐example.pdf
- Implementation.java
- attachment.doc
主要代码:
protected void createAttachment(PdfWriter writer, Rectangle rect, String
templatePath, String fileName) throws IOException, DocumentException {
// Get instruction document
String embed =
getClass().getClassLoader().getResource(templatePath).getFile();
// The fileName here is used to display in the attachment list of the
// document
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
writer, embed, fileName, null);
// The fileName here is used to display on the document
PdfAnnotation attachment =
PdfAnnotation.createFileAttachment(writer, rect, fileName, fs);
// Specify the width and height of the icon area
PdfAppearance app = writer.getDirectContent().createAppearance(200, 200);
String wordIcon =
getClass().getClassLoader().getResource(WORD_ICON).getFile();
Image img = Image.getInstance(wordIcon);
img.scaleAbsolute(200, 200);
img.setAbsolutePosition(0, 0);
app.addImage(img);
attachment.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);
writer.addAnnotation(attachment);
}
以上代码将被服务调用 class:
Rectangle rect = new Rectangle(220, 620, 250, 640);
createAttachment(pdfWriter, rect, "pdf/attachment.doc",
"attachment.doc");
它在我的 IDE 下正常工作,但是一旦我将它们制作成 jar(包括附件),网络应用程序似乎无法 found/attached 附件。并告诉我无法在系统路径中找到该文件。
我知道这可能是与 class 路径检测机制(例如 class 加载程序和加载方法)有关的问题,但它确实阻止了我进一步前进,因为我没有找到任何可能的解决方案。
异常:
java.io.FileNotFoundException: C:\Projects\work\test-1.3.6.0-
SNAPSHOT.jar!\pdf\attachment.doc (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at java.net.URL.openStream(URL.java:1045)
at com.lowagie.text.pdf.PdfFileSpecification.fileEmbedded(Unknown Source)
at com.lowagie.text.pdf.PdfFileSpecification.fileEmbedded(Unknown Source)
at com.lowagie.text.pdf.PdfFileSpecification.fileEmbedded(Unknown Source)
at example.pdf.Implementation.createAttachment
问题已解决。
唯一的区别是采用输入流而不是文件名,class PdfFileSpecification 有这样的接口。
InputStream embedInputStream =
getClass().getClassLoader().getResourceAsStream(templatePath);
byte[] buffer = new byte[1024];
embedOutputStream = new ByteArrayOutputStream();
int len = -1;
while ((len = embedInputStream.read(buffer)) != -1) {
embedOutputStream.write(buffer, 0, len);
}
// The fileName here is used to display in the attachment list of the document
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
writer, null, fileName, embedOutputStream.toByteArray());
// The fileName here is used to display on the document
PdfAnnotation attachment =
PdfAnnotation.createFileAttachment(writer, rect, fileName, fs);
我正在使用itext注释在pdf中实现添加附件注释,但非常有趣的是,由于web应用程序框架的限制,我必须将实现代码和附件文件放在jar文件中。以下是实施的简要层次结构:
- 套餐example.pdf
- Implementation.java
- attachment.doc
主要代码:
protected void createAttachment(PdfWriter writer, Rectangle rect, String
templatePath, String fileName) throws IOException, DocumentException {
// Get instruction document
String embed =
getClass().getClassLoader().getResource(templatePath).getFile();
// The fileName here is used to display in the attachment list of the
// document
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
writer, embed, fileName, null);
// The fileName here is used to display on the document
PdfAnnotation attachment =
PdfAnnotation.createFileAttachment(writer, rect, fileName, fs);
// Specify the width and height of the icon area
PdfAppearance app = writer.getDirectContent().createAppearance(200, 200);
String wordIcon =
getClass().getClassLoader().getResource(WORD_ICON).getFile();
Image img = Image.getInstance(wordIcon);
img.scaleAbsolute(200, 200);
img.setAbsolutePosition(0, 0);
app.addImage(img);
attachment.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);
writer.addAnnotation(attachment);
}
以上代码将被服务调用 class:
Rectangle rect = new Rectangle(220, 620, 250, 640);
createAttachment(pdfWriter, rect, "pdf/attachment.doc",
"attachment.doc");
它在我的 IDE 下正常工作,但是一旦我将它们制作成 jar(包括附件),网络应用程序似乎无法 found/attached 附件。并告诉我无法在系统路径中找到该文件。
我知道这可能是与 class 路径检测机制(例如 class 加载程序和加载方法)有关的问题,但它确实阻止了我进一步前进,因为我没有找到任何可能的解决方案。
异常:
java.io.FileNotFoundException: C:\Projects\work\test-1.3.6.0-
SNAPSHOT.jar!\pdf\attachment.doc (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at java.net.URL.openStream(URL.java:1045)
at com.lowagie.text.pdf.PdfFileSpecification.fileEmbedded(Unknown Source)
at com.lowagie.text.pdf.PdfFileSpecification.fileEmbedded(Unknown Source)
at com.lowagie.text.pdf.PdfFileSpecification.fileEmbedded(Unknown Source)
at example.pdf.Implementation.createAttachment
问题已解决。 唯一的区别是采用输入流而不是文件名,class PdfFileSpecification 有这样的接口。
InputStream embedInputStream =
getClass().getClassLoader().getResourceAsStream(templatePath);
byte[] buffer = new byte[1024];
embedOutputStream = new ByteArrayOutputStream();
int len = -1;
while ((len = embedInputStream.read(buffer)) != -1) {
embedOutputStream.write(buffer, 0, len);
}
// The fileName here is used to display in the attachment list of the document
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
writer, null, fileName, embedOutputStream.toByteArray());
// The fileName here is used to display on the document
PdfAnnotation attachment =
PdfAnnotation.createFileAttachment(writer, rect, fileName, fs);