Can' use FPDFAnnot_SetAttachmentPoints, return could not set attachment points

27 views
Skip to first unread message

张猛

unread,
Jul 11, 2025, 5:31:14 AMJul 11
to pdfium
I want to use go-pdfium to get an underline/highlight etc annotation. But I got an error: could not set attachment points. And here is my golang code:
```go
func CreateUnderlineAnnotation(instance pdfium.Pdfium, pdfDoc references.FPDF_DOCUMENT, pageIndex int) error {
// create a underline annotation
createAnnoRes, err := instance.FPDFPage_CreateAnnot(&requests.FPDFPage_CreateAnnot{
Page: requests.Page{
ByIndex: &requests.PageByIndex{
Document: pdfDoc,
Index:    pageIndex,
},
},
Subtype: enums.FPDF_ANNOT_SUBTYPE_UNDERLINE,
})
if err != nil {
return fmt.Errorf("无法创建 annotation: %v", err)
}

fmt.Printf("createAnnoRes.Annotation: %v\n", createAnnoRes.Annotation)

// set entire rect
_, err = instance.FPDFAnnot_SetRect(&requests.FPDFAnnot_SetRect{
Annotation: createAnnoRes.Annotation,
Rect: structs.FPDF_FS_RECTF{
Left:   100,
Top:    400,
Right:  600,
Bottom: 200,
},
})
if err != nil {
return fmt.Errorf("无法设置annotation rect: %v", err)
}

// set major color
_, err = instance.FPDFAnnot_SetColor(&requests.FPDFAnnot_SetColor{
Annotation: createAnnoRes.Annotation,
ColorType:  enums.FPDFANNOT_COLORTYPE_Color,
R:          uint(255),
G:          uint(0),
B:          uint(0),
A:          uint(130),
})
if err != nil {
return fmt.Errorf("无法设置 annotation颜色: %v", err)
}

_, err = instance.FPDFAnnot_SetColor(&requests.FPDFAnnot_SetColor{
Annotation: createAnnoRes.Annotation,
ColorType:  enums.FPDFANNOT_COLORTYPE_InteriorColor,
R:          uint(0),
G:          uint(255),
B:          uint(0),
A:          uint(130),
})
if err != nil {
return fmt.Errorf("无法设置 annotation颜色: %v", err)
}

// underline quads
var quadpoints = []structs.FPDF_FS_QUADPOINTSF{
{
X1: 150, Y1: 350, // 左上
X2: 300, Y2: 350, // 右上
X3: 300, Y3: 330, // 右下
X4: 150, Y4: 330, // 左下
},
{
X1: 320, Y1: 280, // 左上
X2: 550, Y2: 280, // 右上
X3: 550, Y3: 260, // 右下
X4: 320, Y4: 260, // 左下
},
}

var minX, minY, maxX, maxY float32
for i, points := range quadpoints {
_, err = instance.FPDFAnnot_SetAttachmentPoints(&requests.FPDFAnnot_SetAttachmentPoints{
Annotation:       createAnnoRes.Annotation,
Index:            0,
AttachmentPoints: points,
})
if err != nil {
return fmt.Errorf("无法设置i:%d FPDFAnnot_SetAttachmentPoints: %v", i, err)
}

minX = min(points.X1, points.X4)
minY = min(points.Y3, points.Y4)
maxX = max(points.X2, points.X3)
maxY = max(points.Y1, points.Y2)
}

// set presice rect

_, err = instance.FPDFAnnot_SetRect(&requests.FPDFAnnot_SetRect{
Annotation: createAnnoRes.Annotation,
Rect: structs.FPDF_FS_RECTF{
Left:   minX,
Top:    maxY,
Right:  maxX,
Bottom: minY,
},
})
if err != nil {
return fmt.Errorf("无法设置annotation rect: %v", err)
}

_, err = instance.FPDFAnnot_SetBorder(&requests.FPDFAnnot_SetBorder{
Annotation:       createAnnoRes.Annotation,
HorizontalRadius: 0,
VerticalRadius:   0,
BorderWidth:      20,
})
if err != nil {
return fmt.Errorf("无法设置 annotation border: %v", err)
}

return nil
}

```

I want to know why? why FPDFAnnot_SetAttachmentPoints can't use? and why get this kind of error.


Reply all
Reply to author
Forward
0 new messages