# How use command \pdfextension in luacode enviroment of package luacode?

When I try to compile, for example, this code in my document:

\begin{luacode}
\pdfextension literal direct{/Span <</ActualText (First paragraph on page one)>> BDC}
tex.print("my text")
\pdfextension literal direct{EMC}
\end{luacode}


I have a problem with the compilation of the document.

How I can use the command \pdfextension in Lua code?

• please always make it easy for people to help you by providing a test file that reproduces the error. – David Carlisle Mar 20 at 20:09
• You can't/shouldn't use luatex tex-commands like \pdfextension inside a luacode environment, it is only for luacode. – Ulrike Fischer Mar 20 at 20:17
• But what command i can use in luacode enviroment,to add span in the page. – Aleksandr Kozlovskiy Mar 20 at 21:04
• You should make a complete example so that it is clear where in the page stream you want to inject the BDC operator. – Ulrike Fischer Mar 20 at 22:13
• Don't put code in the comments. Edit your question. And if you want me to see your comment you should ping me with @UlrikeFischer. – Ulrike Fischer Mar 20 at 23:27

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