เพิ่มคำอธิบายลงใน PDF
หากคุณต้องการ add annotations to PDF จากแอปพลิเคชัน Java — ไม่ว่าคุณกำลังสร้างพอร์ทัลการตรวจสอบสัญญา, แพลตฟอร์ม e‑learning, หรือโปรแกรมแก้ไขเอกสารแบบร่วมมือ — บทแนะนำนี้จะแสดงให้คุณเห็นวิธีทำด้วย GroupDocs.Annotation อย่างละเอียด คุณจะได้เรียนรู้การโหลด PDF, สร้างคอมเมนต์แบบพื้นที่, จัดการทรัพยากรอย่างมีประสิทธิภาพ, และจัดการไฟล์ขนาดใหญ่โดยไม่ทำให้หน่วยความจำหมด
คำตอบอย่างรวดเร็ว
- วัตถุประสงค์หลักของ GroupDocs.Annotation คืออะไร? เพื่อเพิ่ม, แก้ไข, และจัดการคำอธิบายในหลายรูปแบบเอกสารจาก Java.
- ประเภทคำอธิบายใดที่เหมาะสำหรับคอมเมนต์การตรวจสอบ?
AreaAnnotationพร้อมข้อความที่กำหนดเองและเมตาดาต้าผู้ใช้. - ฉันต้องการไลเซนส์สำหรับการพัฒนาหรือไม่? การทดลองใช้ฟรีทำงานสำหรับการทดสอบ; ไลเซนส์เต็มจำเป็นสำหรับการใช้งานจริง.
- ฉันสามารถประมวลผล PDF ที่ใหญ่กว่า 50 MB ได้หรือไม่? ใช่ — ใช้การสตรีม, การประมวลผลเป็นชุด, และการทำลายที่เหมาะสมเพื่อรักษาการใช้หน่วยความจำให้ต่ำ.
- ไลบรารีนี้ปลอดภัยต่อการทำงานหลายเธรดหรือไม่? อินสแตนซ์ไม่ปลอดภัยต่อการทำงานหลายเธรด; สร้าง
Annotatorแยกสำหรับแต่ละเธรด.
ทำไม GroupDocs Annotation ถึงโดดเด่น
GroupDocs.Annotation ช่วยให้คุณ add annotations to PDF อย่างรวดเร็วพร้อมสนับสนุนระบบไฟล์ที่หลากหลาย มันมี:
- Comprehensive format support – รองรับรูปแบบไฟล์กว่า 50 แบบ ทั้งการนำเข้าและส่งออก รวมถึง DOCX, PPTX, XLSX, JPEG, PNG, และ TIFF.
- Rich annotation palette – ไฮไลท์, ลูกศร, วอเตอร์มาร์ค, การแทนที่ข้อความ, และรูปทรงแบบกำหนดเอง, ทั้งหมดพร้อมใช้งานผ่าน API เดียว.
- Enterprise‑ready features – มีไลเซนส์ในตัว, ตัวเลือกการขยายขนาด, และการผสานรวมอย่างราบรื่นกับสแตก Java ที่มีอยู่.
- Active development – การปล่อยเวอร์ชันใหม่ทุกเดือนนำมาซึ่งการเพิ่มประสิทธิภาพ (เร็วขึ้นถึง 30 % ในการเรนเดอร์) และประเภทคำอธิบายใหม่.
ข้อกำหนดเบื้องต้นและการตั้งค่า
สิ่งที่คุณต้องการก่อนเริ่ม
- Development environment – JDK 8 หรือใหม่กว่า (แนะนำ Java 11+), Maven หรือ Gradle, และ IDE ที่คุณชอบ (IntelliJ IDEA, Eclipse, หรือ VS Code).
- Basic knowledge – ไวยากรณ์ Java, การทำงานกับไฟล์ I/O, และการจัดการ dependencies ของ Maven.
- Optional – ความคุ้นเคยกับพิกัดหน้าของ PDF และประสบการณ์กับไลบรารี Java อื่น ๆ.
การตั้งค่า GroupDocs.Annotation สำหรับ Java
การกำหนดค่า Maven
เพิ่มรีโพซิทอรีของ GroupDocs และ dependency ของ Annotation ไปยัง pom.xml ของคุณ. แทนที่ 25.2 ด้วยเวอร์ชันล่าสุดหากมีเวอร์ชันใหม่กว่า.
<!-- Maven repository -->
<repository>
<id>groupdocs-repo</id>
<url>https://repo.groupdocs.com/maven2</url>
</repository>
<!-- Annotation dependency -->
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-annotation</artifactId>
<version>25.2</version>
</dependency>
เคล็ดลับ: ตรวจสอบเวอร์ชันล่าสุดเสมอบนเว็บไซต์ของ GroupDocs. การปล่อยเวอร์ชันใหม่มักจะลดการใช้หน่วยความจำสูงสุดถึง 20 % สำหรับ PDF ขนาดใหญ่.
ตัวเลือกไลเซนส์ (และความหมายที่แท้จริง)
- Free trial – ผลลัพธ์มีลายน้ำ, เหมาะสำหรับการประเมิน.
- Temporary license – ไม่จำกัดเป็นเวลา 30 วัน; รับ temporary license Temporary License.
- Full license – จำเป็นสำหรับการใช้งานจริง; ราคาจะแตกต่างตามขนาดการใช้งาน.
การตรวจสอบเบื้องต้น
หลังจากเพิ่ม dependency, รันการทดสอบเล็ก ๆ นี้เพื่อยืนยันว่าไลบรารีโหลดอย่างถูกต้อง:
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/annotation/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-annotation</artifactId>
<version>25.2</version>
</dependency>
</dependencies>
วิธีสร้างคำอธิบาย PDF ด้วย Java และ GroupDocs.Annotation
โหลดเอกสาร, เพิ่ม AreaAnnotation, และบันทึกผลลัพธ์. คำตอบโดยตรงต่อไปนี้สรุปขั้นตอนทั้งหมด:
โหลด PDF ของคุณด้วย new Annotator("input.pdf"), สร้าง AreaAnnotation ที่กำหนดสี่เหลี่ยมและข้อความคอมเมนต์, เพิ่มคำอธิบายลงใน Annotator, จากนั้นเรียก save("output.pdf"). รูปแบบสามขั้นตอนนี้จัดการกับ PDF ขนาดใดก็ได้และรับประกันว่าทรัพยากรจะถูกปล่อยโดยอัตโนมัติ.
การโหลดเอกสาร: มากกว่าการระบุเส้นทางไฟล์
การโหลดเอกสารพื้นฐาน
คลาส Annotator เป็นจุดเริ่มต้นสำหรับการดำเนินการคำอธิบายทั้งหมด. มันเป็นตัวแทนของเอกสาร PDF ในหน่วยความจำและให้เมธอดสำหรับอ่าน, แก้ไข, และบันทึกคำอธิบาย.
Annotator annotator = new Annotator("sample.pdf");
บริบทจริง: ในการใช้งานจริง, เส้นทางไฟล์อาจมาจากโฟลเดอร์อัปโหลด, blob ของฐานข้อมูล, หรือ URL ของคลาวด์สตอเรจ. GroupDocs.Annotation รองรับไฟล์ในเครื่อง, InputStreams, และ URL ระยะไกลโดยไม่ต้องเขียนโค้ดเพิ่มเติม.
การจัดการแหล่งข้อมูลเข้าแบบต่าง ๆ
คุณสามารถโหลด PDF จากอาร์เรย์ไบต์หรือ URL HTTP:
import com.groupdocs.annotation.Annotator;
public class SetupVerification {
public static void main(String[] args) {
try {
// This should not throw any ClassNotFoundException
System.out.println("GroupDocs.Annotation version: " +
com.groupdocs.annotation.internal.c.a.a.d());
System.out.println("Setup successful!");
} catch (Exception e) {
System.err.println("Setup failed: " + e.getMessage());
}
}
}
การเพิ่มคำอธิบายแรกของคุณ
ทำความเข้าใจ area annotations
AreaAnnotation เป็นประเภทที่หลากหลายที่สุดสำหรับการไฮไลท์พื้นที่สี่เหลี่ยมและแนบคอมเมนต์. มันทำงานเหมือนโน้ตดิจิทัลที่สามารถย้าย, ปรับขนาด, และกำหนดสไตล์ได้.
AreaAnnotation area = new AreaAnnotation();
area.setRectangle(new Rectangle(100, 150, 200, 80)); // x, y, width, height
area.setComment("Review this section for compliance.");
area.setCreator("alice@example.com");
อธิบายระบบพิกัด: พิกัด PDF เริ่มจากมุมล่างซ้าย, แต่ GroupDocs ใช้จุดกำเนิดที่มุมบนซ้าย, ซึ่งสอดคล้องกับเฟรมเวิร์ก UI ทั่วไป. ค่าถูกแสดงเป็นจุด (1/72 นิ้ว).
ตัวอย่างการอธิบายเชิงปฏิบัติ
การไฮไลท์ข้อความสำคัญ
HighlightAnnotation highlight = new HighlightAnnotation();
highlight.setRectangle(new Rectangle(50, 200, 300, 20));
highlight.setColor(Color.YELLOW);
annotator.add(highlight);
การสร้างคอมเมนต์การตรวจสอบ
AreaAnnotation comment = new AreaAnnotation();
comment.setRectangle(new Rectangle(120, 400, 250, 100));
comment.setComment("Please verify the calculation in this paragraph.");
comment.setCreator("bob@example.com");
annotator.add(comment);
การบันทึกและการจัดการทรัพยากร
เทคนิคการบันทึกไฟล์ที่เหมาะสม
annotator.save("annotated.pdf");
ทำไมการทำลายจึงสำคัญ: Annotator เก็บเนื้อหา PDF ในหน่วยความจำเพื่อการเข้าถึงที่รวดเร็ว. การไม่ปิดอาจทำให้เกิดการรั่วไหลของหน่วยความจำ, โดยเฉพาะเมื่อประมวลผลหลายสิบไฟล์ในงานแบบแบช.
รูปแบบการจัดการทรัพยากรที่ดีกว่า
try (Annotator annotator = new Annotator("input.pdf")) {
// add annotations here
annotator.save("output.pdf");
}
ข้อผิดพลาดทั่วไปและวิธีหลีกเลี่ยง
ปัญหาเส้นทางไฟล์และสิทธิ์
- Problem: “File not found” or “Access denied”.
- Solution: Use absolute paths during development, verify read/write permissions, and confirm the file exists before opening.
File file = new File("C:/docs/input.pdf");
if (!file.exists() || !file.canRead()) {
throw new IllegalArgumentException("File is missing or unreadable.");
}
ความผิดพลาดในการจัดการหน่วยความจำ
- Problem: Application slows down after processing many PDFs.
- Solution: Always employ try‑with‑resources or explicit
close()calls to free native buffers.
try (Annotator annotator = new Annotator("large.pdf")) {
// process
}
ความสับสนของระบบพิกัด
- Problem: Annotations appear off‑screen.
- Solution: Remember GroupDocs uses a top‑left origin. Test with known coordinates such as (50, 50, 100, 50) before applying dynamic values.
AreaAnnotation test = new AreaAnnotation();
test.setRectangle(new Rectangle(50, 50, 100, 50));
annotator.add(test);
กรณีการใช้งานจริงและแอปพลิเคชัน
กระบวนการตรวจสอบเอกสาร
ทีมกฎหมายมักต้องการติดตามว่าใครคอมเมนต์ในข้อใด. ด้วย GroupDocs.Annotation คุณสามารถ:
- กำหนดสีเฉพาะให้กับผู้ตรวจสอบแต่ละคน.
- เก็บเวลาติดตั้งและ ID ผู้ใช้ในเมตาดาต้าของคำอธิบาย.
- ส่งออก PDF ที่มีคำอธิบายสำหรับการแจกจ่ายให้ลูกค้าโดยคงรักษาเส้นทางการตรวจสอบ.
area.setCreator("lawyer@example.com");
area.setCreatedOn(LocalDateTime.now());
การสร้างเนื้อหาการศึกษา
แพลตฟอร์ม e‑learning ใช้คำอธิบายเพื่อไฮไลท์แนวคิดสำคัญ, ฝังแบบสอบถาม, หรือเพิ่มโน้ตอธิบายโดยตรงบน PDF การศึกษา. สิ่งนี้ช่วยเพิ่มการจดจำได้ถึง 35 % เมื่อเทียบกับ PDF ธรรมดา (การศึกษาในองค์กร).
เอกสารการประกันคุณภาพ
ผู้ผลิตทำคำอธิบายบนภาพวาดเทคนิคเพื่อระบุ tolerances, การแก้ไข, และคำเตือนด้านความปลอดภัย. การทำ markup มาตรฐานช่วยลดงานซ้ำประมาณ 22 % ในสายการผลิต.
เคล็ดลับการเพิ่มประสิทธิภาพ
การจัดการเอกสารขนาดใหญ่อย่างมีประสิทธิภาพ
การประมวลผล PDF 200 หน้า สามารถทำได้ภายในต่ำกว่า 2 วินาทีบนเซิร์ฟเวอร์ 4‑core ปกติเมื่อสตรีมหน้าต่าง ๆ แทนการโหลดไฟล์ทั้งหมด.
Annotator annotator = new Annotator(new FileInputStream("big.pdf"));
การตรวจสอบการใช้หน่วยความจำ
เปิดใช้งานเครื่องมือมอนิเตอร์ของ Java (JVisualVM, JConsole) เพื่อตรวจสอบการใช้ heap ขณะทำคำอธิบายไฟล์ขนาดใหญ่. ตั้งเป้าหมายให้ heap อยู่ต่ำกว่า 70 % ของการจัดสรรสูงสุด.
พิจารณาการประมวลผลพร้อมกัน
อินสแตนซ์ของ GroupDocs.Annotation ไม่ปลอดภัยต่อการทำงานหลายเธรด. สำหรับงานแบบขนาน, สร้าง Annotator แยกต่อเธรดหรือใช้ thread‑local pool.
ExecutorService executor = Executors.newFixedThreadPool(4);
for (String path : pdfPaths) {
executor.submit(() -> {
try (Annotator a = new Annotator(path)) {
// annotate
a.save(path.replace(".pdf", "_annotated.pdf"));
}
});
}
เทคนิคการอธิบายขั้นสูง
หลายประเภทคำอธิบายในเอกสารเดียว
คุณสามารถผสมไฮไลท์, โน้ตข้อความ, และวอเตอร์มาร์คในขั้นตอนเดียว:
String INPUT_PDF = "YOUR_DOCUMENT_DIRECTORY/input.pdf";
String outputPath = "YOUR_OUTPUT_DIRECTORY/output_annotated.pdf";
// Initialize Annotator with the path to your document
final Annotator annotator = new Annotator(INPUT_PDF);
คำอธิบายแบบไดนามิกตามเนื้อหา
ผสาน GroupDocs.Annotation กับ Apache PDFBox หรือ Tika เพื่อค้นหาคำสำคัญโดยอัตโนมัติและเพิ่มโน้ตตามบริบท.
// pseudo‑code: detect the word "confidential"
if (pageText.contains("confidential")) {
WatermarkAnnotation wm = new WatermarkAnnotation();
wm.setText("CONFIDENTIAL");
annotator.add(wm);
}
คู่มือการแก้ไขปัญหา
ข้อความแสดงข้อผิดพลาดทั่วไปและวิธีแก้
- “Invalid license” – ตรวจสอบว่าไฟล์ไลเซนส์อยู่ใน classpath และตรงกับประเภทการใช้งานของคุณ.
- “Unsupported file format” – ยืนยันว่า PDF ไม่เสียหาย, ไม่ถูกป้องกันด้วยรหัสผ่าน, หรือมีขนาดศูนย์ไบต์.
- Performance slowdown – ใช้การสตรีม, ทำลายอ็อบเจกต์
Annotatorอย่างทันท่วงที, และพิจารณาการประมวลผลเป็นชุด.
เคล็ดลับการดีบัก
เปิดการบันทึกล็อก
System.setProperty("log4j.configurationFile", "log4j2.xml");
ตรวจสอบอินพุต
Objects.requireNonNull(filePath, "File path cannot be null");
คำถามที่พบบ่อย
Q: ฉันจะเพิ่มหลายคำอธิบายลงใน PDF เดียวอย่างมีประสิทธิภาพได้อย่างไร?
A: เรียก annotator.add(annotation) สำหรับแต่ละคำอธิบาย, จากนั้นเรียก save() ครั้งเดียว. GroupDocs จะทำแบตช์การเปลี่ยนแปลงและเขียนในขั้นตอนเดียว.
annotator.add(area1);
annotator.add(highlight2);
annotator.save("combined.pdf");
Q: GroupDocs.Annotation รองรับรูปแบบไฟล์อะไรบ้างนอกจาก PDF?
A: รองรับมากกว่า 50 รูปแบบ รวมถึง DOC, DOCX, PPT, PPTX, XLS, XLSX, JPEG, PNG, TIFF, และอื่น ๆ อีกมาก. ดูรายการเต็มใน เอกสาร GroupDocs.Annotation.
Q: ฉันจะจัดการกับ PDF ที่ป้องกันด้วยรหัสผ่านอย่างไร?
Annotator annotator = new Annotator("secure.pdf", "password123");
Q: ฉันสามารถดึงและแก้ไขคำอธิบายที่มีอยู่ใน PDF ได้หรือไม่?
List<AnnotationBase> existing = annotator.getAll();
for (AnnotationBase a : existing) {
if (a instanceof TextAnnotation) {
((TextAnnotation) a).setComment("Updated comment");
}
}
annotator.save("updated.pdf");
Q: ผลกระทบต่อประสิทธิภาพของการประมวลผล PDF ขนาดใหญ่คืออะไร?
A: ไฟล์ที่ใหญ่กว่า 50 MB ควรสตรีมหน้า‑ต่อหน้า. การใช้หน่วยความจำจะอยู่ต่ำกว่า 150 MB เมื่อคุณทำลาย Annotator แต่ละอันหลังจากประมวลผลชุดหน้าหนึ่ง.
Q: ฉันจะจัดการกับการประมวลผลเอกสารพร้อมกันในเว็บแอปพลิเคชันอย่างไร?
// each request gets its own Annotator instance
public void processPdf(String path) {
try (Annotator a = new Annotator(path)) {
// annotate
a.save(path);
}
}
Q: วิธีที่ดีที่สุดในการดีบักปัญหาตำแหน่งของคำอธิบายคืออะไร?
A: เริ่มด้วยสี่เหลี่ยมที่รู้จัก (เช่น (50, 50, 100, 50)) และตรวจสอบผลลัพธ์ภาพ. ปรับพิกัดทีละน้อยและตรวจสอบขนาดหน้าของ PDF (โดยทั่วไป 612 × 792 จุด).
Q: ฉันจะผสาน GroupDocs.Annotation กับ Spring Boot อย่างไร?
@Service
public class PdfAnnotationService {
public void addComment(String filePath, String comment) throws IOException {
try (Annotator annotator = new Annotator(filePath)) {
AreaAnnotation a = new AreaAnnotation();
a.setRectangle(new Rectangle(100, 200, 250, 80));
a.setComment(comment);
annotator.add(a);
annotator.save(filePath);
}
}
}
คำถามเพิ่มเติม
Q: ฉันสามารถส่งออก PDF ที่มีคำอธิบายเป็นรูปแบบอื่นได้หรือไม่?
A: ได้, คุณสามารถแปลง PDF ที่มีคำอธิบายเป็น DOCX, PPTX, หรือรูปภาพโดยคงรักษา markup ทั้งหมด.
Q: มีวิธีใดบ้างที่จะรายการประเภทคำอธิบายทั้งหมดที่ไลบรารีสนับสนุน?
A: ใช้ AnnotationType.values() เพื่อดึงอาเรย์ของ enum ที่สนับสนุนทั้งหมด.
Q: ฉันจะปรับแต่งลักษณะของคำอธิบายวอเตอร์มาร์คอย่างไร?
A: ตั้งค่าคุณสมบัติเช่น setOpacity, setRotation, และ setBackgroundColor บน WatermarkAnnotation ก่อนเพิ่มเข้าไป.
Q: ไลบรารีนี้สนับสนุนการเพิ่มคอมเมนต์โดยโปรแกรมจากฐานข้อมูลหรือไม่?
A: แน่นอน. อ่านข้อมูลคอมเมนต์จากแหล่งใดก็ได้, เติมข้อมูลลงใน AreaAnnotation (หรือ TextAnnotation) ด้วยข้อความ, แล้วเพิ่มลงในเอกสาร.
Q: ควรทำอย่างไรหากพบการรั่วไหลของหน่วยความจำระหว่างการประมวลผลแบบแบช?
A: ตรวจสอบให้แน่ใจว่า Annotator ทุกตัวถูกปิด (try‑with‑resources), มอนิเตอร์ heap ของ JVM, และแบ่งแบชขนาดใหญ่เป็นกลุ่มเล็ก ๆ.
แหล่งข้อมูลเพิ่มเติม
- เอกสาร GroupDocs.Annotation
- คู่มืออ้างอิง API
- ดาวน์โหลดเวอร์ชันล่าสุด
- ซื้อไลเซนส์
- เข้าถึงการทดลองใช้ฟรี
- ไลเซนส์ชั่วคราว
- ฟอรั่มสนับสนุน
อัปเดตล่าสุด: 2026-10-16
ทดสอบด้วย: GroupDocs.Annotation 25.2 สำหรับ Java
ผู้เขียน: GroupDocs
// From file path (most common)
Annotator annotatorFromPath = new Annotator("path/to/document.pdf");
// From InputStream (useful for uploaded files)
FileInputStream inputStream = new FileInputStream("document.pdf");
Annotator annotatorFromStream = new Annotator(inputStream);
// Don't forget to close streams when done!
inputStream.close();
import com.groupdocs.annotation.models.Rectangle;
import com.groupdocs.annotation.models.annotationmodels.AreaAnnotation;
// Create the annotation
AreaAnnotation area = new AreaAnnotation();
// Position and size: x, y, width, height
area.setBox(new Rectangle(100, 100, 100, 100));
// Background color in ARGB format (65535 = yellow with transparency)
area.setBackgroundColor(65535);
// Add the annotation to your document
annotator.add(area);
// Create a semi‑transparent highlight
AreaAnnotation highlight = new AreaAnnotation();
highlight.setBox(new Rectangle(50, 200, 200, 25));
highlight.setBackgroundColor(0x80FFFF00); // Semi‑transparent yellow
highlight.setMessage("Important clause - review carefully");
// Add a comment annotation with custom styling
AreaAnnotation comment = new AreaAnnotation();
comment.setBox(new Rectangle(300, 150, 150, 75));
comment.setBackgroundColor(0x80FF0000); // Semi‑transparent red
comment.setMessage("Needs revision - see discussion in email");
comment.setCreatedOn(new Date());
comment.setUser("John Reviewer");
// Save the annotated document
annotator.save(outputPath);
// Always dispose of resources (critical for memory management)
annotator.dispose();
public void annotateDocument(String inputPath, String outputPath) {
try (Annotator annotator = new Annotator(inputPath)) {
// Your annotation code here
AreaAnnotation area = new AreaAnnotation();
area.setBox(new Rectangle(100, 100, 100, 100));
area.setBackgroundColor(65535);
annotator.add(area);
annotator.save(outputPath);
System.out.println("Document successfully annotated and saved to: " + outputPath);
} catch (Exception e) {
System.err.println("Annotation failed: " + e.getMessage());
throw new RuntimeException("Failed to annotate document", e);
}
}
public boolean validateInputFile(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
System.err.println("File does not exist: " + filePath);
return false;
}
if (!file.canRead()) {
System.err.println("Cannot read file: " + filePath);
return false;
}
return true;
}
// Good practice - automatic resource management
try (Annotator annotator = new Annotator(inputPath)) {
// Annotation code here
} // Automatically disposed
// If manual disposal is needed
Annotator annotator = null;
try {
annotator = new Annotator(inputPath);
// Annotation code here
} finally {
if (annotator != null) {
annotator.dispose();
}
}
// Start with simple, visible coordinates for testing
Rectangle testPosition = new Rectangle(50, 50, 100, 50);
// Gradually adjust based on your PDF dimensions
// Most PDFs are 612x792 points (8.5"x11" at 72 DPI)
public void addReviewAnnotation(Annotator annotator, String reviewerName,
String comment, Rectangle position, Color highlightColor) {
AreaAnnotation review = new AreaAnnotation();
review.setBox(position);
review.setBackgroundColor(highlightColor.getRGB());
review.setMessage(comment);
review.setUser(reviewerName);
review.setCreatedOn(new Date());
annotator.add(review);
}
public void processDocumentBatch(List<String> documentPaths) {
for (String path : documentPaths) {
try (Annotator annotator = new Annotator(path)) {
// Process each document independently
// This prevents memory accumulation
processAnnotations(annotator);
}
// Optional: Add small delay for very large batches
// Thread.sleep(100);
}
}
Runtime runtime = Runtime.getRuntime();
long memoryBefore = runtime.totalMemory() - runtime.freeMemory();
// Process documents...
long memoryAfter = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Memory used: " + (memoryAfter - memoryBefore) + " bytes");
public class ConcurrentAnnotationProcessor {
public void processDocumentsConcurrently(List<String> documents) {
documents.parallelStream().forEach(docPath -> {
try (Annotator annotator = new Annotator(docPath)) {
// Each thread gets its own Annotator instance
processAnnotations(annotator);
}
});
}
}
public void createComprehensiveAnnotation(Annotator annotator) {
// Highlight important text
AreaAnnotation highlight = new AreaAnnotation();
highlight.setBox(new Rectangle(100, 100, 200, 30));
highlight.setBackgroundColor(0x80FFFF00);
// Add explanatory note
AreaAnnotation note = new AreaAnnotation();
note.setBox(new Rectangle(320, 95, 150, 40));
note.setBackgroundColor(0x80ADD8E6);
note.setMessage("See reference document #123");
annotator.add(highlight);
annotator.add(note);
}
// Add to your application properties or logging configuration
java.util.logging.Logger.getLogger("com.groupdocs").setLevel(Level.FINE);
public boolean validateAnnotationParameters(Rectangle box, int color) {
if (box.getWidth() <= 0 || box.getHeight() <= 0) {
System.err.println("Invalid annotation dimensions");
return false;
}
if (box.getX() < 0 || box.getY() < 0) {
System.err.println("Annotation position cannot be negative");
return false;
}
return true;
}
try (Annotator annotator = new Annotator("document.pdf")) {
annotator.add(annotation1);
annotator.add(annotation2);
annotator.add(annotation3);
annotator.save("output.pdf"); // All annotations applied at once
}
LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("your-password");
Annotator annotator = new Annotator("protected.pdf", loadOptions);
try (Annotator annotator = new Annotator("annotated.pdf")) {
List<AnnotationInfo> annotations = annotator.get(AnnotationType.Area);
for (AnnotationInfo annotation : annotations) {
// Modify properties as needed
annotation.setMessage("Updated comment");
}
annotator.update(annotations);
annotator.save("updated.pdf");
}
@Service
public class AnnotationService {
public CompletableFuture<String> annotateAsync(String inputPath) {
return CompletableFuture.supplyAsync(() -> {
try (Annotator annotator = new Annotator(inputPath)) {
// Process annotations
return processDocument(annotator);
}
});
}
}
@Service
public class DocumentAnnotationService {
public void annotateDocument(MultipartFile file, List<AnnotationRequest> requests) {
try (InputStream inputStream = file.getInputStream();
Annotator annotator = new Annotator(inputStream)) {
// Process annotation requests
requests.forEach(request -> addAnnotation(annotator, request));
annotator.save("output.pdf");
}
}
}