透かしの種類と機能の紹介

PDFウォーターマークには、テキストウォーターマークとイメージウォーターマークの2種類があります。 テキストの透かしは、一般的に商業分野で使用され、著作者がその文書が著作権で保護されていること、および他者がその文書を無償でコピーまたは使用することができないことを通知する。 この機能に加えて、ウォーターマークを使用して、ドラフトステータスや最終バージョンなど、このドキュメントの基本的なステータス情報をマークすることもできます。 イメージウォーターマーキングは、PDFファイルの美しさに適しています。カラフルでユニークな画像をPDFファイルの背景として使用できます。 では、PDFファイルにウォーターマークをプログラムで追加する方法は? この記事では、ウォーターマークを追加する方法について詳しく説明します。

必要なツールFree Spire.PDF for .NET

注:このコンポーネントをダウンロードしてインストールし、Spire.PDF for .NETのリファレンスを追加して、名前空間に追加する必要があります

1.   テキスト透かし

C#

using Spire.Pdf;

using Spire.Pdf.Graphics;

using System.Drawing;

 

namespace Watermark_PDF

{

    class Program

    {

        static void Main(string[] args)

        {

            //create an object of PdfDocument class and load the PDF file

            PdfDocument pdf = new PdfDocument();

            pdf.LoadFromFile("sample.pdf");

 

            //get the first page of PDF

            PdfPageBase page = pdf.Pages[0];

 

            //add watermark into the first page and set the formatting

            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, 24), 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));

 

            //save to file and open the result  

            pdf.SaveToFile("TextWaterMark.pdf");

            System.Diagnostics.Process.Start("TextWaterMark.pdf");

 

        }

    }

}

 

2.  画像の透かし

C#

using Spire.Pdf;

using System.Drawing;

 

namespace ImageWatermark_PDF

{

    class Program

    {

        static void Main(string[] args)

        {

            //create an object of PdfDocument class and load PDF file

            PdfDocument pdf = new PdfDocument();

            pdf.LoadFromFile("sample.pdf");

 

            //Get the first page

            PdfPageBase page = pdf.Pages[0];

 

            //load the image and set the image as watermark

            Image img = Image.FromFile("img.jpg");

            page.BackgroundImage = img;

 

            //save to file and open the result

            pdf.SaveToFile("ImageWaterMark.pdf");

 

        }

    }

}