我正在尝试创建一个PDF文档,其中包含一些纵向页面和其他横向页面,但是看到这个示例(iText7 - 页面方向和旋转),我发现页面旋转到横向但文本也是这样(从iText7生成的PDF)样品),然后,我需要的页面旋转而文本从左边继续向右,接下来的图像如何英寸
注意:我尝试使用document.getPdfDocument().addNewPage(new PageSize(PageSize.A4.rotate()));
但它适用于一个页面,而不适用于下一个x页面.
我正在使用Itext7创建一个带有表格的pdf.
该表占用多个页面.
我还要重写单元格渲染器,将formfield添加到某个单元格.
我注意到当表转到新页面时,第一行不会触发CellRender类的draw方法.
我在下面放了一些代码以便更好地理解.
...
//adding the cells to table
for (int i = 0; i < 10; i++) {
addRow(table,i);
}
...
//addRow implementation
private void addRow(Table table, int row) throws IOException {
table.startNewRow();
PdfFont zapfdingbats = PdfFontFactory.createFont(FontConstants.ZAPFDINGBATS);
Cell checkBoxCell = new Cell().add(new Paragraph("o").setFont(zapfdingbats).setMargins(9, 0, 0, 11)).setKeepTogether(true);
checkBoxCell.setNextRenderer(new CheckboxCellRenderer(checkBoxCell, "cb"+row));
table.addCell(checkBoxCell);
...
//other cells here
}
...
//The implementation of the custom CellRender
protected class CheckboxCellRenderer extends CellRenderer {
protected String name;
public CheckboxCellRenderer(Cell modelElement, String name) { …
Run Code Online (Sandbox Code Playgroud) 我在段落对象中添加了以空格开头的Text对象,
但是在iText7(7.0.4)中删除了段落的空白。
看起来像左修剪。这是段落的规范吗?
有什么办法可以在文本前保留空格?
Paragraph p = new Paragraph().add(new Text(" abc")); // Only "abc" appears in pdf
Run Code Online (Sandbox Code Playgroud) 我可以按如下方式设置新段落的宽度,这会产生一定的高度:
Paragraph p = new Paragraph("some longer text some longer text some longer text");
p.setWidth(100);
System.out.println("height " + p.getHeight());
document.add(p);
Run Code Online (Sandbox Code Playgroud)
当然p.getHeight()
是null
,因为在渲染PDF文件期间计算渲染高度.但我需要在最终渲染之前的高度.我怎样才能最有效地获得它?
我正在尝试在 中实现页脚iText7
,页脚在文档的最后一页上应该有所不同,我添加了一个事件处理程序,该事件处理程序在文档关闭时调用,但尝试循环页面会导致空指针异常:
java.lang.NullPointerException
at com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:482)
at com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:152)
at com.itextpdf.kernel.pdf.PdfPage.newContentStream(PdfPage.java:777)
at com.itextpdf.kernel.pdf.PdfPage.newContentStreamBefore(PdfPage.java:212)
Run Code Online (Sandbox Code Playgroud)
这是我试图实现的代码:
private void onCloseDocument(
final PdfDocument document, final float xpos, final float ypos)
{
for (int i = 1; i <= document.getPdfDocument().getNumberOfPages(); i++)
{
final PdfPage page = document.getPdfDocument().getPage(i);
final PdfCanvas canvas =
new PdfCanvas(page.newContentStreamBefore(), page.getResources(), document.getPdfDocument());
canvas.beginText();
canvas.setFontAndSize(document.getFont(), FONT_SIZE);
canvas.setLeading(14.4F);
canvas.moveText(xpos, ypos);
if (i == document.getPdfDocument().getNumberOfPages())
{
canvas.showText("Last page");
}
else
{
canvas.showText("Another page");
}
canvas.endText();
canvas.stroke();
canvas.release();
}
}
Run Code Online (Sandbox Code Playgroud)
最初我认为这是有效的,因为我插入了一个区域分隔符以使pdf分割成两页,但是当我添加更多内容并且页面自然增加时,我得到了这个异常,类似地,我现在已经通过尝试插入多个进行了测试区域中断,我也遇到同样的异常。
注意* 我发现,如果immediateFlush
在创建文档时设置为 false,则不会出现此问题。 …
我是 Java 新手,我正在研究从 html 生成 pdf。因此,我使用iText7,我可以通过PdfWriter和Document生成一个普通的pdf文件,但我不能使用html2pdf来做到这一点。
这是我的 Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springexample</groupId>
<artifactId>PDFGenerator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>bean</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.0.4</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfa</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdftest</artifactId>
<version>7.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/html2pdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.1.4</version>
</dependency> …
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试使用 itext7 和 itext7.pdfhtml 将 HTML 转换为 PDF,但遇到了一个小问题。
我有一个固定的页脚 (.footer),在使用浏览器打开时效果很好,但在使用下面的代码进行转换时,div 并未固定到页面底部。该 div 位于其之前的其他 div 内容之后。
C#.net核心代码
string fullBody = System.IO.File.ReadAllText("index.html");
var stream = new MemoryStream();
var writer = new iText.Kernel.Pdf.PdfWriter(stream);
writer.SetCloseStream(false);
iText.Html2pdf.HtmlConverter.ConvertToPdf(fullBody , writer);
writer.Close();
stream.Seek(0, SeekOrigin.Begin);
Run Code Online (Sandbox Code Playgroud)
超文本标记语言
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="header">
<img src="header1.bmp" width="100%" />
<img src="header2.bmp" width="100%"/>
</div>
...
<div class="footer">
Fixed footer
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
CSS
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
text-align: center;
} …
Run Code Online (Sandbox Code Playgroud) 我使用 iTextSharp 来合并字节数组中的两个文档,如下所示:
using (MemoryStream ms = new MemoryStream())
using (Document doc = new Document())
using (PdfSmartCopy copy = new PdfSmartCopy(doc, ms))
{
// Open document
doc.Open();
// Create reader from bytes
using (PdfReader reader = new PdfReader(pdf1.DocumentBytes))
{
//Add the entire document instead of page-by-page
copy.AddDocument(reader);
}
// Create reader from bytes
using (PdfReader reader = new PdfReader(pdf2.DocumentBytes))
{
//Add the entire document instead of page-by-page
copy.AddDocument(reader);
}
// Close document
doc.Close();
// Return array
return ms.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
由于很多内容发生了变化,我无法将其转换为 …
在 iTextSharp 中,我们可以重写 OnCloseDocument() 事件并在文档页脚添加#total 的页码。但是,PdfDocument 不再有此文档关闭事件。既然我们在添加新页面时无法确定总页数,那么我们如何确定这个总页数并在生成文档时将其放在页脚上呢?
我见过一些使用暴力方法的建议:在生成 PDF 文档之后、刷新之前,使用 PdfReader 读取它以获取总数,然后再更新页脚。这是唯一的方法吗?有更好的方法吗?
谢谢。
我需要创建一个具有自定义彩色单元格和边框的表格。Color
类中定义了一些常量,但我需要自定义颜色。我需要#a6cb0b 作为带有颜色代码#cccccc 的标题和边框线的背景颜色。我如何设置它们?
Table table = new Table(new float[]{1,1,1});
Cell cell = new Cell();
cell.add(new Paragraph("TITLE"));
cell.setBackgroundColor(Color.???);
table.addCell(cell);
...
...
Run Code Online (Sandbox Code Playgroud)