For an electronic publication of my thesis I need to remove two chapters due to copyright restrictions. However, I would still like to have everything else look exactly the same (table of content, list of figures, tables, ...).
At the moment, I create a second version of the PDF in which I replace the content of the corresponding pages:
\newpage Content not included due to copyright restrictions. Please refer to the original article in ...
As this affects the table of content and the lists, I merge parts of the original document and the new one to the final one:
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dFirstPage=1 -dLastPage=53 -sOutputFile=output_01.pdf thesis.pdf
...
#merge all files
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=thesis_blackened.pdf output_01.pdf output_01_black.pdf output_02.pdf output_02_black.pdf output_03.pdf
The result I get is almost exactly what I need.
Unfortunately, the listing of the structure and the hyperlinks are a little messed up.
Is there a way of not showing content of a latex file in the PDF but still consider it in the listings?
Another option I can think of would be to use some other program to erase the content in the thesis PDF itself.
Is there another solution you could think of?

pdftk(command line) orpdfarranger(graphical interface) allows one to remove pages from a document and insert pages from another one. Not sure it will work better than what you did withgs, though. – frougon Jan 26 '20 at 12:01