Booklet

This example demonstrates how to create booklet.

Source PDF file:
Page Settings
Width: pt Height: pt

Booklet is very usefull when someone needs to express about a certain subject . In few paper in our case it can be a hard copy or a e booklet

pdf source

area we choose a pdf file which we need it to be converted to pdf booklet.

page settings

  • Page Widthyou can setup a page width using this textbox

  • Page Heightyou can setup a page height using this textbox

  • Double sidea booklet can be double sided and this check allows you to decide

After clicking generate it will create a new pdf file with pages depending upon the above settings

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;

namespace OfficeComponent.Samples
{
    class BookletExample : ExampleBase
#if WEB
, IUIExample
#endif
    {
        #region Input

        public string SourcePdf { get; set; }
        public float Width { get; set; }
        public float Height { get; set; }
        public bool TwoSide { get; set; }

        #endregion

        public BookletExample()
            : base(null,null)
        {

        }

        public BookletExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public BookletExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {

        }

        public override string Execute()
        {
#if WEB
            ProcessForm();
#endif
            if (string.IsNullOrWhiteSpace(SourcePdf))
            {
                ShowError("Please specify a PDF document.");
                return null;
            }

            // Create a new instance of PdfDocument class.
            PdfDocument doc = new PdfDocument();

            var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
            using (PdfImportedDocument ldoc = new PdfImportedDocument(SourcePdf))
            {
                //Create booklet with two sides               
                doc = PdfBookletUtil.CreateBooklet(ldoc, new SizeF(Width, Height), TwoSide);
                
                //Save the document
                doc.Save(outputPath);
                doc.Close(true);
            }

            return outputPath;
        }

        public override String ActionTitle 
        {
            get
            {
                return "Create Booklet";
            }
        }

#if WEB
        void ProcessForm()
        {
            SourcePdf = GetPostFile("SourceFile");
        }
#endif
    }
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text

Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics

Namespace OfficeComponent.Samples
#If WEB Then
	Friend Class BookletExample
		Inherits ExampleBase
		Implements IUIExample
#Else
	Friend Class BookletExample
		Inherits ExampleBase
#End If
		#Region "Input"

		Private privateSourcePdf As String
		Public Property SourcePdf() As String
			Get
				Return privateSourcePdf
			End Get
			Set(ByVal value As String)
				privateSourcePdf = value
			End Set
		End Property
		Private privateWidth As Single
		Public Property Width() As Single
			Get
				Return privateWidth
			End Get
			Set(ByVal value As Single)
				privateWidth = value
			End Set
		End Property
		Private privateHeight As Single
		Public Property Height() As Single
			Get
				Return privateHeight
			End Get
			Set(ByVal value As Single)
				privateHeight = value
			End Set
		End Property
		Private privateTwoSide As Boolean
		Public Property TwoSide() As Boolean
			Get
				Return privateTwoSide
			End Get
			Set(ByVal value As Boolean)
				privateTwoSide = value
			End Set
		End Property

		#End Region

		Public Sub New()
			MyBase.New(Nothing,Nothing)

		End Sub

		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
#If WEB Then
			ProcessForm()
#End If
			If String.IsNullOrWhiteSpace(SourcePdf) Then
				ShowError("Please specify a PDF document.")
				Return Nothing
			End If

			' Create a new instance of PdfDocument class.
			Dim doc As New PdfDocument()

			Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
			Using ldoc As New PdfImportedDocument(SourcePdf)
				'Create booklet with two sides               
				doc = PdfBookletUtil.CreateBooklet(ldoc, New SizeF(Width, Height), TwoSide)

				'Save the document
				doc.Save(outputPath)
				doc.Close(True)
			End Using

			Return outputPath
		End Function

		Public Overrides ReadOnly Property ActionTitle() As String
			Get
				Return "Create Booklet"
			End Get
		End Property

#If WEB Then
		Private Sub ProcessForm()
			SourcePdf = GetPostFile("SourceFile")
		End Sub
#End If
	End Class
End Namespace