「pdf拼接java」PDF拼接免费
本篇文章给大家谈谈pdf拼接java,以及PDF拼接免费对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java生成pdf几种常见方式
- 2、如何把两个PDF文件拼接在一起
- 3、java中如何实现向已有的PDF文件插入附件?
- 4、怎么把pdf文件拼接到一起
- 5、java生成pdf
- 6、Java PDFBox 能在PDF上添加标签吗?
java生成pdf几种常见方式
用Spire.PDF for Java来生成PDF文档的效果不错,支持格式化操作比较多,可以参考下 Java 中创建 PDF 文档
如何把两个PDF文件拼接在一起
如果我们的电脑里有两个单独的PDF文件,而这两个文件的类型非常接近,那么为了以后查看起来更方便,我们完全有必要将它们拼接(合并)到一起,这样不仅方便了以后的阅读,也整理了电脑上凌乱的文件,那么如何拼接呢?
01
拼接的方法有很多种,不过最简单的还是借助一款比较好的PDF阅读器,我给大家推荐一款名为“Adobe Acrobat DC”的软件,它是支持PDF拼接的...
02
安装到电脑以后,我们找到要拼接的那两个PDF文件,如图一;然后使用Adobe Acrobat DC打开其中任意一个文件进行查看;
03
在查看页面的右侧我们可以看到一项“合并文件”选项,点击以后会弹出添加文件的对话框,如图二...
04
点击对话框左下角的“添加文件”,然后将刚才剩下的那个PDF文件添加进来,如图所示...
05
添加完成以后,我们点击对话框右下角的“合并文件”,然后系统会自动生成一片名为“组合”的新的PDF文件,我们将其保存到自己需要的地方即可;
java中如何实现向已有的PDF文件插入附件?
可以用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文件拼接到一起
把pdf文件拼接到一起,首先打开任意一个pdf文件,进入页面,切换到页面选项栏,选择pdf合并,弹出窗口,点击添加文件。
将需要合并的文件添加进来,完成后输入输出名称和输出目录,点击开始合并,完成后,退出页面,即可看到合并好的文件。
java生成pdf
从JAVA直接读取EXCEL、WORD并生成PDF文件
1。操作EXCEL和WORD文件
使用JAVA从EXCEL、WORD文件中读写数据,可以使用 提供的JAVA API-Java Excel API,这里有其指南 ,可由此下载JAR文件 。
此API提供所有的读、写、修改、建立的功能。对于一般应用足够了。在任何支持JAVA的系统上使用。目前,JExcelApi尚不支持有关chart、graph 或者 macro的信息,但是会拷贝并保留这些信息。只支持PNG图像。
2。生成PDF文件
若从JAVA生成PDF文件,可以使用 提供的应用程序库jPDFWriter。jPDFWriter是一个类库,可以直接从JAVA的应用程序调用以产生PDF文件。其网站的下载文件中包含了说明和例子,有兴趣可以仔细研究。
3。把JExcelAPI和jPDFWriter结合起来,就可以在应用中直接把XLS文件转换成PDF文件。如果再结合Database,就可以直接把有关的报告数据生成XLS文件的同时生成PDF文件。
下边给出了一个例子,是针对XLS,虽然简单,但是完整,以此为基础,可以按照自己的需要展开工作了。对于WORD也是一样,所有不再另给了。
//
//Compile:
//javac -classpath .;jexcelapi/jxl.jar;jPDFWriter/jPDFWriter.jar xlsPrinter.java
//Run:
//java -classpath .;jexcelapi/jxl.jar;jPDFWriter/jPDFWriter.jar xlsPrinter
//
//
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.util.Vector;
import java.util.Date;
import jxl.*;//JExcelAPI
import com.qoppa.pdfPrinter.PDFPrinterJob;//jPDFWriter
public class xlsPrinter implements Printable
{
private int m_CurrentPage;
private int m_CurrentPageStartRow;
private int m_CurrentPageEndRow;
private final static int DEFAULT_COLUMN_WIDTH = 72;
private final static int CELL_MARGIN_X = 4;
private final static int CELL_MARGIN_Y = 4;
private Vector m_Data;
private int m_ColumnWidths [];
private boolean m_DrawGrid;
/**
* @param printString
*/
public xlsPrinter(Vector data, int [] colWidths, boolean drawGrid)
{
super();
m_Data = data;
m_ColumnWidths = colWidths;
m_DrawGrid = drawGrid;
}
//读取EXCEL数据并存入内存
private static Vector initData ()
{
Vector data = new Vector ();
try{
Workbook workbook = Workbook.getWorkbook(new File("output.xls"));
Sheet sheet = workbook.getSheet(0);
int rows = sheet.getRows();
int cols = sheet.getColumns();
// Initialize data
for (int row = 0; row rows; ++row){
Vector rowData = new Vector ();
for (int col = 0; col cols; ++col){
rowData.addElement (sheet.getCell(col,row).getContents());
}
data.addElement (rowData);
}
workbook.close();
}catch(Exception e){
e.printStackTrace();
}
return data;
}
//主程序
public static void main (String args [])
{
xlsPrinter gridPrinter = new xlsPrinter(initData (), null, true);
PrinterJob printerJob = PDFPrinterJob.getPrinterJob();
printerJob.setPrintable(gridPrinter);
try{
printerJob.print();
}catch (PrinterException pe){
pe.printStackTrace();
}
System.exit(0);
}
//打印接口,由此生成PDF文件,此段代码来源于jPDFWriter的例子
public int print (Graphics g, PageFormat pf, int pageIndex)
{
int lineHeight = g.getFontMetrics().getHeight();
// Reset current pos
int currentRow = 0;
if (pageIndex == 0)
{
// Need to do this in case the instance of this class
// gets used multiple times to print a string
m_CurrentPage = 0;
m_CurrentPageStartRow = 0;
}
// Need to do this because Java PrinterJob can call this
// method multiple times for the same page;
else if (m_CurrentPage == pageIndex)
{
currentRow = m_CurrentPageStartRow;
}
else
{
currentRow = m_CurrentPageEndRow + 1;
m_CurrentPageStartRow = currentRow;
}
// If we're out of lines, tell the PrinterJob we're done
if (currentRow = m_Data.size())
{
return Printable.NO_SUCH_PAGE;
}
// Loop through lines until we fill the page
int currentY = (int)(pf.getImageableY() + lineHeight);
while (currentRow m_Data.size()
currentY + lineHeight pf.getImageableY() + pf.getImageableHeight())
{
// Draw the next line
int currentX = (int)pf.getImageableX();
Vector nextRow = (Vector)m_Data.elementAt (currentRow);
for (int col = 0; col nextRow.size(); ++col)
{
String cellString = (String)nextRow.elementAt (col);
g.drawString (cellString, currentX + CELL_MARGIN_X, currentY + CELL_MARGIN_Y);
int colWidth = DEFAULT_COLUMN_WIDTH;
if (m_ColumnWidths != null m_ColumnWidths.length col)
{
colWidth = m_ColumnWidths [col];
}
// Draw grid if needed
if (m_DrawGrid)
{
g.drawRect (currentX, currentY - (lineHeight / 2), colWidth, lineHeight);
}
// Advance x
currentX += colWidth;
}
// Advance to the next line
++currentRow;
currentY += lineHeight;
}
// Save the ned line and current page
// Again, we have to do this because of multiple calls for the same page.
m_CurrentPageEndRow = currentRow;
m_CurrentPage = pageIndex;
return Printable.PAGE_EXISTS;
}
}
Java PDFBox 能在PDF上添加标签吗?
应该是能加东西的,PDFBox 提供了下面的功能:
(1)文本的提取
即在PDF文件里面把文本内容提取出来。
(2) 合并和拆分PDF文档
可以把几个PDF合并成一个PDF文件,也可以把一个PDF文件拆分成多个PDF文件。
(3) 表单填充
从PDF文件的表单中提取数据或者写入数据到PDF表单中。
(4) PDF/A的校验
根据PDF/A ISO 标准校验PDF文档
(5)打印PDF
通过Java的API去打印PDF文件
(6) 把PDF转换成图片
把PDF文件转换成图片
(7) 创建PDF文件
(8) PDF签名
关于pdf拼接java和PDF拼接免费的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-26,除非注明,否则均为
原创文章,转载请注明出处。