การเพิ่มความคิดเห็นอย่างรวดเร็วโดยตรงลงในเอกสาร Word เป็นความต้องการทั่วไปสำหรับกระบวนการตรวจสอบแบบร่วมมือ GroupDocs.Annotation Cloud SDK for Node.JS ช่วยให้คุณสามารถเพิ่มคำอธิบายที่หลากหลายโดยโปรแกรมได้โดยไม่ต้องออกจากสภาพแวดล้อมเซิร์ฟเวอร์ของคุณ ในคู่มือนี้เราจะสาธิตวิธีสร้าง Sticky Notes บนหน้า Word ใน Node.JS รวมถึงการตั้งค่า การอธิบายอย่างละเอียด ตัวอย่างโค้ดเต็มรูปแบบ และคำสั่ง cURL ที่เทียบเท่า

การตั้งค่า GroupDocs.Annotation Cloud SDK สำหรับ Node.JS

ก่อนที่คุณจะเริ่มต้น โปรดตรวจสอบว่าคุณมี:

  • Node.js 14+ และ npm ติดตั้งแล้ว
  • บัญชี GroupDocs cloud พร้อม clientId และ clientSecret
  • เข้าถึงไฟล์ต้นฉบับ DOCX ที่คุณต้องการทำคำอธิบาย

ติดตั้ง SDK ผ่าน npm:

npm install groupdocs-annotation-cloud

ดาวน์โหลดแพ็กเกจล่าสุดจากหน้าการปล่อยอย่างเป็นทางการหากคุณต้องการติดตั้งด้วยตนเอง: GroupDocs.Annotation Cloud SDK for Node.JS ดาวน์โหลด.

สร้างอ็อบเจ็กต์การกำหนดค่าโดยใช้ข้อมูลประจำตัวของคุณ (ส่วนย่อยจากตัวอย่างเต็ม):

const { Configuration } = require("@groupdocs.annotation-cloud");

const config = new Configuration({
    clientId: "YOUR_CLIENT_ID",
    clientSecret: "YOUR_CLIENT_SECRET"
});

เมื่อ SDK ถูกติดตั้งและข้อมูลประจำตัวพร้อมแล้ว เราสามารถดำเนินการต่อไปยังการทำงานได้

สร้างโน้ตสติ๊กกี้บนหน้าของ Word ใน Node.JS: คู่มือแบบขั้นตอนต่อขั้นตอน

ในบทแนะนำนี้ เราจะสร้าง Sticky Notes บนหน้า Word ใน Node.JS โดยใช้คลาส annotation ของ SDK

ขั้นตอนที่ 1: เริ่มต้น Annotation API

ก่อนแรก ให้นำเข้าคลาสที่จำเป็นและสร้างอินสแตนซ์ของ AnnotateApi.

const { AnnotateApi, models } = require("@groupdocs.annotation-cloud");
const annotateApi = new AnnotateApi(config);

ขั้นตอนที่ 2: กำหนดไฟล์ต้นทางและไฟล์ปลายทาง

ระบุไฟล์ DOCX อินพุตและเส้นทางสำหรับผลลัพธ์ที่มีการอธิบายประกอบ

const inputFilePath = "sample.docx";
const outputFilePath = "sample_annotated.docx";

const fileInfo = new models.FileInfo();
fileInfo.filePath = inputFilePath;

ขั้นตอนที่ 3: ตั้งค่าตำแหน่งของโน้ตติดกาว

สร้าง Rectangle ที่กำหนดตำแหน่งที่โน้ตติดกาวจะปรากฏบนหน้า 1.

const rect = new models.Rectangle();
rect.x = 100;      // distance from left edge (points)
rect.y = 100;      // distance from top edge (points)
rect.width = 200;  // width of the sticky note
rect.height = 100; // height of the sticky note

ขั้นตอนที่ 4: สร้างวัตถุการทำเครื่องหมายแบบโน้ตติดกาว

กำหนดประเภทของการทำเครื่องหมาย, หมายเลขหน้า, สี่เหลี่ยม, ข้อความ, และสีพื้นหลัง.

const stickyNote = new models.AnnotationInfo();
stickyNote.annotationType = "StickyNote";
stickyNote.pageNumber = 1;
stickyNote.rect = rect;
stickyNote.text = "This is a sticky note added via GroupDocs.Annotation Cloud SDK.";
stickyNote.color = "FFFF00"; // Yellow background

ขั้นตอนที่ 5: ดำเนินการคำขอการทำหมายเหตุ

รวมตัวเลือกของคำขอและเรียกใช้เมธอด annotate.

const annotateOptions = new models.AnnotateOptions();
annotateOptions.fileInfo = fileInfo;
annotateOptions.annotations = [stickyNote];
annotateOptions.outputPath = outputFilePath;

(async () => {
    try {
        const request = new models.AnnotateRequest(annotateOptions);
        const response = await annotateApi.annotate(request);
        console.log("Sticky note created successfully. Output saved to:", response.path);
    } catch (err) {
        console.error("Failed to create sticky note:", err);
    }
})();

ตัวอย่างทำงานเต็มสำหรับการสร้างโน้ตสติ๊กกี้บนหน้า Word ใน Node.JS

ตัวอย่างต่อไปนี้แสดงการใช้งานเต็มรูปแบบสำหรับการสร้าง Sticky Notes บนหน้า Word ใน Node.JS โดยใช้ GroupDocs.Annotation Cloud SDK.

const { AnnotateApi, Configuration, models } = require("@groupdocs.annotation-cloud");

// -----------------------------------------------------------------------------
// Configuration – replace with your actual GroupDocs credentials
// -----------------------------------------------------------------------------
const config = new Configuration({
    clientId: "YOUR_CLIENT_ID",
    clientSecret: "YOUR_CLIENT_SECRET"
});

const annotateApi = new AnnotateApi(config);

// -----------------------------------------------------------------------------
// Define input and output files
// -----------------------------------------------------------------------------
const inputFilePath = "sample.docx";
const outputFilePath = "sample_annotated.docx";

// -----------------------------------------------------------------------------
// Build FileInfo object for the source document
// -----------------------------------------------------------------------------
const fileInfo = new models.FileInfo();
fileInfo.filePath = inputFilePath;

// -----------------------------------------------------------------------------
// Define rectangle (position & size) for the sticky note on page 1
// -----------------------------------------------------------------------------
const rect = new models.Rectangle();
rect.x = 100;      // distance from left edge (points)
rect.y = 100;      // distance from top edge (points)
rect.width = 200;  // width of the sticky note
rect.height = 100; // height of the sticky note

// -----------------------------------------------------------------------------
// Create the sticky‑note annotation
// -----------------------------------------------------------------------------
const stickyNote = new models.AnnotationInfo();
stickyNote.annotationType = "StickyNote";
stickyNote.pageNumber = 1;
stickyNote.rect = rect;
stickyNote.text = "This is a sticky note added via GroupDocs.Annotation Cloud SDK.";
stickyNote.color = "FFFF00"; // Yellow background

// -----------------------------------------------------------------------------
// Assemble the annotate request options
// -----------------------------------------------------------------------------
const annotateOptions = new models.AnnotateOptions();
annotateOptions.fileInfo = fileInfo;
annotateOptions.annotations = [stickyNote];
annotateOptions.outputPath = outputFilePath;

// -----------------------------------------------------------------------------
// Execute the request
// -----------------------------------------------------------------------------
(async () => {
    try {
        const request = new models.AnnotateRequest(annotateOptions);
        const response = await annotateApi.annotate(request);
        console.log("Sticky note created successfully. Output saved to:", response.path);
    } catch (err) {
        console.error("Failed to create sticky note:", err);
    }
})();

หมายเหตุ: ตัวอย่างโค้ดนี้แสดงการทำงานหลัก ก่อนนำไปใช้ในโครงการของคุณ ให้ตรวจสอบและอัปเดตเส้นทางไฟล์และค่าการกำหนดค่าต่าง ๆ ให้ตรงกับสภาพแวดล้อมจริงของคุณ ตรวจสอบว่าขึ้นตอนการพึ่งพาที่จำเป็นทั้งหมดได้ติดตั้งอย่างถูกต้อง และทดสอบอย่างละเอียดในสภาพแวดล้อมการพัฒนา หากพบปัญหาใด ๆ โปรดดูที่ เอกสารอย่างเป็นทางการ หรือ ติดต่อทีม สนับสนุน เพื่อขอความช่วยเหลือ

การเพิ่มหมายเหตุแบบ Sticky Note ด้วย cURL และ REST API

REST API ให้ความสามารถเดียวกันโดยไม่ต้องเขียนโค้ด ด้านล่างเป็นคำสั่ง cURL ที่จำเป็น

ขั้นแรก ให้รับโทเค็นการเข้าถึง:

curl -X POST "https://api.groupdocs.cloud/v2.0.0/auth/token" \
     -H "Content-Type: application/json" \
     -d '{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'

อัปโหลดไฟล์ DOCX ต้นฉบับ (แทนที่ YOUR_ACCESS_TOKEN ด้วยโทเคนจากขั้นตอนก่อนหน้า):

curl -X PUT "https://api.groupdocs.cloud/v2.0.0/storage/file/sample.docx" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/octet-stream" \
     --data-binary @sample.docx

สร้างหมายเหตุแบบสติ๊กกี้:

curl -X POST "https://api.groupdocs.cloud/v2.0.0/annotation/annotate" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{
           "fileInfo": { "filePath": "sample.docx" },
           "annotations": [
               {
                   "annotationType": "StickyNote",
                   "pageNumber": 1,
                   "rect": { "x": 100, "y": 100, "width": 200, "height": 100 },
                   "text": "This is a sticky note added via GroupDocs.Annotation Cloud SDK.",
                   "color": "FFFF00"
               }
           ],
           "outputPath": "sample_annotated.docx"
         }'

ดาวน์โหลดเอกสารที่มีคำอธิบาย:

curl -X GET "https://api.groupdocs.cloud/v2.0.0/storage/file/sample_annotated.docx?download=true" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -o sample_annotated.docx

สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ payload ของคำขอ, ดูที่ เอกสารอ้างอิง API.

การกำหนดค่าตัวเลือกการอธิบายประกอบสำหรับโน้ตติด

SDK เปิดเผยคุณสมบัติต่าง ๆ หลายรายการที่ให้คุณปรับแต่งลักษณะและพฤติกรรมของโน้ตติดได้อย่างละเอียด:

  • annotationType - ต้องตั้งค่าเป็น "StickyNote" (จำเป็น)
  • pageNumber - หน้าที่โน้ตจะปรากฏ
  • rect - กำหนดตำแหน่ง (x, y) และขนาด (width, height). ปรับค่าต่าง ๆ เพื่อควบคุมการวางตำแหน่ง
  • text - เนื้อหาของโน้ต
  • color - สีพื้นหลังในรูปแบบ HEX (เช่น "FFFF00" สำหรับสีเหลือง)

คุณยังสามารถตั้งค่าฟิลด์เสริมเช่น author, fontFamily, หรือ opacity ผ่านโมเดล AnnotationInfo ได้อีกด้วย. ดูที่ อ้างอิง API สำหรับรายการคุณสมบัติที่สามารถกำหนดค่าได้ทั้งหมด.

สรุป

โดยทำตามขั้นตอนข้างต้น คุณจะรู้วิธีสร้าง Sticky Notes บนหน้า Word ใน Node.JS ด้วย GroupDocs.Annotation Cloud SDK แล้ว ไม่ว่าคุณจะชอบการใช้งานแบบเนทีฟของ Node.js หรือเวิร์กโฟลว์ cURL ที่ใช้ REST‑based SDK จะมอบวิธีที่สอดคล้องและเชื่อถือได้ในการใส่คำอธิบายลงในไฟล์ DOCX อย่างอัตโนมัติ อย่าลืมใช้ใบอนุญาตที่ถูกต้องสำหรับการใช้งานในสภาพแวดล้อมการผลิต; คุณสามารถรับใบอนุญาตชั่วคราวได้จาก หน้าใบอนุญาต หรือดูตัวเลือกการกำหนดราคาในหน้าผลิตภัณฑ์ ขอให้สนุกกับการใส่คำอธิบาย!

คำถามที่พบบ่อย

  • ฉันจะสร้าง Sticky Notes บนหน้า Word ใน Node.JS โดยใช้ GroupDocs.Annotation Cloud SDK ได้อย่างไร?
    ใช้ AnnotateApi ตามที่แสดงในตัวอย่างโค้ด SDK จะจัดการการเข้าถึงไฟล์ การสร้างคำอธิบายภาพ และการสร้างผลลัพธ์โดยอัตโนมัติ

  • ไฟล์ฟอร์แมตใดบ้างที่รองรับการทำ annotation ด้วย SDK?
    SDK รองรับ DOCX, PDF, PPTX, XLSX, และฟอร์แมตยอดนิยมอื่น ๆ อีกหลายรูปแบบ ดูรายการเต็มได้ใน เอกสารอย่างเป็นทางการ.

  • ฉันจะทำการยืนยันตัวตนอย่างไรเมื่อใช้ REST API สำหรับโน้ตสติ๊กกี้?
    รับโทเค็นการเข้าถึงผ่าน endpoint /auth/token โดยใช้ข้อมูลประจำตัวของไคลเอนต์ของคุณ แล้วใส่โทเค็นในส่วนหัว Authorization: Bearer สำหรับการเรียกใช้ทั้งหมดต่อไป รายละเอียดขั้นตอนอยู่ใน API reference

  • ฉันสามารถปรับแต่งสีหรือขนาดของโน้ตติดได้หรือไม่?
    ใช่. ตั้งค่าคุณสมบัติ color เป็นค่า HEX และปรับขนาด rect (width, height) เพื่อควบคุมขนาดและตำแหน่งของโน้ต.

อ่านเพิ่มเติม