
可以用Spire.Pdf for Java类库给PDF文档添加附件,下面的代码是插入Excel和Word附件给你参考:
import com.spire.pdf.annotations.*
import com.spire.pdf.attachments.PdfAttachment
import com.spire.pdf.graphics.*
import java.awt.*
import java.awt.geom.Dimension2D
import java.awt.geom.Rectangle2D
import java.io.File
import java.io.FileInputStream
import java.io.IOException
public class AttachFiles {
public static void main(String[] args) throws IOException {
//创建PdfDocument对象
PdfDocument doc = new PdfDocument()
//加载PDF文档
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf")
//添加附件到PDF
PdfAttachment attachment = new PdfAttachment("C:\\Users\\Administrator\\Desktop\\使用说明书.docx")
doc.getAttachments().add(attachment)
//绘制标签
String label = "财务报表.xlsx"
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true)
double x = 35
double y = doc.getPages().get(0).getActualSize().getHeight() - 200
doc.getPages().get(0).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y)
//添加注释附件到PDF
String filePath = "C:\\Users\\Administrator\\Desktop\\财务报表.xlsx"
byte[] data = toByteArray(filePath)
Dimension2D size = font.measureString(label)
Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 2), (float) y, 10, 15)
PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data)
annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)))
annotation.setFlags(PdfAnnotationFlags.Default)
annotation.setIcon(PdfAttachmentIcon.Graph)
annotation.setText("点击打开财务报表.xlsx")
doc.getPages().get(0).getAnnotationsWidget().add(annotation)
//保存文档
doc.saveToFile("Attachments.pdf")
}
//读取文件到byte数组
public static byte[] toByteArray(String filePath) throws IOException {
File file = new File(filePath)
long fileSize = file.length()
if (fileSize >Integer.MAX_VALUE) {
System.out.println("file too big...")
return null
}
FileInputStream fi = new FileInputStream(file)
byte[] buffer = new byte[(int) fileSize]
int offset = 0
int numRead = 0
while (offset <buffer.length &&(numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead
}
if (offset != buffer.length) {
throw new IOException("Could not completely read file "
+ file.getName())
}
fi.close()
return buffer
}
}
效果:
为pdf文件添加书签的方法有很多,下面给你介绍一种:启动Adobe Reader,随便打开一个PDF文件,单击菜单“编辑/首选项”,打开相应的对话框。 在左侧单击“Java script ”项,在右侧勾选 “启用Acrobat Java script (J)”、 “启用菜单项目Java script 执行权限(M)” “启用全局对象安全性策略” 单击“确定”按钮
此时,单击菜单“视图”,在出现的级联菜单中。 你会发现多了“Bookmark This Page” “Go To Bookmark”等四个与书签有关的命令。 以后如果遇到需要插入书签的时候,只需在文档中选中相应的部分,单击“Bookmark This Page”命令,然后在出现的“Bookmark Name” (书签名)对话框中,输入书签名称,单击“确定”按钮,即可完成书签的添加工作。
以后要快速定位到书签时,只需在“视图”菜单中选择“Go To Bookmark”命令,然后从d出的对话框中选择相应的书签名称。 Adobe Reader就会自动转到对应的部分,供你阅读或标注了。
删除书签。如果仅仅想删除某一个或几个书签,可以在“视图”菜单下选择“Remove a Bookmark”(删除书签)命令,并在出现的对话框中选择不想继续使用的书签即可。 如果要一次性地删除所有书签,可在“视图”菜单下选择“Clear Bookmark”(清理书签)命令。
可以用生成PDF报表的Java组件--iText。具体实现方法如下:1、导入itext-2。1。5。jar跟itextasian-1。5。2。jar两个包到项目里,2、建立一个pdf文件。
一般情况下,iText使用在有以下一个要求的项目中:1。内容无法提前利用:取决于用户的输入或实时的数据库信息。2。由于内容,页面过多,PDF文档不能手动生成。3。文档需在无人参与,批处理模式下自动创建。4。内容被定制或个性化。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)