สรุปบทช่วยสอนอย่างง่ายนี้เกี่ยวกับ วิธีเพิ่มความคิดเห็นใน Word โดยใช้ Java คุณสามารถตั้งค่าคุณสมบัติขั้นสูงที่แตกต่างกันของความคิดเห็นสำหรับไฟล์ Word และบันทึกไฟล์อีกครั้งเป็น DOCX นี่คือขั้นตอนเกี่ยวกับ วิธีแทรกความคิดเห็นใน Word โดยใช้ Java
ขั้นตอนในการเพิ่มความคิดเห็นใน Word โดยใช้ Java
- เพิ่ม Aspose.Words จากที่เก็บ Maven เพื่อแทรกความคิดเห็น
- เปิดไฟล์ Word ต้นฉบับโดยใช้วัตถุคลาส Document
- เลื่อนเคอร์เซอร์ไปที่ย่อหน้าเป้าหมาย
- แทรกความคิดเห็นในย่อหน้าเอกสาร Word โดยใช้ Java
- บันทึกไฟล์หลังจากเพิ่มความคิดเห็น
ใช้ขั้นตอนเหล่านี้ในการเปิดเอกสาร Word และเริ่มต้นวัตถุ DocumentBuilder ที่สามารถใช้เพื่อเข้าถึงองค์ประกอบต่างๆ ของไฟล์ Word เช่น ย่อหน้า เราสามารถย้ายเคอร์เซอร์ไปยังองค์ประกอบใดก็ได้ที่เลียนแบบการเคลื่อนไหวของเคอร์เซอร์ใน MS Word ด้วยตนเอง ในที่สุดเราก็เพิ่มความคิดเห็นและบันทึกไฟล์ Word
รหัสเพื่อเพิ่มความคิดเห็นในเอกสาร Word โดยใช้ Java
import com.aspose.words.License; | |
import com.aspose.words.Document; | |
import com.aspose.words.DocumentBuilder; | |
import com.aspose.words.Comment; | |
import com.aspose.words.Paragraph; | |
import com.aspose.words.Run; | |
import java.util.Date; | |
public class HowToAddCommentsInWordUsingJava | |
{ | |
public static void main(String[] args) throws Exception { //main function for AddImageInWord class | |
// Initialize a license to avoid trial version watermark in the output Word file after adding image | |
License license = new License(); | |
license.setLicense("Aspose.Words.lic"); | |
// Load the Word document where comments are to be added | |
Document DocumentForComment = new Document("input.docx"); | |
DocumentBuilder builder = new DocumentBuilder(DocumentForComment); | |
// Move the cursor to the beginning of the document for adding comments | |
builder.moveToDocumentStart(); | |
// Insert comment to first paragraph of document by providing Author, Initial, time and comment text | |
Comment comment = new Comment(DocumentForComment, "Aspose.Words", "AW", new Date()); | |
builder.getCurrentParagraph().appendChild(comment); | |
comment.getParagraphs().add(new Paragraph(DocumentForComment)); | |
comment.getFirstParagraph().getRuns().add(new Run(DocumentForComment, "Comment text.")); | |
// Save the Document with comments | |
DocumentForComment.save("OutputDocumentWithComments.docx"); | |
} | |
} |
ในโค้ด Java นี้ เราใช้ออบเจกต์คลาส Comment ที่มีคุณสมบัติทั้งหมดที่จำเป็นในการกำหนดค่าความคิดเห็นในเอกสาร Word เราระบุชื่อผู้แต่ง ชื่อย่อของผู้ใช้ เวลาที่แสดงความคิดเห็น จากนั้นจึงตั้งค่าข้อความความคิดเห็น
ในบทช่วยสอนแบบทีละขั้นตอนนี้ เราได้เปิดไฟล์ที่มีอยู่และเพิ่มความคิดเห็นลงไป หากคุณต้องการเรียนรู้คุณลักษณะเพิ่มเติม เช่น การเพิ่มแถวในตาราง โปรดดูบทความเกี่ยวกับ วิธีเพิ่มแถวในตารางใน Word โดยใช้ Java โปรดทราบว่าระบบไม่จำเป็นต้องใช้ MS Word หรือ Interop ในการรันโค้ดด้านบน