com.itextpdf.io.font 的maven 地址
时间: 2023-08-31 16:32:32 浏览: 210
### 回答1:
`com.itextpdf.io.font` 是 iText PDF 库的一部分,其Maven坐标为:
```xml
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.13.2</version>
</dependency>
```
注意,该版本已经比较旧,如果您使用的是最新版本的 iText,可能需要更新该依赖项的版本号。
此外,`com.itextpdf.io.font` 在 iText 7 中已经被删除,不再提供支持。如果您正在使用 iText 7,您可能需要查找其他解决方案来处理字体。
### 回答2:
com.itextpdf.io.font的maven地址是https://mvnrepository.com/artifact/com.itextpdf/io-font。
### 回答3:
com.itextpdf.io.font的Maven地址是https://mvnrepository.com/artifact/com.itextpdf/io.font.
相关问题
java使用itext库创建pdf并生成目录
使用iText库可以方便地创建PDF文档并生成目录。
首先需要导入iText库,可以通过Maven或Gradle方式导入。
接着可以创建一个PdfDocument对象,并设置文档属性:
```java
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output.pdf"));
Document document = new Document(pdfDoc);
pdfDoc.setTagged();
pdfDoc.getCatalog().setLang(new PdfString("en-us"));
pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
document.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters() {
@Override
public boolean isSplitCharacter(GlyphLine text, int glyphPos) {
return false;
}
});
```
然后可以添加章节标题和内容:
```java
// 添加章节标题
Paragraph chapterTitle = new Paragraph("Chapter 1").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(16);
Chapter chapter = new Chapter(chapterTitle, 1);
document.add(chapter);
// 添加章节内容
Paragraph para1 = new Paragraph("This is the first paragraph.");
Paragraph para2 = new Paragraph("This is the second paragraph.");
document.add(para1);
document.add(para2);
```
最后可以生成目录:
```java
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new IEventHandler() {
@Override
public void handleEvent(Event event) {
try {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfPage page = docEvent.getPage();
PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
Rectangle rect = new Rectangle(36, 750, 523, 36);
Canvas canvas1 = new Canvas(canvas, pdfDoc, rect);
Paragraph p = new Paragraph().add("Table of Contents").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(12);
canvas1.add(p);
canvas1.close();
PdfOutline root = pdfDoc.getOutlines(false);
for (int i = 1; i <= 3; i++) {
PdfOutline chapterOutline = root.addOutline(String.format("Chapter %d", i));
chapterOutline.addDestination(PdfDestination.makeDestination(new PdfString(String.format("chapter_%d", i))));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
```
完整的代码示例可参考:
```java
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Chapter;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.AreaBreakType;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.renderer.DocumentRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;
import com.itextpdf.layout.renderer.ParagraphRenderer;
import com.itextpdf.layout.splitting.DefaultSplitCharacters;
import com.itextpdf.layout.splitting.GlyphLine;
import com.itextpdf.layout.splitting.ISplitCharacters;
import com.itextpdf.layout.splitting.SplitCharacters;
import com.itextpdf.layout.splitting.SplitCharacters.DefaultSplitCharacter;
import com.itextpdf.layout.splitting.SplitCharacters.SplitCharacter;
import java.io.IOException;
public class PdfGenerator {
public static void main(String[] args) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output.pdf"));
Document document = new Document(pdfDoc);
pdfDoc.setTagged();
pdfDoc.getCatalog().setLang(new PdfString("en-us"));
pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
document.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters() {
@Override
public boolean isSplitCharacter(GlyphLine text, int glyphPos) {
return false;
}
});
// 添加章节
Paragraph chapterTitle = new Paragraph("Chapter 1").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(16);
Chapter chapter = new Chapter(chapterTitle, 1);
chapter.setDestination("chapter_1");
document.add(chapter);
Paragraph para1 = new Paragraph("This is the first paragraph.");
Paragraph para2 = new Paragraph("This is the second paragraph.");
document.add(para1);
document.add(para2);
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
chapterTitle = new Paragraph("Chapter 2").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(16);
chapter = new Chapter(chapterTitle, 2);
chapter.setDestination("chapter_2");
document.add(chapter);
para1 = new Paragraph("This is the third paragraph.");
para2 = new Paragraph("This is the fourth paragraph.");
document.add(para1);
document.add(para2);
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
chapterTitle = new Paragraph("Chapter 3").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(16);
chapter = new Chapter(chapterTitle, 3);
chapter.setDestination("chapter_3");
document.add(chapter);
para1 = new Paragraph("This is the fifth paragraph.");
para2 = new Paragraph("This is the sixth paragraph.");
document.add(para1);
document.add(para2);
// 添加目录
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new IEventHandler() {
@Override
public void handleEvent(Event event) {
try {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfPage page = docEvent.getPage();
PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
Rectangle rect = new Rectangle(36, 750, 523, 36);
Canvas canvas1 = new Canvas(canvas, pdfDoc, rect);
Paragraph p = new Paragraph().add("Table of Contents").setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD)).setFontSize(12);
canvas1.add(p);
canvas1.close();
PdfOutline root = pdfDoc.getOutlines(false);
for (int i = 1; i <= 3; i++) {
PdfOutline chapterOutline = root.addOutline(String.format("Chapter %d", i));
chapterOutline.addDestination(PdfDestination.makeDestination(new PdfString(String.format("chapter_%d", i))));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
document.close();
}
}
```
在运行代码后,将会生成一个名为"output.pdf"的PDF文档,包含了三个章节和一个目录。
java生成pdf小条保存到本地,编码格式是utf-8
好的,您可以使用Java的iText库来生成和保存PDF文件。您可以按照以下步骤进行操作:
1. 添加iText库依赖,您可以在[iText官网](https://itextpdf.com/en/products/itext-7)下载最新版本的iText库,或者通过Maven或Gradle添加依赖。
2. 编写Java代码:
```
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class PdfGenerator {
public static void main(String[] args) throws IOException {
// 创建PDF文件
File outputFile = new File("example.pdf");
FileOutputStream outputStream = new FileOutputStream(outputFile);
PdfWriter writer = new PdfWriter(outputStream);
PdfDocument pdfDocument = new PdfDocument(writer);
Document document = new Document(pdfDocument, new PageSize(new Rectangle(200, 100)));
// 设置字体
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
// 添加内容
Paragraph title = new Paragraph("小条标题");
title.setFont(font).setFontSize(12).setBold();
title.setTextAlignment(TextAlignment.CENTER).setMarginBottom(10);
document.add(title);
Paragraph content = new Paragraph("这里是小条内容,可以自定义。");
content.setFont(font).setFontSize(10);
document.add(content);
// 关闭文档并保存到本地
document.close();
}
}
```
在上面的代码中,您需要将 'example.pdf' 替换为您要保存的PDF文件路径。
通过设置字体为HELVETICA和设置字符串编码为UTF-8,您可以确保生成的PDF文件是UTF-8编码的。
3. 运行代码并查看生成的PDF文件。
阅读全文