Rasterize document or page

Watermarks in PDFs can be removed by third-party tools. If you need watermarks that are very hard to remove, rasterize the document: convert its pages to images so the content — including the watermark — becomes non-editable. Access the PDF content tree with Watermarker.get_content() and call rasterize().

Warning
Rasterization is irreversible: the original text and vector content cannot be restored afterwards, and the output file size usually increases.

Rasterize the whole document

The example adds a watermark, rasterizes every page to a PNG image at 100 DPI, and saves the result.

from groupdocs.watermark import Watermarker
from groupdocs.watermark.watermarks import TextWatermark, Font, Color
from groupdocs.watermark.options.pdf import PdfLoadOptions
from groupdocs.watermark.contents.pdf import PdfImageConversionFormat

def rasterize_document():
    with Watermarker("./document.pdf", PdfLoadOptions()) as watermarker:
        watermark = TextWatermark("CONFIDENTIAL", Font("Arial", 19.0))
        watermark.foreground_color = Color.red
        watermarker.add(watermark)

        # Rasterize every page at 100x100 DPI to PNG images
        content = watermarker.get_content()
        content.rasterize(100, 100, PdfImageConversionFormat.PNG)

        watermarker.save("./output.pdf")

if __name__ == "__main__":
    rasterize_document()

document.pdf is the sample file used in this example. Click here to download it.

Binary file (PDF, 1.1 MB)

Download full output

Rasterize a particular page

To rasterize a single page, call rasterize() on that page instead of the whole content:

from groupdocs.watermark import Watermarker
from groupdocs.watermark.options.pdf import PdfLoadOptions
from groupdocs.watermark.contents.pdf import PdfImageConversionFormat

def rasterize_page():
    with Watermarker("./document.pdf", PdfLoadOptions()) as watermarker:
        content = watermarker.get_content()
        content.pages[0].rasterize(100, 100, PdfImageConversionFormat.PNG)
        watermarker.save("./output.pdf")

if __name__ == "__main__":
    rasterize_page()

document.pdf is the sample file used in this example. Click here to download it.

Binary file (PDF, 570 KB)

Download full output

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.