Layering

This example demonstrates how to use layer.

We often need different levels at which we can place an object or image file

Clicking generate button will launch a pdf on which you can see one layer is placed on another layer .

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;

namespace OfficeComponent.Samples
{
    class LayeringExample : ExampleBase
    {
        public LayeringExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {
        }

        public LayeringExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {
        }

        public override string Execute()
        {
            // Create a new instance of PdfDocument class.
            PdfDocument doc = new PdfDocument();
            doc.PageSettings = new PdfPageSettings(new SizeF(450, 300));

            PdfPage page = doc.Pages.Add();

            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);

            page.Graphics.DrawString("Layers Sample", font, PdfBrushes.Black, new PointF(150, 10));

            //Add the first layer
            page.Layers.Add();

            Color c1 = Color.FromArgb(0xff, 0x30, 0x76, 0xff);
            Color c2 = Color.FromArgb(0xff, 0x5a, 0x91, 0xfe);
            Color c3 = Color.FromArgb(0xff, 0xa3, 0xc1, 0xfd);
            Color c4 = Color.FromArgb(0xff, 0xda, 0xe6, 0xfc);

            PdfGraphics graphics = page.DefaultLayer.Graphics;
            graphics.TranslateTransform(50, 60);

            //Draw Arc 
            PdfPen pen = new PdfPen(c1, 50);
            RectangleF rect = new RectangleF(0, 0, 50, 50);
            graphics.DrawArc(pen, rect, 360, 360);

            pen = new PdfPen(c2, 30);
            graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360);

            pen = new PdfPen(c3, 20);
            graphics.DrawArc(pen, rect, 360, 360);

            pen = new PdfPen(c4, 10);
            graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360);

            //Add another layer on the page
            page.Layers.Add();

            //Increment the layer
            page.DefaultLayerIndex += 1;
            graphics = page.DefaultLayer.Graphics;
            graphics.TranslateTransform(150, 50);
            graphics.SkewTransform(0, 30);
            graphics.ScaleTransform(0.3f, 0.3f);

            //Draw another set of elements
            //pen = new PdfPen(c1, 50);
            //graphics.DrawArc(pen, rect, 360, 360);
            //pen = new PdfPen(c2, 30);
            //graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360);
            //pen = new PdfPen(c3, 20);
            //graphics.DrawArc(pen, rect, 360, 360);
            //pen = new PdfPen(c4, 10);
            //graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360);

            string tifImage = CommonDataPath + "\\256.tif";
            PdfImage image = new PdfBitmap(tifImage);
            graphics.DrawImage(image, 0, 0);

            //Add another layer
            page.Layers.Add();
            page.DefaultLayerIndex += 1;
            graphics = page.DefaultLayer.Graphics;
            graphics.TranslateTransform(320, 70);

            const int a1 = -80;
            const int a2 = 160;

            //Draw another set of elements.
            pen = new PdfPen(c1, 50);
            graphics.DrawArc(pen, rect, a1, a2);
            pen = new PdfPen(c2, 30);
            graphics.DrawArc(pen, 0, 0, 50, 50, a1, a2);
            pen = new PdfPen(c3, 20);
            graphics.DrawArc(pen, rect, a1, a2);
            pen = new PdfPen(c4, 10);
            graphics.DrawArc(pen, 0, 0, 50, 50, a1, a2);
            
            // Save and close the document.
            var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
            doc.Save(outputPath);
            doc.Close(true);

            return outputPath;
        }
    }
}
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics

Namespace OfficeComponent.Samples
	Friend Class LayeringExample
		Inherits ExampleBase
		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)
		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)
		End Sub

		Public Overrides Function Execute() As String
			' Create a new instance of PdfDocument class.
			Dim doc As New PdfDocument()
			doc.PageSettings = New PdfPageSettings(New SizeF(450, 300))

			Dim page As PdfPage = doc.Pages.Add()

			Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 16)

			page.Graphics.DrawString("Layers Sample", font, PdfBrushes.Black, New PointF(150, 10))

			'Add the first layer
			page.Layers.Add()

			Dim c1 As Color = Color.FromArgb(&Hff, &H30, &H76, &Hff)
			Dim c2 As Color = Color.FromArgb(&Hff, &H5a, &H91, &Hfe)
			Dim c3 As Color = Color.FromArgb(&Hff, &Ha3, &Hc1, &Hfd)
			Dim c4 As Color = Color.FromArgb(&Hff, &Hda, &He6, &Hfc)

			Dim graphics As PdfGraphics = page.DefaultLayer.Graphics
			graphics.TranslateTransform(50, 60)

			'Draw Arc 
			Dim pen As New PdfPen(c1, 50)
			Dim rect As New RectangleF(0, 0, 50, 50)
			graphics.DrawArc(pen, rect, 360, 360)

			pen = New PdfPen(c2, 30)
			graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360)

			pen = New PdfPen(c3, 20)
			graphics.DrawArc(pen, rect, 360, 360)

			pen = New PdfPen(c4, 10)
			graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360)

			'Add another layer on the page
			page.Layers.Add()

			'Increment the layer
			page.DefaultLayerIndex += 1
			graphics = page.DefaultLayer.Graphics
			graphics.TranslateTransform(150, 50)
			graphics.SkewTransform(0, 30)
			graphics.ScaleTransform(0.3F, 0.3F)

			'Draw another set of elements
			'pen = new PdfPen(c1, 50);
			'graphics.DrawArc(pen, rect, 360, 360);
			'pen = new PdfPen(c2, 30);
			'graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360);
			'pen = new PdfPen(c3, 20);
			'graphics.DrawArc(pen, rect, 360, 360);
			'pen = new PdfPen(c4, 10);
			'graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360);

			Dim tifImage As String = CommonDataPath & "\256.tif"
			Dim image As PdfImage = New PdfBitmap(tifImage)
			graphics.DrawImage(image, 0, 0)

			'Add another layer
			page.Layers.Add()
			page.DefaultLayerIndex += 1
			graphics = page.DefaultLayer.Graphics
			graphics.TranslateTransform(320, 70)

			Const a1 As Integer = -80
			Const a2 As Integer = 160

			'Draw another set of elements.
			pen = New PdfPen(c1, 50)
			graphics.DrawArc(pen, rect, a1, a2)
			pen = New PdfPen(c2, 30)
			graphics.DrawArc(pen, 0, 0, 50, 50, a1, a2)
			pen = New PdfPen(c3, 20)
			graphics.DrawArc(pen, rect, a1, a2)
			pen = New PdfPen(c4, 10)
			graphics.DrawArc(pen, 0, 0, 50, 50, a1, a2)

			' Save and close the document.
			Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
			doc.Save(outputPath)
			doc.Close(True)

			Return outputPath
		End Function
	End Class
End Namespace