Chỉnh sửa Word trong Node SDK

Sử dụng REST API để chỉnh sửa Word trong JavaScript theo chương trình

Khi các nhà phát triển sửa đổi Word tài liệu bằng thư viện JavaScript này, những gì thực sự đang được chỉnh sửa là Mô hình Đối tượng Tài liệu (DOM). Do đó, hầu hết mọi thay đổi đều có thể được thực hiện đối với Word tài liệu được biểu thị dưới dạng DOM. Với Node SDK được cung cấp, nhà phát triển có thể dễ dàng chỉnh sửa tài liệu: sửa đổi văn bản, cập nhật bảng, thêm hình ảnh, v.v. Chỉ cần tải Word, thực hiện các thay đổi cần thiết theo chương trình và xuất kết quả sang cùng một hoặc bất kỳ định dạng lưu nào được hỗ trợ.

Xem đoạn mã

Chỉnh sửa Word trong JavaScript bằng REST API

Thư viện Node của chúng tôi cung cấp cho các nhà phát triển khả năng sửa đổi Word tài liệu trực tiếp bằng cách chỉnh sửa Mô hình đối tượng tài liệu (DOM), có nghĩa là không cần cài đặt thêm phần mềm.

Chỉnh sửa văn bản trong Word tệp bằng JavaScript

Trường hợp phổ biến nhất để chỉnh sửa Word tệp là chỉnh sửa văn bản. Với giải pháp phần mềm đã cho, bạn có thể thêm, sửa đổi hoặc xóa văn bản bằng JavaScript trong tài liệu.

Chỉnh sửa bảng trong Word tệp bằng JavaScript

Một tùy chọn chỉnh sửa Word phổ biến khác là chỉnh sửa bảng. Node SDK của chúng tôi cho phép bạn làm việc với các bảng và văn bản trong các ô của bảng.

Các nhà phát triển JavaScript có thể thêm hoặc xóa bảng và ô bảng, cũng như thêm, chỉnh sửa và xóa văn bản bên trong chúng.

Thêm hình ảnh vào Word tệp bằng JavaScript

Bên cạnh việc chỉnh sửa văn bản và bảng trong Word, có một tùy chọn phổ biến khác: thêm hình ảnh vào tài liệu trong JavaScript. Các nhà phát triển Node cũng có thể thêm một hình ảnh vào Word tệp bằng cách sử dụng DOM.

Chỉnh sửa Word theo chương trình

Hãy dùng thử Node SDK mạnh mẽ này và đánh giá một số tùy chọn trong chỉnh sửa Word tài liệu. Sử dụng ví dụ sau, tải Word tài liệu của bạn và thực hiện một số thay đổi: thêm văn bản, thêm bảng và ô bảng có văn bản hoặc chèn hình ảnh vào Word tài liệu:

Tải lên tài liệu bạn muốn sửa đổi
Mã vận hành
Tải lên hình ảnh bạn muốn chèn
Chọn định dạng mục tiêu từ danh sách
import * as fs from "fs";

const wordsApi = new WordsApi("####-####-####-####-####", "##################");

const requestDocument = fs.createReadStream("Input.docx");
const requestParagraph = new model.ParagraphInsert({
    text: "Morbi enim nunc faucibus a."
})
const insertParagraphRequest = new model.InsertParagraphOnlineRequest({
    document: requestDocument,
    paragraph: requestParagraph
});

wordsApi.insertParagraphOnline(insertParagraphRequest)
.then((insertParagraphResult) => {    
    const requestDocument = insertParagraphResult.Document.Values();
    const convertDocument = new model.ConvertDocumentRequest({
        document: requestDocument,
        format: "docx"
    });

    wordsApi.convertDocument(convertDocument)
    .then((convertDocumentResult) => {
        console.log("Result of ConvertDocument: ", convertDocumentResult);
    });
});
import * as fs from "fs";

const wordsApi = new WordsApi("####-####-####-####-####", "##################");

const requestDocument = fs.createReadStream("Input.docx");
const requestTable = new model.TableInsert({
    columnsCount: 1,
    rowsCount: 2
})
const insertTableRequest = new model.InsertTableOnlineRequest({
    document: requestDocument,
    table: requestTable,
    nodePath: ""
});

wordsApi.insertTableOnline(insertTableRequest)
.then((insertTableResult) => {    
    const requestDocument = insertTableResult.Document.Values();
    const convertDocument = new model.ConvertDocumentRequest({
        document: requestDocument,
        format: "docx"
    });

    wordsApi.convertDocument(convertDocument)
    .then((convertDocumentResult) => {
        console.log("Result of ConvertDocument: ", convertDocumentResult);
    });
});
import * as fs from "fs";

const wordsApi = new WordsApi("####-####-####-####-####", "##################");

const requestDocument = fs.createReadStream("Input1.docx");
const requestDrawingObject = new model.DrawingObjectInsert({
    height: 0,
    left: 0,
    top: 0,
    width: 0,
    relativeHorizontalPosition: model.DrawingObjectInsert.RelativeHorizontalPositionEnum.Margin,
    relativeVerticalPosition: model.DrawingObjectInsert.RelativeVerticalPositionEnum.Margin,
    wrapType: model.DrawingObjectInsert.WrapTypeEnum.Inline
})
const requestImageFile = fs.createReadStream("Input2.docx");
const insertDrawingObjectRequest = new model.InsertDrawingObjectOnlineRequest({
    document: requestDocument,
    drawingObject: requestDrawingObject,
    imageFile: requestImageFile,
    nodePath: "sections/0"
});

wordsApi.insertDrawingObjectOnline(insertDrawingObjectRequest)
.then((insertDrawingObjectResult) => {    
    const requestDocument = insertDrawingObjectResult.Document.Values();
    const convertDocument = new model.ConvertDocumentRequest({
        document: requestDocument,
        format: "docx"
    });

    wordsApi.convertDocument(convertDocument)
    .then((convertDocumentResult) => {
        console.log("Result of ConvertDocument: ", convertDocumentResult);
    });
});
Mã vận hành
  
Sao chép mã JavaScript vào khay nhớ tạm

Cách Chỉnh sửa Word trong JavaScript

  1. Cài đặt 'Aspose. AdWords Cloud cho Node'
  2. Thêm tham chiếu thư viện (nhập thư viện) vào dự án JavaScript của bạn
  3. Tải Word để chỉnh sửa trong JavaScript
  4. Chèn nội dung vào đầu Word
  5. Tải xuống tài liệu kết quả từ bộ nhớ đám mây

JavaScript thư viện để sử dụng DOCX chỉnh sửa

Cài đặt 'Aspose.Words Cloud SDK for Node.js' bằng trình quản lý gói NPM. Chạy npm install asposewordscloud --save từ dòng lệnh để cài đặt SDK thông qua trình quản lý gói NPM. Thay vào đó, bạn có thể sao chép thủ công Aspose.Words Cloud SDK for Node.js từ GitHub và sử dụng nó trong dự án của mình. Vui lòng làm theo các Instructions này để nhanh chóng nhận được thông tin xác thực bảo mật cần thiết và truy cập REST API của chúng tôi.

yêu cầu hệ thống

  • @types/request (version 2.48.3+)
  • lodash (version 4.17.15+)
  • lodash.template (version 4.5.0+)
  • request (version 2.88.0+)
  • request-debug (version 0.2.0+)

Tham khảo Repository Documentation để xem thêm chi tiết.

Các định dạng tệp được hỗ trợ khác

Bạn có thể thực hiện thao tác chỉnh sửa cho các định dạng tệp khác:

5%

Đăng ký cập nhật sản phẩm của Aspose

Nhận bản tin hàng tháng và ưu đãi gửi trực tiếp đến hộp thư của bạn.

© Aspose Pty Ltd 2001-2024. Đã đăng ký Bản quyền.