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
}
}