javapdf合并
『壹』 java 生成PDF表格
实现代码如下:
package com.qhdstar.java.pdf;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
/**
* 描述:TODO 【JAVA生成PDF】
*
*
* @title GeneratePDF
* @version V1.0
*/
public class GeneratePDF {
public static void main(String[] args) {
//调用第一个方法,向C盘生成一个名字为ITextTest.pdf 的文件
try {
writeSimplePdf();
}
catch (Exception e) { e.printStackTrace(); }
//调用第二个方法,向C盘名字为ITextTest.pdf的文件,添加章节。
try {
writeCharpter();
}
catch (Exception e) { e.printStackTrace(); }
}
public static void writeSimplePdf() throws Exception {
// 1.新建document对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
// 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));
// 3.打开文档
document.open();
// 4.向文档中添加内容
// 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200))));
// 5.关闭文档
document.close();
}
/**
* 添加含有章节的pdf文件
*
* @throws Exception
*/
public static void writeCharpter() throws Exception {
// 新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));
// 打开文件
document.open();
// 标题
document.addTitle("Hello mingri example");
// 作者
document.addAuthor("wolf");
// 主题
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello mingri");
document.addCreator("My program using iText");
// document.newPage();
// 向文档中添加内容
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD, new Color(0, 0, 0))));
Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
// 新建章节
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
document.add(chapter1);
// 关闭文档
document.close();
}
}
『贰』 Java如何使用Java向PDF页面中添加文本
试试这个教程,需要依赖免费版的Spire.Pdf.jar包
importjava.awt.*;
importjava.awt.geom.Point2D;
importjava.awt.geom.Rectangle2D;
importjava.io.*;
importcom.spire.pdf.PdfPageBase;
importcom.spire.pdf.graphics.*;
{
publicstaticvoidmain(String[]args)throwsFileNotFoundException,IOException{
//创建PdfDocument对象
PdfDocumentdoc=newPdfDocument();
//添加一页
PdfPageBasepage=doc.getPages().add();
//标题文字
Stringtitle="标题";
//创建单色画刷对象
PdfSolidBrushbrush1=newPdfSolidBrush(newPdfRGBColor(Color.BLUE));
PdfSolidBrushbrush2=newPdfSolidBrush(newPdfRGBColor(Color.BLACK));
//创建TrueType字体对象
PdfTrueTypeFontfont1=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,14),true);
PdfTrueTypeFontfont2=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,10),true);
//创建PdfStringFormat对象
PdfStringFormatformat1=newPdfStringFormat();
format1.setAlignment(PdfTextAlignment.Center);//设置文字居中
//使用drawString方法绘制标题文字
page.getCanvas().drawString(title,font1,brush1,newPoint2D.Float(page.getActualBounds(true).width/2,0),format1);
//从txt文件读取内容到字符串
Stringbody=readFileToString("C:\Users\Administrator\Desktop\bodyText.txt");
//创建PdfStringFormat对象
PdfStringFormatformat2=newPdfStringFormat();
format2.setParagraphIndent(20);//设置段首缩进
//创建Rectangle2D对象
Rectangle2D.Floatrect=newRectangle2D.Float(0,30,page.getActualBounds(true).width,page.getActualBounds(true).height);
//使用drawString方法在矩形区域绘制主体文字
page.getCanvas().drawString(body,font2,brush2,rect,format2);
//保存到PDF文档
doc.saveToFile("ouput.pdf");
}
//自定义方法读取txt文件内容到字符串
(Stringfilepath)throwsFileNotFoundException,IOException{
StringBuildersb=newStringBuilder();
Strings="";
BufferedReaderbr=newBufferedReader(newFileReader(filepath));
while((s=br.readLine())!=null){
sb.append(s+" ");
}
br.close();
Stringstr=sb.toString();
returnstr;
}
}
『叁』 JAVA合并PDF文档后,显示的方向不对
楼主,你也说说是使用哪个组件做的吧,,,,,让人家方便找哪个.jar包。
『肆』 怎样把多个html文件合并成一个完整的pdf
打开html文件,打印pdf文件,再合并
『伍』 如何快速的合并pdf文件 java
将多个PDF文件合并到一起,首先需要有相关软件的帮助,那就是迅捷pdf合并软件。用户电脑上下载安装迅捷PDF合并软件,运行迅捷PDF软件,选择将文件合并为PDF,然后就是添加文件到软件中,支持多个PDF文件的批量转换,点击“添加文件”或者将文件直接拖拽到软件中,添加完成后点击右下角的“合并软件”即可。用户还可以选择合并后的文件的保存路径。只需稍等片刻,迅捷PDF合并软件就可以完成多个文件的合并,合并完成后会自动保存到你所自定义的文件夹中。
方法二:在PDF文件里:文档--插入页面,进入要选择插入的文件,选定后,选择拟插入文件的位置,最后确定即可,非常方便。
方法二:使用pdfFactory软件,到网上下载后,安装到电脑上,这时在“打印机和传真”里出现一个pdfFactory Pro的打印机。合并方法:1、首先将pdfFactory Pro的打印机设为默认打印机;2、选中需要合并的几个文件,点右键打印,出现打印任务;3、在任务里调整合并文件的顺序,最后保存PDF文件,合并文件结束。
『陆』 java itext 合并两个pdf可以做到吗
有兴趣去看开源项目 pdfsam
或者看下回这个例答子
http://itextpdf.com/examples/iia.php?id=123
『柒』 用JAVA 将PDF文档转换为TXT格式
用apache的tesseract将pdf读取出来整理到txt中,2个txt合并就好说了
『捌』 如何将两个PDF档案合并成一个PDF档案
首先打开一个文件,菜单栏--文档---插入页面,选择你要合并的文件确定即可。
『玖』 java itext转换PDF
public void GenerateAllParts() {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("d:\\all.pdf"));
// 生成字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
// 标题字体
Font f30 = new Font(bfChinese, 30, Font.NORMAL, Color.BLACK);
// 正文字体
Font f12 = new Font(bfChinese, 12, Font.NORMAL, Color.BLACK);
Font f6 = new Font(bfChinese, 6, Font.NORMAL, Color.BLACK);
Font f8 = new Font(bfChinese, 8, Font.NORMAL, Color.BLACK);
document.open();
// 标题
document.add(new Paragraph("报表实例", f30));
// 换行
document.add(new Chunk("\n\n"));
//
document.add(
new Paragraph(
new Chunk(".......................点击查看报表", f12)
.setLocalGoto("table")));
// 换行
document.add(new Chunk("\n\n"));
document.add(
new Paragraph(
new Chunk(".......................点击查看图片", f12)
.setLocalGoto("image")));
document.add(new Chunk("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"));
///////////////////////////////////////////////////
// 报表位置
document.add(new Chunk("报表实例", f12).setLocalDestination("table"));
// 添加table实例
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
PdfPCell cell = new PdfPCell();
cell.setBackgroundColor(new Color(213, 141, 69));
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
// 表格标题
cell.setPhrase(new Paragraph("标题一", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("标题二", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("标题三", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("标题四", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("标题五", f8));
table.addCell(cell);
// 表格数据
PdfPCell newcell = new PdfPCell();
newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
newcell.setPhrase(new Paragraph("数据一", f8));
table.addCell(newcell);
newcell.setPhrase(new Paragraph("数据二", f8));
table.addCell(newcell);
newcell.setPhrase(new Paragraph("数据三", f8));
table.addCell(newcell);
newcell.setPhrase(new Paragraph("数据四", f8));
table.addCell(newcell);
newcell.setPhrase(new Paragraph("数据五", f8));
table.addCell(newcell);
document.add(table);
////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// 添加连接
document.add(new Chunk("图片实例", f12).setLocalDestination("image"));
Image jpg = Image.getInstance("d:\\3.jpg");
document.add(jpg);
//////////////////////////////////////////////////////////
document.close();
} catch (Exception e) {
// TODO: handle exception
}
}