徒手墨迹绘图未在pdfbox中显示

时间:2018-07-12 05:35:48

标签: java annotations pdfbox

我想在pdf页面上添加手绘墨水注释。徒手注释已添加,但未在pdf页面上显示。我没有得到这里的问题。我正在分享已完成的代码。

import java.io.IOException;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;

public class Freehand {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    File file = new File("C:/Users/sinssb/Documents/07904660.pdf");
    PDDocument document = PDDocument.load(file);
    System.out.println("PDF loaded.");

    try {
        PDPage page = document.getPage(0);
        List<PDAnnotation> annotations = page.getAnnotations();
        PDColor color = new PDColor(new float[] {0, 0, 1}, PDDeviceRGB.INSTANCE);
        PDBorderStyleDictionary thickness = new PDBorderStyleDictionary();
        thickness.setWidth((float)2);

        PDAnnotationMarkup freehand = new PDAnnotationMarkup();
        freehand.getCOSObject().setName(COSName.SUBTYPE, PDAnnotationMarkup.SUB_TYPE_INK);
        freehand.setColor(color);
        freehand.setBorderStyle(thickness);

        float[] coordinates = new float[] {86,140,85,140,83,140,81,139,79,137,76,135,73,133,71,131,69,129,68,127,67,125,67,123,67,122,67,120,67,119,67,116}; 
        PDRectangle points = new PDRectangle();

        float[] allX = new float[coordinates.length / 2];
        float[] allY = new float[coordinates.length / 2];

        int k = 0, l = 0;
        for (int j = 0; j < coordinates.length; j++) {
            if (j % 2 == 0) {
                allX[k] = coordinates[j];
                k++;
            }
            else {
                allY[l] = coordinates[j];
                l++;
            }               
        }

        Arrays.sort(allX);
        Arrays.sort(allY);

        float smallestX = allX[0];
        float smallestY = allY[0];
        float largestX = allX[allX.length - 1];
        float largestY = allY[allY.length - 1];

        points.setLowerLeftX(smallestX);
        points.setLowerLeftY(smallestY);
        points.setUpperRightX(largestX);
        points.setUpperRightY(largestY);
        freehand.setRectangle(points);
        System.out.println(points);
        freehand.setContents("Hello");

        COSArray verticesArray = new COSArray();

        for (int i = 0; i < coordinates.length; i++) {
            verticesArray.add(new COSFloat(coordinates[i]));
        }

        freehand.getCOSObject().setItem(COSName.INKLIST, verticesArray);
        annotations.add(freehand);
        System.out.println("Freehand is added.");
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    // Save the file
    document.save(file);

    // Close the document
    document.close();
}

}

此代码添加了注释,因为我可以在Acrobat Reader的注释部分中看到注释和注释,但是我看不到页面上的图形。

谢谢。

1 个答案:

答案 0 :(得分:1)

墨水列表是一个数组数组(因为一个注释可以包含多行),因此请按以下方式更改代码:

COSArray verticesArray = new COSArray();

for (int i = 0; i < coordinates.length; i++) {
    verticesArray.add(new COSFloat(coordinates[i]));
}

// new / changed
COSArray verticesArrayArray = new COSArray();
verticesArrayArray.add(verticesArray);
freehand.getCOSObject().setItem(COSName.INKLIST, verticesArrayArray);