Spire.Doc for JavaScript 13.4.0 现已正式发布。最新版本支持了许多新功能,例如在 Word 文档中添加各种类型的图表。详情请阅读以下内容。
新功能:
- 支持在 Word 文档中添加多种类型的图表。
- 支持在 Word 文档中插入 SVG 图片。
- 支持提取 Word 文档中的图片。
- 支持移除图片水印。
- 支持移除文本水印。
- 支持 设置 Word 文档文档属性。
- 支持设置 Word 文档装订线的位置。
例如添加条形图:
// Load word document
let document = wasmModule.Document.Create();
// Add a section to the document
let section = document.AddSection();
// Add a paragraph to the section and append text to it
section.AddParagraph().AppendText("Bar chart.");
// Add a new paragraph to the section
let newPara = section.AddParagraph();
// Append a bar chart shape to the paragraph with specified width and height
let chartShape = newPara.AppendChart(wasmModule.ChartType.Bar, 400.0, 300.0);
let chart = chartShape.Chart;
// Get the title of the chart
let title = chart.Title;
// Set the text of the chart title
title.Text = "My Chart";
// Show the chart title
title.Show = true;
// Overlay the chart title on top of the chart
title.Overlay = true;
// Save the document to the specified path
const outputFileName = "AppendBarChart.docx";
document.SaveToFile({
fileName: outputFileName,
fileFormat: wasmModule.FileFormat.Docx2013,
});
//Open a Word document
let document = wasmModule.Document.Create();
let section = document.AddSection();
//Add a new paragraph
let para = section.AddParagraph();
//add a svg file to the paragraph
let svgPicture = para.AppendPicture({imgFile: inputSVGFileName});
//Set svg's width
svgPicture.Width = 200;
//Set svg's height
svgPicture.Height = 200;
// Save the document
const outputFileName = "AddSvg.docx";
document.SaveToFile({ fileName: outputFileName, fileFormat: wasmModule.FileFormat.Docx });
//open document
let document = wasmModule.Document.Create();
document.LoadFromFile(inputFileName);
//embedded images list.
let images = [];
for (let i = 0; i < document.Sections.Count; i++) {
let section = document.Sections.get(i);
for (let j = 0; j < section.Body.ChildObjects.Count; j++) {
let objInSec = section.Body.ChildObjects.get(j);
if (objInSec.DocumentObjectType == wasmModule.DocumentObjectType.Paragraph) {
for (let k = 0; k < objInSec.ChildObjects.Count; k++) {
let objInPara = objInSec.ChildObjects.get(k);
if (objInPara.DocumentObjectType == wasmModule.DocumentObjectType.Picture) {
images.push(objInPara.ImageBytes)
}
}
} else if (objInSec.DocumentObjectType == wasmModule.DocumentObjectType.Table) {
let table = objInSec;
for (let i = 0; i < table.Rows.Count; i++) {
let row = table.Rows.get(i);
for (let j = 0; j < row.Cells.Count; j++) {
let cell = row.Cells.get(j);
for (let k = 0; k < cell.Paragraphs.Count; k++) {
let paragraph = cell.Paragraphs.get_Item(k);
for (let a = 0; a < paragraph.ChildObjects.Count; a++) {
let para = paragraph.ChildObjects.get(a);
if (para.DocumentObjectType == wasmModule.DocumentObjectType.Picture) {
images.push(para.ImageBytes)
}
}
}
}
}
}
}
}
//save images
for (let i = 0; i < images.length; i++) {
let outFile = "Image-" + i + ".png";
const modifiedFile = new Blob([images[i]], { type: 'image/png'});
downloadItems.value.push({
name: outFile,
url: URL.createObjectURL(modifiedFile)
});
}
// Dispose of the document object to free resources
document.Dispose();
//Create Word document.
let document = wasmModule.Document.Create();
//Load the file from disk.
document.LoadFromFile(inputFileName);
//Set the watermark as null to remove the text and image watermark.
document.Watermark = null;
// Save the document to the specified path
const outputFileName = "RemoveImageWatermark.docx";
document.SaveToFile({fileName: outputFileName,fileFormat: wasmModule.FileFormat.Docx2013});
//Create Word document.
let document = wasmModule.Document.Create();
//Load the file from disk.
document.LoadFromFile(inputFileName);
//Set the watermark as null to remove the text and image watermark.
document.Watermark = null;
// Save the document to the specified path
const outputFileName = "RemoveTextWatermark.docx";
document.SaveToFile({fileName: outputFileName,fileFormat: wasmModule.FileFormat.Docx2013});
//Create a document
let document = wasmModule.Document.Create();
//Load the document from disk.
document.LoadFromFile(inputFileName);
//Set the build-in Properties.
document.BuiltinDocumentProperties.Title = "Document Demo Document";
document.BuiltinDocumentProperties.Author = "James";
document.BuiltinDocumentProperties.Company = "e-iceblue";
document.BuiltinDocumentProperties.Keywords = "Document, Property, Demo";
document.BuiltinDocumentProperties.Comments = "This document is just a demo.";
//Set the custom properties.
let custom = document.CustomDocumentProperties;
custom.Add("e-iceblue", true);
custom.Add("Authorized By", "John Smith");
custom.Add("Authorized Date", wasmModule.DateTime.get_Today());
custom.Add("Age", 13);
custom.Add("price", 25.4);
// Save the document to the specified path
const outputFileName = "SetDocumentProperties.docx";
document.SaveToFile({ fileName: outputFileName, fileFormat: wasmModule.FileFormat.Docx2013 });
//Create Word document.
let document = wasmModule.Document.Create();
//Load the file from disk.
document.LoadFromFile(inputFileName);
//Create a new section
let section = document.Sections.get(0);
// Set the top gutter option to true for the section's page setup.
section.PageSetup.IsTopGutter = true;
// Set the width of the gutter in points (100f).
section.PageSetup.Gutter = 100;
// Save the document to the specified path
const outputFileName = "SetGutterPosition.docx";
document.SaveToFile({ fileName: outputFileName, fileFormat: wasmModule.FileFormat.Docx2013 });
获取 Spire.Doc for JavaScript 13.4.0 请点击:
https://www.e-iceblue.cn/Downloads/Spire-DOC-Javascript.html