I translated your example in the comments to something that compiles below.
But ...
You are using the wrong callback. If you look in the pdf you will see something like this:
%PDF-1.5 %ÌÕÁÔÅØÐÄÆ /Span <</ActualText (test)>> BDC EMC 3 0 obj << /Length 205 >>
This means that your BDC-operator ended outside the page stream. The pdf is broken.
The documentation describes the callback like this:
This callback is called whenever LuaTEX is ready to move stuff to the main vertical list. You can use this callback to do specialized manipulation of the page building stage like imposition or column balancing.
\documentclass{article} \usepackage{luacode} \pagestyle{empty} \begin{luacode} pdf.setcompresslevel(0) b=0 function f(a) texio.write_nl("Value "..a) if a=="new_graf" then if b%2==0 then pdf.print ("direct","/Span <</ActualText (test)>> BDC ") else pdf.print ("direct","EMC ") end b=b+1 end end luatexbase.add_to_callback("buildpage_filter",f,"filter") \end{luacode} \begin{document} paragraph one paragraph two \end{document}