Thursday, 24 January 2013 03:08

Create PDF Booklet in C#/VB.NET

PDF booklet is pretty helpful when people print a huge PDF document. It enjoys special popularity among book, newspaper and magazine editors. This section will introduce a very simple way to create PDF booklet via a .NET PDF component in C#, VB.NET.

Spire.PDF for .NET is a .NET PDF library which can manipulate PDF documents without Adobe Acrobat or any third party library. Using this PDF component, you can quickly create PDF booklet in your .NET applications. After setting the PDF page width and height by a class Spire.Pdf.PdfPageSize, you can create your PDF booklet through implementing PdfDocument.CreateBooklet(string fileName, float width, float height, bool doubleSide) directly. Below picture shows the effect of this task:

Create PDF Booklet

Here you can quickly download Spire.PDF for .NET and install it on your system. After adding Spire.Pdf reference, please see the detail code of PDF booklet below.

Draw PDF Barcode in C#, VB.NET

[C#]
using System.Drawing;
using Spire.Pdf;

namespace PDF_Booklet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load a PDF file
            PdfDocument doc = new PdfDocument();
            String srcPdf = @"..\read PDF.pdf";

            //Create PDF booklet
            float width = PdfPageSize.A4.Width * 2;
            float height = PdfPageSize.A4.Height;
            doc.CreateBooklet(srcPdf, width, height, true);

            //Save pdf file.
            doc.SaveToFile("Booklet.pdf");
            doc.Close();
            //Launching the Pdf file.
            System.Diagnostics.Process.Start("Booklet.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf

Namespace PDF_Booklet
	Class Program
		Private Shared Sub Main(args As String())
			'Load a PDF file
			Dim doc As New PdfDocument()
			Dim srcPdf As [String] = "..\read PDF.pdf"

			'Create PDF booklet
			Dim width As Single = PdfPageSize.A4.Width * 2
			Dim height As Single = PdfPageSize.A4.Height
			doc.CreateBooklet(srcPdf, width, height, True)

			'Save pdf file.
			doc.SaveToFile("Booklet.pdf")
			doc.Close()
			'Launching the Pdf file.
			System.Diagnostics.Process.Start("Booklet.pdf")
		End Sub
	End Class
End Namespace

Spire.PDF for .NET is a PDF component that enables you to create, read, edit and manipulate PDF files in C#, VB.NET

Published in Page Setting

Optimizing the viewer preferences and zoom factors is crucial for improving the viewing experience of PDF documents. By using the appropriate viewer preferences and zoom factors, you can make your PDF documents more user-friendly, viewable and suitable for different devices and platforms. In this article, we will demonstrate how to set viewer preferences and zoom factors for PDF documents in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Set Viewer Preferences for PDF in C# and VB.NET

Viewer preferences are settings that can be applied to a PDF document to control how it is displayed when it is opened in a PDF viewer. These preferences can affect various aspects of the viewing experience, such as the initial view, page layout, and navigation tabs.

To set the viewer preference for a PDF document using Spire.PDF for .NET, you can follow these steps:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the PdfViewerPreferences object.
  • Set viewer preference for the document using the properties provided by the PdfViewerPreferences class.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace SetViewerPreference
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of PdfDocument class
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile(@"Example.pdf");

            //Get the PdfViewerPreferences object
            PdfViewerPreferences viewerPreferences = pdf.ViewerPreferences;

            //Set viewer preference           
            viewerPreferences.FitWindow = false;
            viewerPreferences.HideMenubar = true;
            viewerPreferences.HideToolbar = true;
            viewerPreferences.CenterWindow= true;
            viewerPreferences.DisplayTitle = false;
            viewerPreferences.PageLayout = PdfPageLayout.SinglePage;
            viewerPreferences.PageMode = PdfPageMode.UseNone;

            //Save the result document
            pdf.SaveToFile("SetViewerPreference.pdf");
            pdf.Close();
        }
    }
}

C#/VB.NET: Set Viewer Preferences and Zoom Factors for PDFs

Set Zoom Factors for PDF in C# and VB.NET

The zoom factor determines the zoom level of the PDF document when it is opened. By default, most PDF viewers set the zoom factor to "Fit Page," which scales the document to fit the width of the viewer window. However, you can also set a specific zoom factor, such as 60%, 150% or 200%, depending on your needs.

To set the zoom factor for a PDF document using Spire.PDF for .NET, you can follow these steps:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get a specific page using PdfDocument.Pages[int index] property.
  • Initialize an instance of the PdfDestination class.
  • Set the destination mode, location and zoom factor using PdfDestination.Mode and PdfDestination.Location and PdfDestination.Zoom properties.
  • Initialize an instance of the PdfGoToAction class and pass the PdfDestination instance to the constructor of the class as a parameter.
  • Set the action to be executed when the document is opened using PdfDocument.AfterOpenAction property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
using System.Drawing;

namespace SetZoomFactor
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the PdfDocument class
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile(@"Example.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Initialize an instance of the PdfDestination class
            PdfDestination dest = new PdfDestination(page);
            //Set the destination mode
            dest.Mode = PdfDestinationMode.Location;
            //Set the destination location
            dest.Location = new PointF(40f, 40f);
            //Set the zoom factor
            dest.Zoom = 1.5f;

            //Initialize an instance of the PdfGoToAction class
            PdfGoToAction gotoAction = new PdfGoToAction(dest);
            //Set the action to be executed when the document is opened
            pdf.AfterOpenAction = gotoAction;

            //Save the result document
            pdf.SaveToFile("SetZoomFactor.pdf");
            pdf.Close();
        }
    }
}

C#/VB.NET: Set Viewer Preferences and Zoom Factors for PDFs

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Page Setting
Monday, 13 February 2012 08:03

Paginate PDF File in C#/VB.NET

PDF pagination not only enables users to add page numbers in PDF file but also can divide PDF document into sections so that some additional information such as PDF file cover, introduction or reference can be added. So PDF pagination provides great convenience for managing large files or organizing books. This section will show you a solution to paginate PDF pages via a .NET PDF component in C#, VB.NET.

Spire.PDF for .NET, a PDF component for managing PDF files, enables you to paginate PDF pages quickly with C#, VB.NET. Before expounding the main code, I want to show the screenshot of the output PDF file as below:

Set PDF Pagination

Here you can download Spire.PDF for .NET and install it on your system. After adding Spire.Pdf dll, let us start to paginate PDF file together.

In the whole solution, there are three main steps to paginate PDF file. One is to draw page number; another is to draw content; the last one is to draw PDF cover so that you can add additional information. Let us see them one by one.

Paginate PDF - Draw PDF Page Number

When drawing page number, page label is also set in below method. By calling this method: DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y, PdfStringFormat format). You can not only set page label font, color, position and format. Please see detail code below:

[C#]
            foreach (PdfPageBase page in section.Pages)
            {
                page.Canvas.SetTransparency(0.5f);
                PdfBrush brush = PdfBrushes.Black;
                PdfPen pen = new PdfPen(brush, 0.75f);
                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true);
                PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
                format.MeasureTrailingSpaces = true;
                float space = font.Height * 0.75f;
                float x = margin.Left;
                float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
                float y = page.Canvas.ClientSize.Height - margin.Bottom + space;
                page.Canvas.DrawLine(pen, x, y, x + width, y);
                y = y + 1;
                String numberLabel
                    = String.Format("{0} of {1}", startNumber++, pageCount);
                page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format);
                page.Canvas.SetTransparency(1);
            }
[VB.NET]
         For Each page As PdfPageBase In section.Pages
            page.Canvas.SetTransparency(0.5!)
            Dim brush As PdfBrush = PdfBrushes.Black
            Dim pen As PdfPen = New PdfPen(brush, 0.75!)
            Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial", 9!, FontStyle.Italic), true)
            Dim format As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Right)
            format.MeasureTrailingSpaces = true
            Dim space As Single = (font.Height * 0.75!)
            Dim x As Single = margin.Left
            Dim width As Single = (page.Canvas.ClientSize.Width  _
                        - (margin.Left - margin.Right))
            Dim y As Single = ((page.Canvas.ClientSize.Height - margin.Bottom)  _
                        + space)
            page.Canvas.DrawLine(pen, x, y, (x + width), y)
            y = (y + 1)
            Dim numberLabel As String = String.Format("{0} of {1}", startNumber++, pageCount)
            page.Canvas.DrawString(numberLabel, font, brush, (x + width), y, format)
            page.Canvas.SetTransparency(1)
        Next

Paginate PDF - Draw PDF Content

PDF content depends on your own like, so here I avoid coding them in detail.

Paginate PDF - Draw PDF Cover

Cover page is always the best place for adding additional information about this PDF file. In below method, I ignore adding reference content and document title, you can add cover image a class PdfImage in the PDF cover page. Please see code below:

[C#]
            //cover
            PdfBrush brush3 = PdfBrushes.Black;
            PdfBrush brush4 = new PdfSolidBrush(new PdfRGBColor(0xf9, 0xf9, 0xf9));
            PdfImage image
                = PdfImage.FromFile(@"..\potala palace1.jpg");
            String text = paginate_PDF.Properties.Resources.ImageDescription;
            float r = image.PhysicalDimension.Height / image.Height;
            PdfPen pen = new PdfPen(brush1, r);
            SizeF size = font1.MeasureString(text, image.PhysicalDimension.Width - 2);
            PdfTemplate template
                = new PdfTemplate(image.PhysicalDimension.Width + 4 * r + 4,
                    image.PhysicalDimension.Height + 4 * r + 7 + size.Height);
            template.Graphics.DrawRectangle(pen, brush4, 0, 0, template.Width, template.Height);
[VB.NET]
        'cover
        Dim brush3 As PdfBrush = PdfBrushes.Black
        Dim brush4 As PdfBrush = New PdfSolidBrush(New PdfRGBColor(249, 249, 249))
        Dim image As PdfImage = PdfImage.FromFile("..\potala palace1.jpg")
        Dim text As String = paginate_PDF.Properties.Resources.ImageDescription
        Dim r As Single = (image.PhysicalDimension.Height / image.Height)
        Dim pen As PdfPen = New PdfPen(brush1, r)
        Dim size As SizeF = font1.MeasureString(text, (image.PhysicalDimension.Width - 2))
        Dim template As PdfTemplate = New PdfTemplate((image.PhysicalDimension.Width  _
                        + ((4 * r)  _
                        + 4)), (image.PhysicalDimension.Height  _
                        + ((4 * r) + (7 + size.Height))))
        template.Graphics.DrawRectangle(pen, brush4, 0, 0, template.Width, template.Height)

Spire.PDF for .NET is a professional PDF component to generate, edit, read, and manipulate PDF documents in your .NET applications with C#, VB.NET.

Published in Page Setting

In a PDF document, the background refers to the overall visual appearance behind the content of the pages. The background can be a simple solid color or an image of your choice. Adding backgrounds to PDFs can help you add visual interest to your documents and also enhance readability. In this article, you will learn how to programmatically set the background color or image for PDF using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF 

Add Background Color to PDF Documents in C# and VB.NET

The PdfPageBase.BackgroundColor property offered by Spire.PDF for .NET allows you to set a solid color as the PDF background. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Loop through all the PDF pages and add a background color to each page using PdfPageBase.BackgroundColor property.
  • Set the opacity of the background using PdfPageBase.BackgroudOpacity property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing;

namespace PDFBackgroundColor
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF file from disk
            pdf.LoadFromFile("input.pdf");

            //Loop through the pages in the PDF file
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Set the background color for each page
                page.BackgroundColor = Color.Yellow;

                //Set the opacity of the background
                page.BackgroudOpacity = 0.1f;
            }

            //Save the result PDF file
            pdf.SaveToFile("BackgroundColor.pdf");
            pdf.Close();

        }
    }
}

C#/VB.NET: Add Background Color or Background Image to PDF

Add Background Images to PDF Documents C# and VB.NET

If you want to add an image as a background to match the document theme, you can use the PdfPageBase.BackgroundImage property. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Loop through all the PDF pages and add a background picture to each page using PdfPageBase.BackgroundImage property.
  • Set the opacity of the background using PdfPageBase.BackgroudOpacity property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing;

namespace PDFBackgroundImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF file from disk
            pdf.LoadFromFile("input.pdf");

            //Load an image
            Image background = Image.FromFile("background.png");

            //Loop through the pages in the PDF file
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Set the loaded image as the background image for each page
                page.BackgroundImage = background;

                //Set the opacity of the background
                page.BackgroudOpacity = 0.2f;
            }

            //Save the result PDF file
            pdf.SaveToFile("BackgroundImage.pdf");
            pdf.Close();

        }
    }
}

C#/VB.NET: Add Background Color or Background Image to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Page Setting
Page 3 of 3