Page Settings

This example demonstrates how to use set page Settings

Page settings can be made for pdf document

Below settinsg can be set

  • page size
  • viewer prefernce
  • page orientation
  • page margins
  • page section
  • page rotate
  • page number

Click generae button will launch the pdf with above page settings

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

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

        public PageSettingsExample(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();
            PdfSection section;

            // Create few sections with one page in each.
            for (int i = 0; i < 5; ++i)
            {
                section = doc.Sections.Add();

                //Create page label
                PdfPageLabel label = new PdfPageLabel();

                label.Prefix = "Sec" + i + "-";
                section.PageLabel = label;
                section.Pages.Add();
                section.Pages[0].Graphics.SetTransparency(0.55f);
                section.PageSettings.Transition.PageDuration = 1;
                section.PageSettings.Transition.Duration = 1;
                section.PageSettings.Transition.Style = PdfTransitionEffect.Box;
            }

            //Set page size
            doc.PageSettings.Size = PdfPageSize.A6;

            //Set viewer prefernce.
            doc.ViewerPreferences.HideToolbar = true;
            doc.ViewerPreferences.PageMode = PdfPageDisplayMode.DisplayFullScreen;

            //Set page orientation
            doc.PageSettings.Orientation = PdfPageOrientation.Landscape;

            //Create a brush
            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
            brush.Color = new PdfColor(System.Drawing.Color.FromArgb(0xa0, 0xa0, 0xa0));

            //Create a Rectangle
            PdfRectangle rect = new PdfRectangle(0, 0, 1000f, 1000f);
            rect.Brush = brush;
            PdfPen pen = new PdfPen(System.Drawing.Color.Blue);
            pen.Width = 6f;

            //Get the first page in first section
            PdfPage page = doc.Sections[0].Pages[0];

            //Draw the rectangle
            rect.Draw(page.Graphics);

            // Add margins.
            doc.PageSettings.SetMargins(0f);

            //Get the first page in second section
            page = doc.Sections[1].Pages[0];
            doc.Sections[1].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
            rect.Draw(page.Graphics);

            // Change the angle f the section. This should rotate the previous page.
            doc.Sections[2].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180;
            page = doc.Sections[2].Pages[0];
            rect.Draw(page.Graphics);

            // Change the angle f the section. This should rotate the previous page.
            doc.Sections[3].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle270;
            page = doc.Sections[3].Pages[0];
            rect.Draw(page.Graphics);

            section = doc.Sections[4];
            section.PageSettings.Orientation = PdfPageOrientation.Portrait;
            page = section.Pages[0];
            rect.Draw(page.Graphics);

            //Set the font
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16f);
            PdfSolidBrush fieldBrush = new PdfSolidBrush(Color.Black);

            //Create page number field
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, fieldBrush);

            //Create page count field
            PdfPageCountField count = new PdfPageCountField(font, fieldBrush);

            //Draw page template
            PdfPageTemplateElement templateElement = new PdfPageTemplateElement(400, 400);
            templateElement.Graphics.DrawString("Page :\tof", font, PdfBrushes.Black, new PointF(260, 200));

            //Draw current page number
            pageNumber.Draw(templateElement.Graphics, new PointF(306, 200));

            //Draw number of pages
            count.Draw(templateElement.Graphics, new PointF(345, 200));
            doc.Template.Stamps.Add(templateElement);
            templateElement.Background = true;




            // 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.Fields
Imports OfficeComponent.Pdf.Graphics

Namespace OfficeComponent.Samples
	Friend Class PageSettingsExample
		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()
			Dim section As PdfSection

			' Create few sections with one page in each.
			For i As Integer = 0 To 4
				section = doc.Sections.Add()

				'Create page label
				Dim label As New PdfPageLabel()

				label.Prefix = "Sec" & i & "-"
				section.PageLabel = label
				section.Pages.Add()
				section.Pages(0).Graphics.SetTransparency(0.55F)
				section.PageSettings.Transition.PageDuration = 1
				section.PageSettings.Transition.Duration = 1
				section.PageSettings.Transition.Style = PdfTransitionEffect.Box
			Next i

			'Set page size
			doc.PageSettings.Size = PdfPageSize.A6

			'Set viewer prefernce.
			doc.ViewerPreferences.HideToolbar = True
			doc.ViewerPreferences.PageMode = PdfPageDisplayMode.DisplayFullScreen

			'Set page orientation
			doc.PageSettings.Orientation = PdfPageOrientation.Landscape

			'Create a brush
			Dim brush As New PdfSolidBrush(Color.Black)
			brush.Color = New PdfColor(System.Drawing.Color.FromArgb(&Ha0, &Ha0, &Ha0))

			'Create a Rectangle
			Dim rect As New PdfRectangle(0, 0, 1000F, 1000F)
			rect.Brush = brush
			Dim pen As New PdfPen(System.Drawing.Color.Blue)
			pen.Width = 6F

			'Get the first page in first section
			Dim page As PdfPage = doc.Sections(0).Pages(0)

			'Draw the rectangle
			rect.Draw(page.Graphics)

			' Add margins.
			doc.PageSettings.SetMargins(0F)

			'Get the first page in second section
			page = doc.Sections(1).Pages(0)
			doc.Sections(1).PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90
			rect.Draw(page.Graphics)

			' Change the angle f the section. This should rotate the previous page.
			doc.Sections(2).PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180
			page = doc.Sections(2).Pages(0)
			rect.Draw(page.Graphics)

			' Change the angle f the section. This should rotate the previous page.
			doc.Sections(3).PageSettings.Rotate = PdfPageRotateAngle.RotateAngle270
			page = doc.Sections(3).Pages(0)
			rect.Draw(page.Graphics)

			section = doc.Sections(4)
			section.PageSettings.Orientation = PdfPageOrientation.Portrait
			page = section.Pages(0)
			rect.Draw(page.Graphics)

			'Set the font
			Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 16F)
			Dim fieldBrush As New PdfSolidBrush(Color.Black)

			'Create page number field
			Dim pageNumber As New PdfPageNumberField(font, fieldBrush)

			'Create page count field
			Dim count As New PdfPageCountField(font, fieldBrush)

			'Draw page template
			Dim templateElement As New PdfPageTemplateElement(400, 400)
			templateElement.Graphics.DrawString("Page :" & vbTab & "of", font, PdfBrushes.Black, New PointF(260, 200))

			'Draw current page number
			pageNumber.Draw(templateElement.Graphics, New PointF(306, 200))

			'Draw number of pages
			count.Draw(templateElement.Graphics, New PointF(345, 200))
			doc.Template.Stamps.Add(templateElement)
			templateElement.Background = True




			' 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