创建复杂PDF
上篇文章展示了使用Java和Aspose.PDF简单步骤。本文中,我们将使用Java和Aspose.PDF创建更复杂的文档结构。此示例中,我们将创建包含图片、两个文本片段(标题和段落)和表格。
创建文档参考以下步骤:
- 实例化一个Document对象。本步骤中将创建一个空的PDF文档,只包含一些metadata没有任何页面。
- 添加Page到Document对象。至此,文档有一个Page。
- 添加Image。
- 加载image流
- 添加图片到Page Resources的图片集合
- GSave:保存当前图行状态
- 创建一个Matrix对象
- ConcatenateMatrix:定义图像如何放置
- Do:绘制图像
- GRestore:恢复图形状态
- 创建一个header的文本片段。header使用Arial、24pt 字体并且居中。
- 添加header到页面段落中。
- 添加说明文本片段。使用Arial、24pt 字体并且居中。
- 添加说明文本片段到页面段落中。
- 创建table,设置table属性。
- 添加table到页面段落中。
10.保存文档。
具体代码如下:
package com.aspose.pdf.examples;
/**
* Complex Example
*/
import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.LocalTime;
import com.aspose.pdf.*;
import com.aspose.pdf.operators.ConcatenateMatrix;
import com.aspose.pdf.operators.Do;
import com.aspose.pdf.operators.GRestore;
import com.aspose.pdf.operators.GSave;
public final class ComplexExample {
private ComplexExample() {
}
private static Path _dataDir = Paths.get("/home/admin1/pdf-examples/");
public static void main(String[] args) throws FileNotFoundException {
// Initialize document object
Document document = new Document();
// Add page
Page page = document.getPages().add();
// -------------------------------------------------------------
// Add image
Path imageFileName = Paths.get(_dataDir.toString(),"logo.png");
java.io.FileInputStream imageStream = new java.io.FileInputStream(new java.io.File(imageFileName.toString()));
// Add image to Images collection of Page Resources
page.getResources().getImages().add(imageStream);
// Using GSave operator: this operator saves current graphics state
page.getContents().add(new GSave());
Rectangle _logoPlaceHolder = new Rectangle(20, 730, 120, 830);
// Create Matrix object
Matrix matrix = new Matrix(new double[] {
_logoPlaceHolder.getURX() - _logoPlaceHolder.getLLX(), 0, 0,
_logoPlaceHolder.getURY() - _logoPlaceHolder.getLLY(),
_logoPlaceHolder.getLLX(), _logoPlaceHolder.getLLY() });
// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
page.getContents().add(new ConcatenateMatrix(matrix));
XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());
// Using Do operator: this operator draws image
page.getContents().add(new Do(ximage.getName()));
// Using GRestore operator: this operator restores graphics state
page.getContents().add(new GRestore());
// -------------------------------------------------------------
// Add Header
TextFragment header = new TextFragment("New ferry routes in Fall 2020");
header.getTextState().setFont(FontRepository.findFont("Arial"));
header.getTextState().setFontSize(24);
header.setHorizontalAlignment (HorizontalAlignment.Center);
header.setPosition(new Position(130, 720));
page.getParagraphs().add(header);
// Add description
String descriptionText = "Visitors must buy tickets online and tickets are limited to 5,000 per day. Ferry service is operating at half capacity and on a reduced schedule. Expect lineups.";
TextFragment description = new TextFragment(descriptionText);
description.getTextState().setFont(FontRepository.findFont("Times New Roman"));
description.getTextState().setFontSize(14);
description.setHorizontalAlignment(HorizontalAlignment.Left);
page.getParagraphs().add(description);
// Add table
Table table = new Table();
table.setColumnWidths("200");
table.setBorder(new BorderInfo(BorderSide.Box, 1f, Color.getDarkSlateGray()));
table.setDefaultCellBorder(new BorderInfo(BorderSide.Box, 0.5f, Color.getBlack()));
table.getMargin().setBottom(10);
table.getDefaultCellTextState().setFont(FontRepository.findFont("Helvetica"));
Row headerRow = table.getRows().add();
headerRow.getCells().add("Departs City");
headerRow.getCells().add("Departs Island");
for (Cell headerRowCell : headerRow.getCells())
{
headerRowCell.setBackgroundColor(Color.getGray());
headerRowCell.getDefaultCellTextState().setForegroundColor(Color.getWhiteSmoke());
}
LocalTime time = LocalTime.of(6,0);
Duration incTime = Duration.ofMinutes(30);
for (int i = 0; i < 10; i++)
{
Row dataRow = table.getRows().add();
dataRow.getCells().add(time.toString());
time=time.plus(incTime);
dataRow.getCells().add(time.toString());
}
page.getParagraphs().add(table);
document.save(Paths.get(_dataDir.toString(), "Complex.pdf").toString());
}
}
代码执行结果如下: