How to write pdf object directly in PDF and get number of this object via LaTeX or expl3?

Via Lua I can write and use an object directly in PDF:

local num = pdf.immediateobj("stream", "\$$a^2+b^2=c^2\$$", "/Type /SomeType /Subtype /SomeSubtype")


How can I do the same via expl3 or simple LaTeX commands to make it work in pdfLaTeX and in LuaLaTeX?

You can use the l3pdf commands. I did not check if the syntax of the content of the stream is correct:

\documentclass{article}
\usepackage{l3pdf}
\ExplSyntaxOn
\pdf_uncompress:
\ExplSyntaxOff
\begin{document}
\ExplSyntaxOn
\pdf_object_new:nn {mystream}{stream}
\pdf_object_write:nn {mystream}
{{/Type /SomeType /Subtype /SomeSubtype}
{$$a^2+b^2=c^2$$}}
\pdf_object_ref:n {mystream}
some text ...

%to get only the number as requested in the comments
% make only sense for pdflatex and lualatex
\tl_set:Nx\l_tmpa_tl { \pdf_object_ref:n {mystream} }
\seq_set_split:NnV \l_tmpa_seq {~} { \l_tmpa_tl }
\seq_item:Nn\l_tmpa_seq{1}
\ExplSyntaxOff
\end{document}

• as i understood,\pdf_object_ref:n {mystream} function allow to me get number of this object? – Aleksandr Kozlovskiy Oct 20 at 16:29
• It doesn't give the number but a reference, so e.g. 1 0 R with pdflatex and luatex, @pdf.obj1 for xelatex, and {pdf.obj1} with latex+dvips. – Ulrike Fischer Oct 20 at 16:36
• But i want to get only number,for example 1,but not 1 0 r,because sometimes instead of r it's possible write obj,so the best variant for me,it get only number of object such as in pdf.immediateobj function in lua. – Aleksandr Kozlovskiy Oct 20 at 22:51
• To write the object you can use the name (mystream in my example). – Ulrike Fischer Oct 20 at 23:14
• yes,but i mean,that i want to get number of object,e.g 1 instead of 1 0 r. If exists any way to do this,or it works only for lua? – Aleksandr Kozlovskiy Oct 20 at 23:46