Changing the text and background color of a PDF file

前端 未结 4 1712
旧巷少年郎
旧巷少年郎 2021-02-06 05:26

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 -> Prefer

相关标签:
4条回答
  • 2021-02-06 05:46
    1. Install nodejs.
    2. npm i -g serve
    3. In directory with pdfs run: serve
    4. Open http://localhost:5000 in Chrome and click on some file.
    5. Install Chrome extension Dark Reader
    6. Dark Reader > Toggle localhost:5000

    0 讨论(0)
  • 2021-02-06 05:48

    As you asked for an API, I'll throw one additional possibility in the mix. It is actually possible to write a plug-in for Adobe Acrobat (should be possible for Adobe Reader too, but Reader plug-ins are more difficult) that interferes with display.

    A long time ago I wrote code for Enfocus PitStop to implement a wireframe rendering mode for PDF files inside of Adobe Acrobat. Click a button and the display changes to wireframe, click again and you have your normal view. This works because you can (as a plug-in) modify the display list (the list of objects) drawn by Acrobat.

    This means that to draw your special display mode you could create a new display list (or modify the existing one) so that it has a rectangle at the very back in the color that you want and then modify the color of all objects in the display list to suit your needs.

    This is relatively complex as it is, what makes it more complex is that - if you don't want your changes to affect the PDF file on disk, you have to intercept a myriad of Acrobat notifications and undo your changes. For example, if the user attempts to save the PDF document while viewing in your display mode, you have to make sure you are warned about that and undo the changes during the save. Adobe Acrobat makes this possible because it sends you notifications before and after the save process but it's still a serious job to make sure nothing gets screwed up.

    But it's a absolutely cool and very flexible way to implement what you were after. Just make sure you have more than a couple of weeks to implement it :)

    0 讨论(0)
  • 2021-02-06 06:05

    Just in case the OP doesn't really need to change the PDF document colors permanently, but only wants a PDF viewer other than Acrobat that can do similarly change the displayed colors...

    MuPDF: MuPDF is a lightweight PDF viewer (amongst other things). It can invert the displayed colors with the simple stroke of the i for any document while it is open. MuPDF is also available for Windows (and iOS, and Android, and OSX, and Linux). (It is made by the same company which brought us Ghostscript).

    MuPDF screenshots here: "normal" view (left) and "inverted" view (right)

     

    SumatraPDF: This is a very popular alternative PDF viewer for Windows. Its PDF rendering engine is based on MuPDF. Hitting . in presentation mode, it changes background to black. Hitting w in presentation mode, it changes background to white. (I don't think it can also invert all colors, but I do not have the latest release available right now to check.) For startup adding -invert-colors to the command line, it will invert the colors for the rendered document.

    Zathura: A lightweight PDF viewer for Linux and OSX, which can be controlled by Vim-like keyboard shortcuts. ctrl+r will re-color the rendering of any opened document. Background will change to dark, texts will change to bright gray (however it will not invert a, say blue text to yellow, like MuPDF does). I'm not sure if it is available on Windows too.

    Evince: The Gnome PDF viewer, available for Linux, OSX and Windows. It can invert the colors of the open document too; the keyboard shortcut is ctrl+i.

    XPDF: XPDF is quite an ancient PDF viewer for Unix + Linux (not sure if there is a Windows version available -- maybe in Cygwin). It has a startup option in its command line: xpdf -rv -papercolor "#333333" file.pdf will invert the colors (-rv is for reverse video, -papercolor lets you change the background to something different from pure black [as any inverted white would become]).

    0 讨论(0)
  • 2021-02-06 06:10

    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.pdf
    

    It 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:

    1. Adobe Reader
    2. Adobe Acrobat
    3. gv
    4. Ghostscript/gs
    5. Chrome's native PDF renderer ('pdfium')

    These ones DON'T SHOW inverted colors:

    1. Chrome with PDF.js
    2. Firefox with PDF.js
    3. Zathura
    4. MuPDF
    0 讨论(0)
提交回复
热议问题