HTML JPG PDF XML DOCX
  Product Family
PDF

Làm việc với chú thích trong PDF qua SDK Java

API để quản lý chú thích trong tài liệu PDF bằng Java.

Get Started

Cách làm việc với chú thích trong tài liệu PDF bằng Cloud Java SDK

Để làm việc với chú thích trong tài liệu PDF thông qua Cloud Java SDK, chúng ta sẽ sử dụng Aspose.PDF Cloud Java SDK Bộ SDK Đám mây này cho phép bạn dễ dàng xây dựng các ứng dụng tạo, chỉnh sửa và chuyển đổi PDF dựa trên đám mây bằng ngôn ngữ Java cho nhiều nền tảng đám mây khác nhau. Mở Kho trình quản lý gói, tìm kiếm Aspose.PDF Cloud và cài đặt. Bạn cũng có thể sử dụng lệnh sau từ Package Manager Console để cài đặt bằng Maven.

Thêm kho Aspose Cloud vào pom.xml của ứng dụng của bạn

Thêm kho Aspose Cloud


    <repositories>
        <repository>
            <id>aspose-cloud</id>
            <name>Aspose Cloud Repository</name>
            <url>https://releases.aspose.cloud/java/repo/</url>
        </repository>
    </repositories>

Để cài đặt thư viện client API vào kho Maven cục bộ của bạn, chỉ cần thực thi:

Cài đặt từ Github


    mvn clean install

Để triển khai nó vào kho Maven từ xa, hãy cấu hình các thiết lập của kho và thực thi:

Triển khai kho Maven


    mvn clean deploy

Các bước thêm chú thích bằng Java SDK

Các nhà phát triển Aspose.PDF Cloud có thể dễ dàng tải và thêm chú thích vào tài liệu PDF chỉ với một vài dòng mã.

  1. Tải lên tệp PDF.
  2. Tạo hộp văn bản có kiểu dáng (với phông chữ, màu sắc, căn chỉnh).
  3. Gửi chú thích tới trang được chỉ định.
  4. Tải xuống tài liệu đã chỉnh sửa để sử dụng cục bộ.
 

Mã mẫu này cho thấy cách thêm chú thích vào tài liệu PDF


    package com.aspose.asposecloudpdfusecases.annotations;

    import java.io.File;
    import java.nio.file.Path;
    import java.nio.file.StandardCopyOption;
    import java.util.ArrayList;
    import java.util.List;
    import com.aspose.asposecloudpdf.api.PdfApi;
    import com.aspose.asposecloudpdf.model.AsposeResponse;
    import com.aspose.asposecloudpdf.model.Color;
    import com.aspose.asposecloudpdf.model.FreeTextAnnotation;
    import com.aspose.asposecloudpdf.model.FreeTextIntent;
    import com.aspose.asposecloudpdf.model.HorizontalAlignment;
    import com.aspose.asposecloudpdf.model.Justification;
    import com.aspose.asposecloudpdf.model.Rectangle;
    import com.aspose.asposecloudpdf.model.TextStyle;

    public class AnnotationAddText {
        public static void Create() {
            String REMOTE_FOLDER   = "Your_Temp_Pdf_Cloud";
            String LOCAL_FOLDER    = "c:\\Samples";
            String PDF_DOCUMENT    = "sample.pdf";
            String PDF_OUTPUT      = "annotation_add_freetext_output.pdf";
            Integer PAGE_NUMBER = 1;

            String NEW_UL_ANNOTATION_TEXT        = "NEW FREE TEXT ANNOTATION";
            String NEW_UL_ANNOTATION_DESCRIPTION = "This is a sample free text annotation";
            String NEW_UL_ANNOTATION_SUBJECT     = "Free Text Box Subject";
            String NEW_UL_ANNOTATION_CONTENTS    = "Free text annotation sample contents";

            try {
                PdfApi pdfApi = new PdfApi(API_KEY, API_SECRET);

                // upload local PDF file to remote storage
                File file = new File(Path.of(LOCAL_FOLDER, PDF_DOCUMENT).toString());
                pdfApi.uploadFile(Path.of(REMOTE_FOLDER , PDF_DOCUMENT).toString(), file, null);
                System.out.println(String.format("File '%s' successfully uploaded!", Path.of(LOCAL_FOLDER, PDF_DOCUMENT).toString()));

                TextStyle textStyle = new TextStyle();
                textStyle.setFontSize(20.);
                textStyle.setForegroundColor(new Color().A(255).G(255));
                textStyle.setBackgroundColor(new Color().A(255).R(255));

                FreeTextAnnotation annotation = new FreeTextAnnotation();
                annotation.setName("Free_Text_Annotation");
                annotation.setRect(new Rectangle().LLX(100.).LLY(350.).URX(450.).URY(400.));
                annotation.setHorizontalAlignment(HorizontalAlignment.CENTER);
                annotation.setIntent(FreeTextIntent.FREETEXTTYPEWRITER);
                annotation.setRichText(NEW_UL_ANNOTATION_TEXT);
                annotation.setSubject(NEW_UL_ANNOTATION_SUBJECT);
                annotation.setContents(NEW_UL_ANNOTATION_CONTENTS);
                annotation.setTitle(NEW_UL_ANNOTATION_DESCRIPTION);
                annotation.setZindex(1);
                annotation.setJustification(Justification.CENTER);
                annotation.setTextStyle(textStyle);

                annotation.setModified("11/20/2025 00:00:00.000 AM");

                List<FreeTextAnnotation> annotations = new ArrayList<>();
                annotations.add(annotation);

                AsposeResponse response = pdfApi.postPageFreeTextAnnotations(PDF_DOCUMENT, PAGE_NUMBER, annotations, null, REMOTE_FOLDER);
                if (response.getCode() != 200)
                    System.err.println("Error: unexpected error when adding annotation! '" + response.getStatus() + "'");
                else{
                    // download changed PDF file from remote folder...
                    File f = pdfApi.downloadFile(Path.of(REMOTE_FOLDER , PDF_DOCUMENT).toString(), null, null);
                    java.nio.file.Files.copy(Path.of(f.getPath()), Path.of(LOCAL_FOLDER, PDF_OUTPUT), StandardCopyOption.REPLACE_EXISTING);
                    System.out.println(String.format("File '%s' successfully dowloaded!", Path.of(LOCAL_FOLDER, PDF_OUTPUT).toString()));                
                    System.out.println("Successfully added annotation in document ! '" + PDF_OUTPUT + "'");
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }