這篇文章,主要介紹itext7操作PDF文檔之添加段落文本內容、添加List列表、添加Image圖片、添加Table表格。
目錄
一、itext7操作PDF內容
1.1、添加段落文本內容
1.2、添加列表內容
1.3、添加圖片
1.4、添加表格
(1)列寬采用點單位(pt點單位)
(2)采用百分比單位(%百分比)
一、itext7操作PDF內容
1.1、添加段落文本內容
itext中將文本抽象為一個Text對象,這個Text屬于葉子元素,不能直接添加到Document里面,必須先放入布局元素(layout元素)里面,然后再將布局元素加入到Document中。itext中采用Paragraph類表示段落,這是對一個段落文字的描述,例如:將Text對象先添加到Paragraph段落對象中,然后將Paragraph段落加入到Document里面。
package itext.demo.basic;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import java.io.FileNotFoundException;
/**
* @version 1.0.0
* @Date: 2023/7/19 15:40
* @Author ZhuYouBin
* @Description: 文本內容操作
*/
public class TextOperation {
public static void main(String[] args) throws FileNotFoundException {
// 創(chuàng)建PDF文檔
PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-content.pdf"));
// 創(chuàng)建文檔對象
Document document = new Document(pdfDocument);
// 創(chuàng)建文本對象
Text text = new Text("hello world");
// 創(chuàng)建段落
Paragraph paragraph = new Paragraph();
paragraph.add(text);
// 將段落添加到文檔上面
document.add(paragraph);
// 關閉文檔
document.close();
pdfDocument.close();
}
}
運行結果如下所示:
1.2、添加列表內容
itext中使用List類表示列表對象,列表可以有序列表、無序列表,列表中的每一項使用ListItem類表示,一個List列表可以包含多個ListItem列表項,List列表可以設置縮進、列表項的符號等。
package itext.demo.basic.text;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.ListItem;
import java.io.FileNotFoundException;
/**
* @version 1.0.0
* @Date: 2023/7/19 15:40
* @Author ZhuYouBin
* @Description: 添加列表內容
*/
public class TextOperation {
public static void main(String[] args) throws FileNotFoundException {
// 創(chuàng)建PDF文檔
PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-content.pdf"));
// 創(chuàng)建文檔對象
Document document = new Document(pdfDocument);
// 創(chuàng)建List列表對象
List list = new List();
list.setSymbolIndent(12); // 設置列表項和符號之間的縮進距離
list.setListSymbol("@"); // 設置列表項的符號
// 創(chuàng)建列表項
for (int i = 0; i < 5; i++) {
list.add(new ListItem("this is 00" + i + " item。"));
}
// 將List列表添加到文檔上面
document.add(list);
// 關閉文檔
document.close();
pdfDocument.close();
}
}
運行結果如下所示:
1.3、添加圖片
itext中將圖片抽象成一個Image對象,圖片可以從URL、File等來源進行創(chuàng)建,Image類中的構造方法是protected修飾的,所以不能直接使用new關鍵字進行創(chuàng)建對象,可以使用itext中提供的ImageDataFactory工具類,這個類中提供了一個create()方法可以根據不同的來源創(chuàng)建圖片對象。
package itext.demo.basic.text;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import java.net.URL;
/**
* @version 1.0.0
* @Date: 2023/7/19 15:40
* @Author ZhuYouBin
* @Description: 添加圖片
*/
public class TextOperation {
public static void main(String[] args) throws Exception {
// 創(chuàng)建PDF文檔
PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-content.pdf"));
// 創(chuàng)建文檔對象
Document document = new Document(pdfDocument);
// 創(chuàng)建圖片對象
URL url = new URL("https://www.toopic.cn/public/uploads/small/1658043292312165804329268.png");
Image image = new Image(ImageDataFactory.create(url));
image.setAutoScale(true); // 設置寬高字段縮放
URL url2 = new URL("https://www.toopic.cn/public/uploads/small/1658043887555165804388773.jpg");
Image image2 = new Image(ImageDataFactory.create(url2));
image2.setAutoScale(true); // 設置寬高字段縮放
// 將圖片添加到文檔上面
document.add(image);
document.add(image2);
// 關閉文檔
document.close();
pdfDocument.close();
}
}
運行結果如下所示(添加兩張圖片的效果):
1.4、添加表格
itext中將表格抽象成了Table類,表格就是一張二維表,由行和列組成,其中每一行每一列都是一個單元格,單元格使用Cell類表示。創(chuàng)建Table對象的時候,對應的構造方法必須指定表格中每一個單元格的寬度,列寬度的單位可以是pt、也可以設置百分比,推薦使用百分比單位。
(1)列寬采用點單位(pt點單位)
package itext.demo.basic.text;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
/**
* @version 1.0.0
* @Date: 2023/7/19 15:40
* @Author ZhuYouBin
* @Description: 添加表格【pt單位】
*/
public class TableDemo {
public static void main(String[] args) throws Exception {
// 創(chuàng)建PDF文檔
PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-table.pdf"));
// 創(chuàng)建文檔對象
Document document = new Document(pdfDocument);
// 創(chuàng)建表格
float[] columnWidths = new float[] {
30, 50, 60, 20
};
Table table = new Table(columnWidths);
// 設置表格寬度100%
table.setWidth(UnitValue.createPercentValue(100));
// 設置表格標題
table.setCaption(new Div().add(new Paragraph("this is a caption of table")));
// 添加表頭單元格,上面設置了四列,超過四列會自動換行
table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
// 添加普通單元格
table.addCell(new Cell().add(new Paragraph("cell")));
table.addCell(new Cell().add(new Paragraph("cell")));
table.addCell(new Cell().add(new Paragraph("cell")));
table.addCell(new Cell().add(new Paragraph("cell")));
// 添加表尾單元格
table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
// 添加表格到PDF文檔
document.add(table);
// 關閉文檔
document.close();
pdfDocument.close();
}
}
運行結果如下所示:
(2)采用百分比單位(%百分比)
package itext.demo.basic.text;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
/**
* @version 1.0.0
* @Date: 2023/7/19 15:40
* @Author ZhuYouBin
* @Description: 添加表格【百分比單位】
*/
public class TableDemo {
public static void main(String[] args) throws Exception {
// 創(chuàng)建PDF文檔
PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-table.pdf"));
// 創(chuàng)建文檔對象
Document document = new Document(pdfDocument);
// 創(chuàng)建百分比單位的列寬度
UnitValue[] columnWidths = new UnitValue[] {
UnitValue.createPercentValue(25),
UnitValue.createPercentValue(25),
UnitValue.createPercentValue(25),
UnitValue.createPercentValue(25)
};
Table table = new Table(columnWidths);
// 設置表格寬度100%
table.setWidth(UnitValue.createPercentValue(100));
// 設置表格標題
table.setCaption(new Div().add(new Paragraph("this is a caption of table")));
// 添加表頭單元格,上面設置了四列,超過四列會自動換行
table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
table.addHeaderCell(new Cell().add(new Paragraph("header-cell")));
// 添加普通單元格
table.addCell(new Cell().add(new Paragraph("cell")));
table.addCell(new Cell().add(new Paragraph("cell")));
table.addCell(new Cell().add(new Paragraph("cell")));
table.addCell(new Cell().add(new Paragraph("cell")));
// 添加表尾單元格
table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
table.addFooterCell(new Cell().add(new Paragraph("footer-cell")));
// 添加表格到PDF文檔
document.add(table);
// 關閉文檔
document.close();
pdfDocument.close();
}
}
運行結果如下所示:
到此,itext操作PDF內容之添加段落、列表、圖片、表格就介紹完啦。文章來源:http://www.zghlxwxcb.cn/news/detail-604939.html
綜上,這篇文章結束了,主要介紹itext7操作PDF文檔之添加段落文本內容、添加List列表、添加Image圖片、添加Table表格。文章來源地址http://www.zghlxwxcb.cn/news/detail-604939.html
到了這里,關于【itext7】itext7操作PDF文檔之添加段落文本內容、添加List列表、添加Image圖片、添加Table表格的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!