C # How to add a watermark to a PDF file _c# tutorial

Source: Internet
Author: User

A description of the types and functions of watermarks

There are two kinds of PDF watermarking: text watermark and image watermark. Text watermarks are generally used in the business world, reminding readers that the document is copyrighted and that others cannot copy it 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? A picture watermark is a great choice for beautifying a PDF file, and it can be used as a background for a PDF file with colorful, unique images. So, how to programmatically give PDF files

Add a watermark? There are a number of implementations, one of the quickest and easiest ways to use third party software, such as Spire.pdf. This article will explain how to use the free Third-party software spire.pdf to add text watermarks and image watermarks to PDF files.

free version spire.pdf software introduction

Free version of the Spire.pdf software is a free independent PDF control, which provides programmers a wide range of PDF features, such as reading, writing, new, editing, manipulating and translating PDF files via C # or vb.net. Please note that the Free edition is only supported

Holds 10-page PDF files and three-page conversion capabilities.

How to get it?

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

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

Part I: Add a picture watermark

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

The code is as follows:

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

Pdfdocument PDF = 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"); 
Page. BackgroundImage = img; 

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

Pdf. SaveToFile ("Imagewatermark.pdf");

The effect of adding a picture watermark is as follows:


Picture 1: Image 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 print text in the PDF, and then set the font, color, position and text format of the text. Both of the above functions can pass

This method is invoked quickly: drawstring (strings, pdffontbase font, Pdfbrush brush, float x, float y, pdfstringformat format). Here is the code fragment:

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

Pdfdocument pdf= new Pdfdocument ();
Pdf. LoadFromFile ("Sample.pdf"); 

Step 2: get the first page of the PDF file

Pdfpagebase page = PDF. Pages[0];

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

Pdftilingbrush brush = new Pdftilingbrush (new SizeF 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 ( -45); 
Brush. Graphics.DrawString ("Draft Version", New Pdffont (Pdffontfamily.helvetica,), pdfbrushes.blue,0, 0, new Pdfstringformat (Pdftextalignment.center)); 
Brush. Graphics.restore (); 
Brush. Graphics.settransparency (1); 
Page. Canvas.drawrectangle (Brush, New RectangleF (new PointF (0, 0), page. Canvas.clientsize));

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

Pdf. SaveToFile ("Textwatermark.pdf");

The effect of adding a text watermark is as follows:

Picture 2: Text watermark

Summarize

Although there are many articles on the use of Third-party software can be programmed to add a watermark method, but here I still use the free version of the Spire.pdf software, because in addition to the watermark function, I also need to use the new, transform, print and

Protect PDFs and the like, and this software all supports these features. It works very well, and greatly improves my work efficiency. If you are interested, try it.

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

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.