本文介绍了更改PDF文件的文本和背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想在PDF文档中以编程方式更改背景颜色和文本颜色,以便在晚上阅读更好(类似于Adobe Reader:编辑 - >首选项 - >辅助功能 - >替换文档颜色)。 有没有好的命令行工具或API的Windows可以做到这一点? 到目前为止,我还没有找到任何。 解决方案没有办法直接做到这一点,与没有(免费软件或免费)工具,我知道。 (因为在一般情况下,您必须更改PDF页面的所有颜色,而不仅仅是背景颜色,因此您仍然可以有一些对比度和颜色差异。) 您为Adobe Reader描述的内容不会更改PDF文件本身,它会更改应用程序呈现页面的方式(通过反转颜色或类似方式)。在观看期间和之后,PDF保持不变。 但是,您可以通过应用合适的ICC颜色来实现类似的效果配置文件到输入PDF,并在(最新版本的)Ghostscript的帮助下生成一个新的输出PDF。 问题仍然是: strong> 为您的目的使用适当的ICC颜色配置文件 我很快考虑在 pdftk ... background ... 命令行。但这可能会使一些或许多PDF不可读。 ( 背景肯定会使其无法读取,因为大多数文字都是黑色的,而且会保持不变。) 要创建可用作灰色背景的PDF页面(A4大小),您可以使用Ghostscript: gs -o grey.pdf -sDEVICE = pdfwrite -g5950x8420 -c.8 setgray 0 0 595 842 rectfill showpage。 然后将其应用到您的原始PDF(A4): pdftk original.pdf background grey.pdf output orig- with-backgr.pdf 。 注意,这只会改变这些页面的背景的网页),其中原始背景是透明的,大多数基于文本的PDF是。 不适用于背景为不透明白色或彩色的页面或区域。) 您还可以通过ImageMagick的帮助很容易地实现永久的颜色变化(反色)。但这将同时嚼和制作你的漂亮的矢量PDF,将它们转换为完全光栅图像页面: convert nice.pdf -alpha off -invert inverted-colors-ugly-raster.pdf 。 一个相当不可靠的方法来完成颜色的反转与Ghostscript的帮助。它为输出PDF文件设置了一个 colortransfer 函数: gs -o output.pdf \\ \\ -sDEVICE = pdfwrite \ -c{1 exch sub} {1 exch sub} {1 exch sub} {1 exch sub} setcolortransfer\ -f input。 pdf 这是不可靠的,因为不是每个PDF查看器都会符合该设置 。 Adobe Reader Adobe Acrobat gv Ghostscript / gs Chrome的原生PDF渲染器('pdfium') 这些不显示反转颜色: Chrome with PDF.js Firefox with PDF.js Zathura MuPDF I'd like to change the background color and text color programmatically in PDF documents so that they're nicer to read at night (kinda like in Adobe Reader: Edit -> Preferences -> Accessibility -> Replace Document Colors).Is there any good command line tool or API for Windows that can do that?So far I haven't found any. It's OK if it needs to save the newly colored PDF into a new file. 解决方案 There is no way to do this directly, with no (Free Software or gratis) tool I'm aware of. (Because in the general case, you'll have to change all colors of the PDF pages, not just the background alone, so you can still have some contrast and color differences.)What you describe for Adobe Reader does not change the PDF file itself, it changes the way the application renders the pages (by inverting colors, or similar). The PDF remains the same during and after viewing it.However, you might be able to achieve a similar thing by applying a suitable ICC color profile to the input PDF and produce, with the help of (a very recent version of) Ghostscript, a new output PDF from this.The question would remain: what IS a "suitable" ICC color profil for your purpose?? I've shortly considered to apply a gray-ish background to the PDF with the help of pdftk ... background ... command line. But this would probably make some or many PDFs unreadable. (A black background would surely make it unreadable, because most text is black and would remain so.) To create a PDF page (A4 size) which could serve as the gray background, you could use Ghostscript: gs -o gray.pdf -sDEVICE=pdfwrite -g5950x8420 -c ".8 setgray 0 0 595 842 rectfill showpage". Then apply it to your original PDF (A4): pdftk original.pdf background gray.pdf output orig-with-backgr.pdf. Note, this will only change the background of these pages (or those areas of pages), where the original background is transparent, as most text-based PDFs are. It will not work for pages or areas where the background is opaque white or color.) You can also achieve a permanent color change (inverse colors) quite easily with the help of ImageMagick. But this will at the same time munch and make mincemeat of your nice vector PDFs, converting them into complete-raster image pages: convert nice.pdf -alpha off -invert inverted-colors-ugly-raster.pdf.Finally, here is a rather unreliable way to accomplish the inverting of colors with the help of Ghostscript. It sets up a colortransfer function for the output PDF file:gs -o output.pdf \ -sDEVICE=pdfwrite \ -c "{1 exch sub}{1 exch sub}{1 exch sub}{1 exch sub} setcolortransfer" \ -f input.pdfIt is unreliable, because not every PDF viewer will honor that setting. I've tested it a few times in the past...These viewers DO SHOW inverted colors:Adobe ReaderAdobe AcrobatgvGhostscript/gsChrome's native PDF renderer ('pdfium')These ones DON'T SHOW inverted colors:Chrome with PDF.jsFirefox with PDF.jsZathuraMuPDF 这篇关于更改PDF文件的文本和背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-22 23:58