How to Set Graphics Overlay in PDF – C#/VB.NET

In order to have a wonderful and appealed appearance, people use some special effects on images, which are often used in advertisements. Also, we can apply images with special effects to our documents. In this post, I will show the method about how to overlay one image on another one in PDF by using C#/VB.NET.

Actually, graphic overlay in PDF doesn’t mean that the upper image will cover lower one, but the lower can be regarded as background image with certain transparency or watermark. In my example, I prepare two PDF documents. Each of them has one page. What I will do is to overlay image in document 2 on image in document 1.

                                                G.JPG in Doc 2                                                                         F.JPG in Doc 1

Steps:

Before starting with steps, I must notice that I use Spire.PDF in this example so that I add its dll file as reference in my project.

  1. Load document 1 and document 2 from my computer.
  2. Create a template based on document 1 pages.
  3. Use for loop to overlay pages in document 2 on document 1. Although document 2 has only one page, we should suppose that there are several pages in this document. In this for loop, I set transparency and draw template. In SetTransparency method, I pass three parameters, pen, brush and blend mood, while, in DrawTemplate method, there are two parameters, template which I have created in step 2 and location.
  4. Save and Launch file.

Main Coding:

C#

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

namespace GraphicsOverlay

{

    class overlay

    {

        static void Main(string[] args)

        {

            //Load Document

            PdfDocument doc1 = new PdfDocument();

            doc1.LoadFromFile(@”D:\work\My Documents\F.pdf”);

            PdfDocument doc2 = new PdfDocument();

            doc2.LoadFromFile(@”D:\work\My Documents\G.pdf”);

            //Create Template

            PdfTemplate template = doc1.Pages[0].CreateTemplate();

            foreach (PdfPageBase page in doc2.Pages)

            {

                page.Canvas.SetTransparency(0.55f, 0.55f, PdfBlendMode.Overlay);

                page.Canvas.DrawTemplate(template, PointF.Empty);

            }

            //Save

            doc2.SaveToFile(“Overlay.pdf”);

            doc1.Close();

            doc2.Close();

            //Launch

            System.Diagnostics.Process.Start(“Overlay.pdf”);

        }

    }

}

VB.NET

Imports System

Imports System.Drawing

Imports Spire.Pdf

Imports Spire.Pdf.Graphics

NamespaceGraphicsOverlay

       Friend Class overlay

              Shared Sub Main(ByVal args() As String)

                     ‘Load Document

                     Dim doc1 As New PdfDocument()

                     doc1.LoadFromFile(“D:\work\My Documents\F.pdf”)

                     Dim doc2 As New PdfDocument()

                     doc2.LoadFromFile(“D:\work\My Documents\G.pdf”)

                     ‘Create Template

                     Dim template As PdfTemplate = doc1.Pages(0).CreateTemplate()

                     For Each page As PdfPageBase In doc2.Pages

                           page.Canvas.SetTransparency(0.55f, 0.55f, PdfBlendMode.Overlay)

                           page.Canvas.DrawTemplate(template, PointF.Empty)

Nextpage

‘Save

                     doc2.SaveToFile(“Overlay.pdf”)

doc1.Close()

                     doc2.Close()

‘Launch

                     System.Diagnostics.Process.Start(“Overlay.pdf”)

              End Sub

       End Class

End Namespace

Result Shown by Following:

___________________________________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s