使用我们的 Java 库,开发人员可以轻松地从头开始制作 PDF。为此,Java 开发人员只需执行几个步骤:
值得注意的是,从技术上讲,空白文档应该包含一个段落,因此当您以编程方式创建 PDF 文档时,您将获得完全一致的基本文档结构。
请注意,您可以立即将内容添加到新创建的 PDF 文件中。因此,您将获得的不仅仅是一个空文档,而是一个包含必要内容的文档。有关如何编辑文档的更多信息,请参阅编辑页面。
此 Java 库允许您以编程方式创建 PDF 文档。试试我们强大的功能,看看如何使用以下示例以某些格式创建 PDF:
// Repository path: https://releases.aspose.com/java/repo/
// Maven, where 'ver' - Aspose.Words version number, for example, 24.4.
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>ver</version>
<classifier>jdk17</classifier>
</dependency>
复制
// Repository path: https://releases.aspose.com/java/repo/
// Gradle, where 'ver' - Aspose.Words version number, for example, 24.4.
compile(group: 'com.aspose', name: 'aspose-words', version: 'ver', classifier: 'jdk17')
复制
// Repository path: https://releases.aspose.com/java/repo/
// Ivy, where 'ver' - Aspose.Words version number, for example, 24.4.
<dependency org="com.aspose" name="aspose-words" rev="ver">
<artifact name="aspose-words" m:classifier="jdk17" ext="jar"/>
</dependency>
复制
// Repository path: https://releases.aspose.com/java/repo/
// Sbt, where 'ver' - Aspose.Words version number, for example, 24.4.
libraryDependencies += "com.aspose" % "aspose-words" % "ver"
复制
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Font font = builder.getFont();
font.setName("Courier New");
font.setColor(Color.BLUE);
font.setSize(36);
font.setHighlightColor(Color.YELLOW);
builder.write("Morbi enim nunc faucibus a.");
doc.save("Output.pdf");
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
Run secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun);
builder.moveTo(secondRun);
builder.startBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.getNextSibling() != null)
builder.moveTo(secondRun.getNextSibling());
else
builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");
doc.save("Output.pdf");
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
Run secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun);
builder.moveTo(secondRun);
builder.startBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.getNextSibling() != null)
builder.moveTo(secondRun.getNextSibling());
else
builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("Aspose bookmark", 1);
doc.save("Output.pdf", saveOptions);
import com.aspose.words.*;
Document doc = new Document();
Run run = new Run(doc, "Proin eros metus, sagittis sed.");
Paragraph para = doc.getFirstSection().getBody().getFirstParagraph();
para.appendChild(run);
Comment comment = new Comment(doc);
comment.setAuthor("John Doe");
comment.setInitial("JD");
comment.setDateTime(new Date());
comment.setText("Quisque fringilla leo.");
CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.getId());
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.getId());
run.getParentNode().insertBefore(commentRangeStart, run);
run.getParentNode().insertAfter(commentRangeEnd, run);
commentRangeEnd.getParentNode().insertAfter(comment, commentRangeEnd);
comment.addReply("Jane Doe", "JD", new Date(), "Pellentesque vel sapien justo.");
doc.save("Output.pdf");
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
Run secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun);
builder.moveTo(secondRun);
builder.startBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.getNextSibling() != null)
builder.moveTo(secondRun.getNextSibling());
else
builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");
doc.save("Output.pdf");
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
Run secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun);
builder.moveTo(secondRun);
builder.startBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.getNextSibling() != null)
builder.moveTo(secondRun.getNextSibling());
else
builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("Aspose bookmark", 1);
doc.save("Output.pdf", saveOptions);
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
Run secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun);
builder.moveTo(secondRun);
builder.startBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.getNextSibling() != null)
builder.moveTo(secondRun.getNextSibling());
else
builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("Aspose bookmark", 1);
doc.save("Output.pdf", saveOptions);
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
Run secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun);
builder.moveTo(secondRun);
builder.startBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.getNextSibling() != null)
builder.moveTo(secondRun.getNextSibling());
else
builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");
doc.save("Output.pdf");
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
Run secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun);
builder.moveTo(secondRun);
builder.startBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.getNextSibling() != null)
builder.moveTo(secondRun.getNextSibling());
else
builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("Aspose bookmark", 1);
doc.save("Output.pdf", saveOptions);
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertChart(ChartType.PIE, 432, 252);
Chart chart = shape.getChart();
chart.getTitle().setText("Demo Chart");
chart.getSeries().clear();
chart.getSeries().add("Series 1",
new String[] { "Category1", "Category2", "Category3" },
new double[] { 2.7, 3.2, 0.8 });
doc.save("Output.pdf");
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
Run secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun);
builder.moveTo(secondRun);
builder.startBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.getNextSibling() != null)
builder.moveTo(secondRun.getNextSibling());
else
builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");
doc.save("Output.pdf");
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Run firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
Run secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.getFirstSection().getBody().getFirstParagraph().appendChild(firstRun);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(secondRun);
builder.moveTo(secondRun);
builder.startBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.getNextSibling() != null)
builder.moveTo(secondRun.getNextSibling());
else
builder.moveTo(secondRun.getParentParagraph());
builder.endBookmark("Aspose bookmark");
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("Aspose bookmark", 1);
doc.save("Output.pdf", saveOptions);
我们在Maven存储库中托管我们的 Java 'Aspose.Words for Java' 是一个常见的包含字节码的 JAR 请按照有关如何将其安装到 Java 开发人员环境的分步说明进行操作。
Java SE 7 和更新的 Java 版本。 Java SE 6 提供了一个单独的包,以防您不得不使用这个过时的 JRE 。
我们的 Java 包是跨平台的,可以在所有具有 JVM 实现的 Microsoft Windows 、Linux、macOS、Android 和 iOS。
有关可选包依赖项的信息,例如 JogAmp JOGL 、 Harfbuzz 字体引擎、 Java Advanced Imaging JAI ,请参阅产品文档。