C # Add a watermark to a PDF file

Source: Internet
Author: User

Introduction to the types and functions of watermarks

The PDF watermark is divided into two kinds: text watermark and picture watermark. Text watermarks are generally used in the business world, reminding readers that the document is copyrighted and that other people cannot copy or use it for free. In addition to this feature, watermarks can also be used to mark this document

Some basic state information, such as draft status or final version? Image Watermark is a good choice for beautifying PDF files, it can be used as a background for PDF files with colorful and unique images. So, how to programmatically give a PDF file

Add a watermark? There are many implementations, one of the quickest and easiest ways to do this is with third-party software, such as spire.pdf. This article explains how to use free third-party software spire.pdf to add text watermarks and image watermarks to PDF files.

Free version spire.pdf software Introduction

The free version of the spire.pdf Software is a free, standalone PDF control that provides programmers with a wide range of PDF features such as read, write, new, edit, manipulate and convert PDF files via C # or vb.net. Please note that the free version only supports

10-page PDF file and three-page conversion capabilities.

How to get it?

First, download and install Spire.pdf from the E-iceblue website website. Once the installation is complete, you can get started quickly with "samplecenter" and interface help, with a lot of code snippets and detailed application features.

Here are some examples of how to add a picture watermark and text watermark to a PDF file. I divide it into two parts. Part is the picture watermark, the other part is the text watermark.

Part I: Adding a picture watermark

First, prepare a picture of the watermark you want to set as a PDF file. Second, we just need to call the Image.FromFile (stringfilename) method to load the picture, which is very simple and convenient. Then, set the PDF picture background.

The code is as follows:

Step 1: Create a new PDF instance. Then import the PDF file

New pdfdocument ();  Pdf. LoadFromFile ("sample.pdf");


Step 2: Get the first page of the PDF file

Pdfpagebase page = PDF. pages[0];


Step 3: Import the picture and set it to the background of the PDF file

Image img = image.fromfile ("img.jpg");   = img;

Step 4: Save the file in PDF format, named "Imagewatermark.pdf"

Pdf. SaveToFile ("imagewatermark.pdf");


The image watermark was added as follows :


Figure 1: Picture watermark

Part Two: Adding a text watermark

Unlike adding a picture watermark, adding a text watermark is more complex. In order to best match the PDF page, we need to make the printed text in the PDF, then set the text font, color, location and text format. Both of these functions can be

This method has been called to implement quickly:drawstring (strings, pdffontbase font, Pdfbrush brush, float x, float y, pdfstringformat format). Here is the code snippet :

Step 1: Create a new PDF instance. Then import the PDF file.

New pdfdocument ();pd F. LoadFromFile ("sample.pdf");

Step 2: get The first page of a PDF file

Pdfpagebase page = PDF. pages[0];

Step 3: Add a text watermark to the first page of a file, formatting text

Pdftilingbrush brush =NewPdftilingbrush (NewSizeF (page. Canvas.ClientSize.Width/2, page. Canvas.ClientSize.Height/3)); Brush. Graphics.settransparency (0.3f); Brush.  Graphics.save (); Brush. Graphics.translatetransform (brush. Size.width/2, Brush. Size.Height/2); Brush. Graphics.rotatetransform (- $); Brush. Graphics.DrawString ("Draft Version",NewPdffont (Pdffontfamily.helvetica, -), Pdfbrushes.blue,0,0,NewPdfstringformat (Pdftextalignment.center)); Brush.  Graphics.restore (); Brush. Graphics.settransparency (1); Page. Canvas.drawrectangle (Brush,NewRectangleF (NewPointF (0,0), page. Canvas.clientsize));

Step 4: Save the file in PDF format, named "Textwatermark.pdf"

Pdf. SaveToFile ("textwatermark.pdf");

The text watermark was added as follows:


Figure 2: Text watermark

Summary

Although there are many articles about how to add watermarks programmatically without using third-party software, I still use the free version of Spire.pdf software, because I need to use new, convert, print and

Protect features such as PDFs, all of which are supported by this software. It works very well and greatly improves my work efficiency. If you are interested, try it.

Thank you for your visit, I hope this article can bring you some help.

C # Add a watermark to a PDF file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.