เพิ่มคำอธิบายประกอบในเอกสาร Word โดยใช้ REST API ใน Node.js

เราสามารถใส่คำอธิบายประกอบเอกสาร Word โดยทางโปรแกรมบนคลาวด์ได้อย่างง่ายดาย เราสามารถเพิ่มรูปภาพ ข้อคิดเห็น บันทึกย่อ หรือข้อสังเกตภายนอกประเภทอื่นๆ ลงในเอกสารเป็นคำอธิบายประกอบ ในบทความนี้ เราจะเรียนรู้วิธีเพิ่มคำอธิบายประกอบในเอกสาร Word โดยใช้ REST API ใน Node.js

หัวข้อต่อไปนี้จะครอบคลุมในบทความนี้:

คำอธิบายประกอบเอกสาร Word REST API และ Node.js SDK

สำหรับการเพิ่มคำอธิบายประกอบไฟล์ DOC หรือ DOCX เราจะใช้ API ของ Node.js SDK ของ GroupDocs.Annotation Cloud โปรดติดตั้งโดยใช้คำสั่งต่อไปนี้ในคอนโซล:

npm install groupdocs-annotation-cloud

โปรด รับรหัสลูกค้าและข้อมูลลับจากแดชบอร์ด ก่อนทำตามขั้นตอนดังกล่าว เมื่อคุณมี ID และรหัสลับของคุณแล้ว ให้เพิ่มรหัสตามที่แสดงด้านล่าง:

global.clientId = "659fe7da-715b-4744-a0f7-cf469a392b73"; 
global.clientSecret = "b377c36cfa28fa69960ebac6b6e36421";
global.myStorage = "";

const configuration = new groupdocs_annotation_cloud.Configuration(clientId, clientSecret);
configuration.apiBaseUrl = "https://api.groupdocs.cloud";

ใส่คำอธิบายประกอบเอกสาร Word โดยใช้ REST API ใน Node.js

เราสามารถเพิ่มคำอธิบายประกอบในไฟล์ DOCX โดยทำตามขั้นตอนง่าย ๆ ด้านล่าง:

อัปโหลดเอกสาร

ประการแรก เราจะอัปโหลดไฟล์ DOCX ไปยัง Cloud โดยใช้ตัวอย่างโค้ดที่ระบุด้านล่าง:

// เริ่มต้น API
var fileApi = new groupdocs_annotation_cloud.FileApi(configuration);
// เปิดไฟล์ใน IOStream จากในเครื่อง/ดิสก์
var resourcesFolder = 'C:\\Files\\Annotation\\sample.docx';
fs.readFile(resourcesFolder, (err, fileStream) => {
  // สร้างคำขออัปโหลด
  var request = new groupdocs_annotation_cloud.UploadFileRequest("sample.docx", fileStream, myStorage);
  // อัพโหลดไฟล์
  fileApi.uploadFile(request);
});

ด้วยเหตุนี้ ไฟล์ DOCX ที่อัปโหลดจะอยู่ใน ส่วนไฟล์ ของแดชบอร์ดบนระบบคลาวด์

เพิ่มคำอธิบายประกอบหลายรายการให้กับไฟล์ DOCX ใน Node.js

เราสามารถเพิ่มคำอธิบายประกอบหลายรายการในเอกสาร Word โดยทางโปรแกรมโดยทำตามขั้นตอนด้านล่าง:

  • ขั้นแรก สร้างอินสแตนซ์ของ AnnotateApi
  • จากนั้น สร้างอินสแตนซ์แรกของ AnnotationInfo
  • จากนั้น ตั้งค่าคุณสมบัติคำอธิบายประกอบสำหรับตัวอย่างแรก เช่น ตำแหน่ง ประเภท ข้อความ เป็นต้น
  • ทำซ้ำขั้นตอนข้างต้นเพื่อเพิ่มอินสแตนซ์ของ AnnotationInfo หลายรายการ เราจะตั้งค่าประเภทคำอธิบายประกอบที่แตกต่างกันและคุณสมบัติอื่นๆ สำหรับแต่ละอินสแตนซ์เพื่อเพิ่มคำอธิบายประกอบหลายรายการ
  • จากนั้น สร้างอินสแตนซ์ของ FileInfo และตั้งค่าเส้นทางไฟล์อินพุต
  • จากนั้น สร้างอินสแตนซ์ของ AnnotateOptions
  • ตอนนี้ กำหนด FileInfo และอินสแตนซ์คำอธิบายประกอบที่กำหนดให้กับ AnnotateOptions
  • ตั้งค่าเส้นทางไฟล์เอาต์พุตด้วย
  • หลังจากนั้น ให้เรียกใช้เมธอด AnnotateRequest ด้วย AnnotateOptions
  • สุดท้าย รับผลลัพธ์โดยเรียกเมธอด AnnotateApi.annotate() ด้วย AnnotateRequest

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มคำอธิบายประกอบหลายรายการในเอกสาร Word โดยใช้ REST API ใน Node.js

// เริ่มต้น API
let annotateApi = groupdocs_annotation_cloud.คำอธิบายApi.fromKeys(clientId, clientSecret);

// กำหนดคำอธิบายประกอบระยะทาง
let a1 = new groupdocs_annotation_cloud.AnnotationInfo();
a1.annotationPosition = new groupdocs_annotation_cloud.Point();
a1.annotationPosition.x = 1;
a1.annotationPosition.y = 1;
a1.box = new groupdocs_annotation_cloud.Rectangle();
a1.box.x = 100
a1.box.y = 100
a1.box.width = 200
a1.box.height = 100
a1.pageNumber = 0
a1.penColor = 1201033
a1.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a1.penWidth = 3
a1.opacity = 1
a1.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Distance;
a1.text = "This is distance annotation";
a1.creatorName = "Anonym A.";

// กำหนดคำอธิบายประกอบพื้นที่
let a2 = new groupdocs_annotation_cloud.AnnotationInfo();
a2.annotationPosition = new groupdocs_annotation_cloud.Point();
a2.annotationPosition.x = 1;
a2.annotationPosition.y = 1;
a2.box = new groupdocs_annotation_cloud.Rectangle();
a2.box.x = 80
a2.box.y = 400
a2.box.width = 200
a2.box.height = 100
a2.penColor = 1201033;
a2.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a2.pageNumber = 0;
a2.penWidth = 3;
a2.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Area;
a2.text = "This is area annotation";
a2.creatorName = "Anonym A.";

// กำหนดคำอธิบายประกอบ Arrow
let a3 = new groupdocs_annotation_cloud.AnnotationInfo();
a3.annotationPosition = new groupdocs_annotation_cloud.Point();
a3.annotationPosition.x = 1;
a3.annotationPosition.y = 1;
a3.box = new groupdocs_annotation_cloud.Rectangle();
a3.box.x = 100;
a3.box.y = 100;
a3.box.width = 200;
a3.box.height = 100;
a3.pageNumber = 0;
a3.penColor = 1201033;
a3.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a3.penWidth = 1;
a3.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Arrow;
a3.text = "This is arrow annotation";
a3.creatorName = "Anonym A.";

// กำหนดคำอธิบายประกอบวงรี
let a4 = new groupdocs_annotation_cloud.AnnotationInfo();
a4.annotationPosition = new groupdocs_annotation_cloud.Point();
a4.annotationPosition.x = 1;
a4.annotationPosition.y = 1;
a4.box = new groupdocs_annotation_cloud.Rectangle();
a4.box.x = 350;
a4.box.y = 350;
a4.box.width = 200;
a4.box.height = 100;
a4.pageNumber = 0;
a4.penColor = 1201033;
a4.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a4.penWidth = 4;
a4.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Ellipse;
a4.text = "This is ellipse annotation";
a4.creatorName = "Anonym A.";

// เส้นทางไฟล์อินพุต
let fileInfo = new groupdocs_annotation_cloud.FileInfo();
fileInfo.filePath = "sample.docx";

// กำหนดตัวเลือกคำอธิบายประกอบ
let options = new groupdocs_annotation_cloud.คำอธิบายOptions();
options.fileInfo = fileInfo;
options.annotations = [a1, a2, a3, a4];
options.outputPath = "AddMultipleAnnotations.docx";

// สร้างคำขอคำอธิบายประกอบ
let request = new groupdocs_annotation_cloud.คำอธิบายRequest(options);

// คำอธิบาย
let result = await annotateApi.annotate(request);
เพิ่มคำอธิบายประกอบหลายรายการให้กับไฟล์ DOCX ใน Node.js

เพิ่มคำอธิบายประกอบหลายรายการให้กับไฟล์ DOCX ใน Node.js

ดาวน์โหลดไฟล์คำอธิบายประกอบ

ตัวอย่างโค้ดด้านบนจะบันทึกเอกสาร Word (DOCX) ที่มีคำอธิบายประกอบไว้บนคลาวด์ สามารถดาวน์โหลดได้โดยใช้ตัวอย่างโค้ดต่อไปนี้:

// สร้าง FileApi
var fileApi = new groupdocs_annotation_cloud.FileApi(configuration);

// สร้างคำขอดาวน์โหลดไฟล์
let request = new groupdocs_annotation_cloud.DownloadFileRequest("AddMultipleAnnotations.docx", myStorage);
// ดาวน์โหลดไฟล์
let response = await fileApi.downloadFile(request);

// บันทึกไฟล์ในไดเร็กทอรีการทำงาน
fs.writeFile("C:\\Files\\Annotation\\AddMultipleAnnotations.docx", response, "binary", function (err) { });

เพิ่มคำอธิบายประกอบรูปภาพในเอกสาร Word โดยใช้ REST API ใน Node.js

เราสามารถเพิ่มคำอธิบายประกอบรูปภาพในเอกสาร Word โดยทางโปรแกรมโดยทำตามขั้นตอนด้านล่าง:

  • ขั้นแรก สร้างอินสแตนซ์ของ AnnotateApi
  • จากนั้น สร้างอินสแตนซ์ของ AnnotationInfo
  • จากนั้นกำหนดสี่เหลี่ยมผืนผ้าและกำหนดตำแหน่ง ความสูง และความกว้าง
  • หลังจากนั้นกำหนดคุณสมบัติของคำอธิบายประกอบ เช่น ตำแหน่ง ข้อความ ความสูง ความกว้าง เป็นต้น
  • จากนั้นตั้งค่าประเภทคำอธิบายประกอบเป็นรูปภาพ
  • จากนั้น สร้างอินสแตนซ์ของ FileInfo และตั้งค่าเส้นทางไฟล์อินพุต
  • จากนั้น สร้างอินสแตนซ์ของ AnnotateOptions
  • ตอนนี้ กำหนดวัตถุ FileInfo และคำอธิบายประกอบให้กับ AnnotateOptions
  • ตั้งค่าเส้นทางไฟล์เอาต์พุตด้วย
  • หลังจากนั้น สร้างคำขอโดยเรียกเมธอด AnnotateRequest กับอ็อบเจ็กต์ AnnotateOptions เป็นอาร์กิวเมนต์
  • สุดท้าย รับผลลัพธ์โดยเรียกเมธอด AnnotateApi.annotate() กับอ็อบเจ็กต์ AnnotateRequest

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มคำอธิบายประกอบรูปภาพในเอกสาร Word โดยใช้ REST API ใน Node.js โปรดทำตามขั้นตอนที่กล่าวถึงก่อนหน้านี้เพื่ออัปโหลดและดาวน์โหลดไฟล์

// เริ่มต้น API
let annotateApi = groupdocs_annotation_cloud.คำอธิบายApi.fromKeys(clientId, clientSecret);

// กำหนดคำอธิบายประกอบรูปภาพ
let a1 = new groupdocs_annotation_cloud.AnnotationInfo();
a1.annotationPosition = new groupdocs_annotation_cloud.Point();
a1.annotationPosition.x = 1;
a1.annotationPosition.y = 1;
a1.box = new groupdocs_annotation_cloud.Rectangle();
a1.box.x = 300;
a1.box.y = 320;
a1.box.width = 200;
a1.box.height = 40;
a1.pageNumber = 0;
a1.penColor = 1201033;
a1.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a1.penWidth = 1;
a1.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Image;
a1.text = "This is image annotation";
a1.creatorName = "Anonym A.";
a1.imagePath = "JohnSmith.png";

// เส้นทางไฟล์อินพุต
let fileInfo = new groupdocs_annotation_cloud.FileInfo();
fileInfo.filePath = "sample.docx";

// กำหนดตัวเลือกคำอธิบายประกอบ
let options = new groupdocs_annotation_cloud.คำอธิบายOptions();
options.fileInfo = fileInfo;
options.annotations = [a1];
options.outputPath = "AddImageAnnotation.docx";

// สร้างคำขอคำอธิบายประกอบ
let request = new groupdocs_annotation_cloud.คำอธิบายRequest(options);

// คำอธิบาย
let result = await annotateApi.annotate(request);
เพิ่มคำอธิบายประกอบรูปภาพในเอกสาร Word โดยใช้ REST API ใน Node.js

เพิ่มคำอธิบายประกอบรูปภาพในเอกสาร Word โดยใช้ REST API ใน Node.js

เพิ่มคำอธิบายประกอบฟิลด์ข้อความในเอกสาร Word โดยใช้ REST API ใน Node.js

เราสามารถเพิ่มคำอธิบายประกอบช่องข้อความในเอกสาร Word โดยทางโปรแกรมโดยทำตามขั้นตอนที่กล่าวถึงก่อนหน้านี้ อย่างไรก็ตาม เราจำเป็นต้องตั้งค่าประเภทคำอธิบายประกอบเป็น TextField

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มคำอธิบายประกอบช่องข้อความในเอกสาร Word โดยใช้ REST API ใน Node.js โปรดทำตามขั้นตอนที่กล่าวถึงก่อนหน้านี้เพื่ออัปโหลดและดาวน์โหลดไฟล์

// เริ่มต้น API
let annotateApi = groupdocs_annotation_cloud.คำอธิบายApi.fromKeys(clientId, clientSecret);

// กำหนดคำอธิบายประกอบฟิลด์ข้อความ
let a1 = new groupdocs_annotation_cloud.AnnotationInfo();
a1.annotationPosition = new groupdocs_annotation_cloud.Point();
a1.annotationPosition.x = 1;
a1.annotationPosition.y = 1;
a1.box = new groupdocs_annotation_cloud.Rectangle();
a1.box.x = 300;
a1.box.y = 310;
a1.box.width = 200;
a1.box.height = 50;
a1.pageNumber = 0;
a1.fontColor = 3093151;
a1.fontSize = 12;
a1.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.TextField;
a1.text = "Text field text";
a1.creatorName = "Anonym A.";

// เส้นทางไฟล์อินพุต
let fileInfo = new groupdocs_annotation_cloud.FileInfo();
fileInfo.filePath = "sample.docx";

// กำหนดตัวเลือกคำอธิบายประกอบ
let options = new groupdocs_annotation_cloud.คำอธิบายOptions();
options.fileInfo = fileInfo;
options.annotations = [a1];
options.outputPath = "AddTextFieldAnnotation.docx";

// สร้างคำขอคำอธิบายประกอบ
let request = new groupdocs_annotation_cloud.คำอธิบายRequest(options);

// คำอธิบาย
let result = await annotateApi.annotate(request);
เพิ่มคำอธิบายประกอบในเอกสาร Word โดยใช้ REST API ใน Node.js

เพิ่มคำอธิบายประกอบช่องข้อความในเอกสาร Word โดยใช้ REST API ใน Node.js

คำอธิบายประกอบลายน้ำในเอกสาร Word โดยใช้ REST API ใน Node.js

เราสามารถเพิ่มคำอธิบายประกอบลายน้ำในเอกสาร Word โดยทางโปรแกรมโดยทำตามขั้นตอนที่กล่าวถึงก่อนหน้านี้ อย่างไรก็ตาม เราจำเป็นต้องตั้งค่าประเภทคำอธิบายประกอบเป็นลายน้ำ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มคำอธิบายประกอบลายน้ำในเอกสาร Word โดยใช้ REST API ใน Node.js โปรดทำตามขั้นตอนที่กล่าวถึงก่อนหน้านี้เพื่ออัปโหลดและดาวน์โหลดไฟล์

// เริ่มต้น API
let annotateApi = groupdocs_annotation_cloud.คำอธิบายApi.fromKeys(clientId, clientSecret);

// กำหนดคำอธิบายประกอบลายน้ำ
let a1 = new groupdocs_annotation_cloud.AnnotationInfo();
a1.annotationPosition = new groupdocs_annotation_cloud.Point();
a1.annotationPosition.x = 1;
a1.annotationPosition.y = 1;
a1.box = new groupdocs_annotation_cloud.Rectangle();
a1.box.x = 100;
a1.box.y = 700;
a1.box.width = 500;
a1.box.height = 100;
a1.pageNumber = 0;
a1.penColor = 1201033;
a1.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a1.penWidth = 2;
a1.fontSize = 24;
a1.angle = 75;
a1.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Watermark;
a1.text = "This is a watermark annotation";
a1.creatorName = "Anonym A.";

// เส้นทางไฟล์อินพุต
let fileInfo = new groupdocs_annotation_cloud.FileInfo();
fileInfo.filePath = "sample.docx";

// กำหนดตัวเลือกคำอธิบายประกอบ
let options = new groupdocs_annotation_cloud.คำอธิบายOptions();
options.fileInfo = fileInfo;
options.annotations = [a1];
options.outputPath = "AddWatermarkAnnotation.docx";

// สร้างคำขอคำอธิบายประกอบ
let request = new groupdocs_annotation_cloud.คำอธิบายRequest(options);

// คำอธิบาย
let result = await annotateApi.annotate(request);
คำอธิบายประกอบลายน้ำในเอกสาร Word โดยใช้ REST API ใน Node.js

คำอธิบายประกอบลายน้ำในเอกสาร Word โดยใช้ REST API ใน Node.js

ลองออนไลน์

โปรดลองใช้เครื่องมือคำอธิบายประกอบ DOCX ออนไลน์ฟรีต่อไปนี้ ซึ่งพัฒนาโดยใช้ API ข้างต้น https://products.groupdocs.app/annotation/docx

บทสรุป

ในบทความนี้ เราได้เรียนรู้วิธีเพิ่มคำอธิบายประกอบในเอกสาร Word บนคลาวด์ เราได้เห็นวิธีเพิ่มคำอธิบายประกอบช่องรูปภาพและข้อความในไฟล์ DOCX โดยใช้ REST API ใน Node.js บทความนี้ยังอธิบายวิธีการอัปโหลดไฟล์ DOCX ทางโปรแกรมไปยังระบบคลาวด์ จากนั้นดาวน์โหลดไฟล์ที่แก้ไขแล้วจากระบบคลาวด์ นอกจากนี้ คุณสามารถเรียนรู้เพิ่มเติมเกี่ยวกับ GroupDocs.Annotation Cloud API โดยใช้ เอกสารประกอบ เรายังมีส่วน API Reference ที่ช่วยให้คุณแสดงภาพและโต้ตอบกับ API ของเราได้โดยตรงผ่านเบราว์เซอร์ ในกรณีที่มีความคลุมเครือ โปรดติดต่อเราได้ที่ ฟอรัม

ดูสิ่งนี้ด้วย