Go - ImageCreate from []bytes already in memory

I’m working on some Go code that is placing a signature that is starting out as a Base64 encoded string and I’m trying to get it into a signature widget.

I can successfully produce the desired result, but I’m having to save it to a file first. I’m hoping to be able to skip that step.

q := highlights.GetCurrentQuads().Get(0)

// Find where the annotation should be placed
offset := -25.0
left := math.Min(math.Min(math.Min(q.GetP1().GetX(), q.GetP2().GetX()), q.GetP3().GetX()), q.GetP4().GetX())
right := math.Max(math.Max(math.Max(q.GetP1().GetX(), q.GetP2().GetX()), q.GetP3().GetX()), q.GetP4().GetX())
bottom := math.Min(math.Min(math.Min(q.GetP1().GetY(), q.GetP2().GetY()), q.GetP3().GetY()), q.GetP4().GetY())
top := math.Max(math.Max(math.Max(q.GetP1().GetY(), q.GetP2().GetY()), q.GetP3().GetY()), q.GetP4().GetY())
coordinates := pdftron.NewRect(left, bottom+offset+float64(fields[i].Height), right, top+offset)

imgAsBytes, err := utils.ConvertBase64ToBytes(*fields[i].Content)
if err != nil {
	panic(err) // TODO
}

in_sig_field_name := "./out.png"
tmpFile, err := os.OpenFile(in_sig_field_name, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
	fmt.Println("Unable to open file")
}

_, err = tmpFile.Write(imgAsBytes)
if err != nil {
	panic(err) // TODO
}

tmpFile.Close()

widgetAnnot := pdftron.SignatureWidgetCreate(d.doc, coordinates)
// signatureImage := pdftron.ImageCreate(d.doc.GetSDFDoc(), in_sig_field_name)
signatureImage := pdftron.ImageCreate(d.doc.GetSDFDoc(), imgAsBytes) // Trying to access the constructor linked below
widgetAnnot.CreateSignatureAppearance(signatureImage)
widgetAnnot.RefreshAppearance()

pages.Current().AnnotPushBack(widgetAnnot)

In the above code, if I uncomment the line that loads the image from a filename and comment-out the line below it, I get the desired result.

I believe I need to access the Image.create(Doc doc, byte[] image_data) constructor, but when I try to call it, I get the following error message: interface conversion: []uint8 is not pdftron.Filter: missing method AttachFilter, which suggests I’m actually hitting the Image.create(Doc doc, Filter stream) constructor.

Am I going about this the right way? Any pointers would be very much appreciated.

Ben

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:Forums:

Thank you for contacting us about this. Our go API is based on the C++ api, as such the appropriate method is the following Image.Create function:
https://www.pdftron.com/api/PDFTronSDK/cpp/classpdftron_1_1_p_d_f_1_1_image.html#a4935d847af458be4ce02b0924fe5b588

Thanks @shakthi124 That solves my problem.

I really appreciate it!

Ben