在 JavaScript 中製作 PDF 或 Word 文檔

使用我們的高保真 JavaScript 庫以編程方式創建幾乎任何格式的新文檔

使用我們的編程 API,Node.js via .NET 開發人員只需幾行代碼即可輕鬆製作 PDF、DOC、DOCX、HTML、EPUB 和許多其他格式的文檔。

查看代碼片段

使用 JavaScript 製作文檔

借助給定的強大 API,JavaScript 開發人員可以創建幾乎任何格式的文檔。為此,您需要使用我們的 Node.js via .NET 庫執行幾個步驟:

  1. 添加文件名
  2. 開始使用 JavaScript 創建文檔
  3. 以選定的格式保存創建的文檔

值得注意的是,從技術上講,一個空白文檔應該包含一個段落,因此當您以編程方式創建文檔時,您將獲得準確的基本文檔結構。

請注意,您可以立即將內容添加到新創建的文檔中。因此,您將獲得的不僅僅是一個空文檔,而是一個包含必要內容的文檔。有關如何編輯文檔的更多信息,請參閱編輯頁面。

以編程方式在 JavaScript 中創建文檔

給定的 Node.js via .NET 庫允許您以任何受支持的格式(PDF、DOCX、DOC、RTF、ODT、EPUB、HTML 等)以編程方式創建文檔。

試試我們強大的功能,看看如何使用以下示例創建某些格式的文檔:

使用 JavaScript 創建一個新文檔
從列表中選擇目標格式
運行代碼
npm install @aspose/words
複製
const aw = require('@aspose/words');

var doc = new aw.Document()
var builder = new aw.DocumentBuilder(doc)

font = builder.font
font.name = "Courier New"
font.color = '#FF0000'
font.size = 36
font.highlightColor = '#F0DB4F'

builder.write("Morbi enim nunc faucibus a.")

doc.save("Output.docx")
const aw = require('@aspose/words'); var doc = new aw.Document() var builder = new aw.DocumentBuilder(doc) var firstRun = new aw.Run(doc, "Proin eros metus, sagittis sed. ") var secondRun = new aw.Run(doc, "Morbi enim nunc faucibus a.") doc.firstSection.body.firstParagraph.appendChild(firstRun) doc.firstSection.body.firstParagraph.appendChild(secondRun) builder.moveTo(secondRun) builder.startBookmark("Aspose bookmark") // 如果 NextSibling 為 null,則很可能這是該段落中的最後一次運行。 if (secondRun.nextSibling != null) builder.moveTo(secondRun.nextSibling) else builder.moveTo(secondRun.parentParagraph) builder.endBookmark("Aspose bookmark") doc.save("Output.docx") const aw = require('@aspose/words'); var doc = new aw.Document() var builder = new aw.DocumentBuilder(doc) var firstRun = new aw.Run(doc, "Proin eros metus, sagittis sed. ") var secondRun = new aw.Run(doc, "Morbi enim nunc faucibus a.") doc.firstSection.body.firstParagraph.appendChild(firstRun) doc.firstSection.body.firstParagraph.appendChild(secondRun) builder.moveTo(secondRun) builder.startBookmark("Aspose bookmark") // 如果 NextSibling 為 null,則很可能這是該段落中的最後一次運行。 if (secondRun.nextSibling != null) builder.moveTo(secondRun.nextSibling) else builder.moveTo(secondRun.parentParagraph) builder.endBookmark("Aspose bookmark") var saveOptions = new aw.Saving.PdfSaveOptions() saveOptions.outlineOptions.bookmarksOutlineLevels.add("Aspose bookmark", 1); doc.save("Output.docx", saveOptions)
const aw = require('@aspose/words');

var doc = new aw.Document()

var run = new aw.Run(doc, "Proin eros metus, sagittis sed.")
var para = doc.firstSection.body.firstParagraph
para.appendChild(run)

var comment = new aw.Comment(doc)
comment.author = "John Doe"
comment.initial = "JD"
comment.dateTime = new Date()
comment.setText("Quisque fringilla leo.")

var commentRangeStart = new aw.CommentRangeStart(doc, comment.id)
var commentRangeEnd = new aw.CommentRangeEnd(doc, comment.id)

run.parentNode.insertBefore(commentRangeStart, run)
run.parentNode.insertAfter(commentRangeEnd, run)
commentRangeEnd.parentNode.insertAfter(comment, commentRangeEnd)

comment.addReply("Jane Doe", "JD", new Date(), "Pellentesque vel sapien justo.")

doc.save("Output.docx")
const aw = require('@aspose/words'); var doc = new aw.Document() var builder = new aw.DocumentBuilder(doc) var firstRun = new aw.Run(doc, "Proin eros metus, sagittis sed. ") var secondRun = new aw.Run(doc, "Morbi enim nunc faucibus a.") doc.firstSection.body.firstParagraph.appendChild(firstRun) doc.firstSection.body.firstParagraph.appendChild(secondRun) builder.moveTo(secondRun) builder.startBookmark("Aspose bookmark") // 如果 NextSibling 為 null,則很可能這是該段落中的最後一次運行。 if (secondRun.nextSibling != null) builder.moveTo(secondRun.nextSibling) else builder.moveTo(secondRun.parentParagraph) builder.endBookmark("Aspose bookmark") doc.save("Output.docx") const aw = require('@aspose/words'); var doc = new aw.Document() var builder = new aw.DocumentBuilder(doc) var firstRun = new aw.Run(doc, "Proin eros metus, sagittis sed. ") var secondRun = new aw.Run(doc, "Morbi enim nunc faucibus a.") doc.firstSection.body.firstParagraph.appendChild(firstRun) doc.firstSection.body.firstParagraph.appendChild(secondRun) builder.moveTo(secondRun) builder.startBookmark("Aspose bookmark") // 如果 NextSibling 為 null,則很可能這是該段落中的最後一次運行。 if (secondRun.nextSibling != null) builder.moveTo(secondRun.nextSibling) else builder.moveTo(secondRun.parentParagraph) builder.endBookmark("Aspose bookmark") var saveOptions = new aw.Saving.PdfSaveOptions() saveOptions.outlineOptions.bookmarksOutlineLevels.add("Aspose bookmark", 1); doc.save("Output.docx", saveOptions)
const aw = require('@aspose/words');

var doc = new aw.Document()
var builder = new aw.DocumentBuilder(doc)

var firstRun = new aw.Run(doc, "Proin eros metus, sagittis sed. ")
var secondRun = new aw.Run(doc, "Morbi enim nunc faucibus a.")
doc.firstSection.body.firstParagraph.appendChild(firstRun)
doc.firstSection.body.firstParagraph.appendChild(secondRun)

builder.moveTo(secondRun)
builder.startBookmark("Aspose bookmark")
// 如果 NextSibling 為 null,則很可能這是該段落中的最後一次運行。
if (secondRun.nextSibling != null)
    builder.moveTo(secondRun.nextSibling)
else
    builder.moveTo(secondRun.parentParagraph)
builder.endBookmark("Aspose bookmark")

doc.save("Output.docx")
const aw = require('@aspose/words'); var doc = new aw.Document() var builder = new aw.DocumentBuilder(doc) var firstRun = new aw.Run(doc, "Proin eros metus, sagittis sed. ") var secondRun = new aw.Run(doc, "Morbi enim nunc faucibus a.") doc.firstSection.body.firstParagraph.appendChild(firstRun) doc.firstSection.body.firstParagraph.appendChild(secondRun) builder.moveTo(secondRun) builder.startBookmark("Aspose bookmark") // 如果 NextSibling 為 null,則很可能這是該段落中的最後一次運行。 if (secondRun.nextSibling != null) builder.moveTo(secondRun.nextSibling) else builder.moveTo(secondRun.parentParagraph) builder.endBookmark("Aspose bookmark") doc.save("Output.docx") const aw = require('@aspose/words'); var doc = new aw.Document() var builder = new aw.DocumentBuilder(doc) var firstRun = new aw.Run(doc, "Proin eros metus, sagittis sed. ") var secondRun = new aw.Run(doc, "Morbi enim nunc faucibus a.") doc.firstSection.body.firstParagraph.appendChild(firstRun) doc.firstSection.body.firstParagraph.appendChild(secondRun) builder.moveTo(secondRun) builder.startBookmark("Aspose bookmark") // 如果 NextSibling 為 null,則很可能這是該段落中的最後一次運行。 if (secondRun.nextSibling != null) builder.moveTo(secondRun.nextSibling) else builder.moveTo(secondRun.parentParagraph) builder.endBookmark("Aspose bookmark") var saveOptions = new aw.Saving.PdfSaveOptions() saveOptions.outlineOptions.bookmarksOutlineLevels.add("Aspose bookmark", 1); doc.save("Output.docx", saveOptions)
const aw = require('@aspose/words');

var doc = new aw.Document()
var builder = new aw.DocumentBuilder(doc)

shape = builder.insertChart(aw.Drawing.Charts.ChartType.Pie, 432, 252)
chart = shape.chart
chart.title.text = "Demo Chart"

chart.series.clear()
chart.series.add("Series 1",
    ["Category1", "Category2", "Category3"],
    [2.7, 3.2, 0.8])

doc.save("Output.docx")
const aw = require('@aspose/words'); var doc = new aw.Document() var builder = new aw.DocumentBuilder(doc) var firstRun = new aw.Run(doc, "Proin eros metus, sagittis sed. ") var secondRun = new aw.Run(doc, "Morbi enim nunc faucibus a.") doc.firstSection.body.firstParagraph.appendChild(firstRun) doc.firstSection.body.firstParagraph.appendChild(secondRun) builder.moveTo(secondRun) builder.startBookmark("Aspose bookmark") // 如果 NextSibling 為 null,則很可能這是該段落中的最後一次運行。 if (secondRun.nextSibling != null) builder.moveTo(secondRun.nextSibling) else builder.moveTo(secondRun.parentParagraph) builder.endBookmark("Aspose bookmark") doc.save("Output.docx") const aw = require('@aspose/words'); var doc = new aw.Document() var builder = new aw.DocumentBuilder(doc) var firstRun = new aw.Run(doc, "Proin eros metus, sagittis sed. ") var secondRun = new aw.Run(doc, "Morbi enim nunc faucibus a.") doc.firstSection.body.firstParagraph.appendChild(firstRun) doc.firstSection.body.firstParagraph.appendChild(secondRun) builder.moveTo(secondRun) builder.startBookmark("Aspose bookmark") // 如果 NextSibling 為 null,則很可能這是該段落中的最後一次運行。 if (secondRun.nextSibling != null) builder.moveTo(secondRun.nextSibling) else builder.moveTo(secondRun.parentParagraph) builder.endBookmark("Aspose bookmark") var saveOptions = new aw.Saving.PdfSaveOptions() saveOptions.outlineOptions.bookmarksOutlineLevels.add("Aspose bookmark", 1); doc.save("Output.docx", saveOptions)
運行代碼

如何在JavaScript中製作文檔

  1. Aspose.Words for Node.js via .NET
  2. 將庫引用(導入庫)添加到您的 JavaScript 項目
  3. 創建一個新文檔
  4. 調用"save()"方法,傳遞文件名
  5. 將結果作為單獨的文件獲取

JavaScript 創建文檔的庫

We host our Node.js via .Net packages in NPM repositories. Please follow the step-by-step instructions on how to install "Aspose.Words for Node.js via .NET" to your developer environment.

System Requirements

This package is compatible with Node.js 14.17.0 or higher.

最流行的文件格式

5%

訂閱 Aspose 產品更新

獲取直接發送到您的郵箱的每月通訊和優惠。

© Aspose Pty Ltd 2001-2024. 版權所有。