Dtronix / PDFiumCore

.NET Standard P/Invoke bindings for PDFium.
Apache License 2.0
145 stars 23 forks source link

How to pass buffers for reading of strings #14

Closed Smith00101010 closed 1 year ago

Smith00101010 commented 1 year ago

Hi,

thanks for your work on this project.

I am currently trying to use it to extract some information from an existing PDF file but I am wondering how I would prepare a buffer to pass to functions like fpdf_annot.FPDFAnnotGetFormFieldName() or fpdf_edit.FPDFTextObjGetText() which expect a buffer and the length of the buffer as parameters.

Would it be possible to provide an example for that? I was not able to find anything and other libraries seem to handle calls to such C functions in a differnt way, e.g. by allowing to pass a StringBuilder.

Smith00101010 commented 1 year ago

I figured it out. I already tried this before but some unrelated precondition was not met while I tried that. Now I am using

var outputArray = new ushort[1024];
var lengthInBytes = fpdf_annot.FPDFAnnotGetFormFieldName(formFill, currentAnnotation, ref outputArray[0], (uint)outputArray.Length);
var asBytes = new byte[lengthInBytes - 2];
Buffer.BlockCopy(outputArray, 0, asBytes, 0, (int)lengthInBytes) - 2;
var formFieldName = Encoding.Unicode.GetString(asBytes);

If there is a better way to do this, please let me know.