Create PDF Booklet in C#/VB.NET

2013-01-24 03:08:46 Written by Koohji

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

Set Table Layout in PDF with C#/VB.NET

2013-01-23 08:34:33 Written by Koohji

Table Layout decides how the table displays in PDF page. People always set PDF table layout in order to let the table perfectly fit PDF page according to their own like. In this section, I will introduce a solution to set table layout in PDF via this .NET PDF component Spire.PDF for .NET with C#, VB.NET. First let us view the target PDF file as below picture:

Set PDF Table Layout

In Spire.PDF for .NET, there is a class called: Spire.Pdf.Tables.PdfTableLayoutFormat. By using this class, we can set table layout type and break type. Here Spire.PDF provided two layout types: Paginate and OnePage and two break type: FitElement and Fit Page. When you set the break type to be FitElement, PDF table will display according to the table length, while for FitPage choice, the table will automatically fit the PDF to every page ignoring table length.

Here let us see this method: PdfLayoutResult.Draw(PdfPageBase page, PointF location, PdfTextLayout format).There are three parameters passed. The first parameter determines the page size and margin. By setting the second parameter, we can set the horizontal and vertical distance between the table and PDF margin. While the last parameter is the table layout we just set. Obviously, we can set table layout by calling this method directly.

Now, you can download Spire.PDF for .NET and start table layout task by below key code:

[C#]
table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);
            PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
            tableLayout.Break = PdfLayoutBreakType.FitElement;
            tableLayout.Layout = PdfLayoutType.Paginate;
            PdfLayoutResult result = table.Draw(page, new PointF(0, y), tableLayout);
            y = result.Bounds.Bottom + 5;
[VB.NET]
table.BeginRowLayout += New BeginRowLayoutEventHandler(table_BeginRowLayout)
Dim tableLayout As New PdfTableLayoutFormat()
tableLayout.Break = PdfLayoutBreakType.FitElement
tableLayout.Layout = PdfLayoutType.Paginate
Dim result As PdfLayoutResult = table.Draw(page, New PointF(0, y), tableLayout)
y = result.Bounds.Bottom + 5

Spire.PDF for .NET is a PDF api that enables users to create, edit, read and handle PDF files in .NET applications.

A simple PDF List is always one level list which displays briefly the content items of a PDF file. When the PDF is very huge and complex, people need more details, creating multiple level list in PDF becomes pretty necessary. Here a solution will be introduced to you about how to create multiple level list in PDF via a .NET PDF component Spire.PDF for .NET in C#, VB.NET. First let us see the effect of PDF multiple level list as below picture:

Create Multiple Level List

Here we can download Spire.PDF for .NET. After adding Spire.Pdf dll in project, please view below key code to start PDF list task.

In this solution, apart from creating PDF list, we also can set list format, position and layout. This class Graphics.PdfTrueTypeFont which is provided by Spire.PDF enables to set list font and size. From above picture, we can view that the first level list is red. It can be set in this class Spire.Pdf.Graphics.PdfLinearGradientBrush. In order to let users themselves to set PDF list style according to their needs, Spire.Pdf.Lists.PdfOrderedMarker can help us to set label style by this enum Spire.Pdf.PdfNumberStyle. Here there are six styles available: LowerLatin, LowerRoman, None, Numeric, UpperLatin and UpperRoman. In below code, I only show a part of code:

[C#]
            //Set PDF list label style
            PdfOrderedMarker marker1
                = new PdfOrderedMarker(PdfNumberStyle.LowerRoman, new PdfFont(PdfFontFamily.Helvetica, 12f));
            PdfOrderedMarker marker2
                = new PdfOrderedMarker(PdfNumberStyle.Numeric, new PdfFont(PdfFontFamily.Helvetica, 10f));
[VB.NET]
'Set PDF list label style
Dim marker1 As New PdfOrderedMarker(PdfNumberStyle.LowerRoman, New PdfFont(PdfFontFamily.Helvetica, 12F))
Dim marker2 As New PdfOrderedMarker(PdfNumberStyle.Numeric, New PdfFont(PdfFontFamily.Helvetica, 10F))

After setting the PDF list format, we can search data in database by System.Data.Oledb.OleDbCommand and then, create multi-level list by two classes in Spire.Pdf: Spire.Pdf.Lists. PdfListItem and Spire.Pdf.Lists. PdfSortedList. The former class represents list item while the later represents the ordered list.

[C#]
           //Create multi-level list in PDF
            PdfSortedList vendorList = new PdfSortedList(font2);
            vendorList.Indent = 0;
            vendorList.TextIndent = 5;
            vendorList.Brush = brush2;
            vendorList.Marker = marker1;
            using (OleDbConnection conn = new OleDbConnection())
            {
                conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=demo.mdb";
                OleDbCommand command = new OleDbCommand();
                command.CommandText
                    = " select VendorNo, VendorName from vendors ";
                command.Connection = conn;
                OleDbCommand command2 = new OleDbCommand();
                command2.CommandText
                    = " select Description from parts where VendorNo = @VendorNo";
                command2.Connection = conn;
                OleDbParameter param = new OleDbParameter("@VendorNo", OleDbType.Double);
                command2.Parameters.Add(param);
                conn.Open();
                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    double id = reader.GetDouble(0);
                    PdfListItem item = vendorList.Items.Add(reader.GetString(1));
                    PdfSortedList subList = new PdfSortedList(font3);
                    subList.Marker = marker2;
                    subList.Brush = brush3;
                    item.SubList = subList;
                    subList.TextIndent = 20;
                    command2.Parameters[0].Value = id;
                    using (OleDbDataReader reader2 = command2.ExecuteReader())
                    {
                        while (reader2.Read())
                        {
                            subList.Items.Add(reader2.GetString(0));
                        }
                    }
                    String maxNumberLabel = Convert.ToString(subList.Items.Count);
                    subList.Indent = 30 - font3.MeasureString(maxNumberLabel).Width;
                }
            }
[VB.NET]
'Create multi-level list in PDF
Dim vendorList As New PdfSortedList(font2)
vendorList.Indent = 0
vendorList.TextIndent = 5
vendorList.Brush = brush2
vendorList.Marker = marker1
Using conn As New OleDbConnection()
	conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=demo.mdb"
	Dim command As New OleDbCommand()
	command.CommandText = " select VendorNo, VendorName from vendors "
	command.Connection = conn
	Dim command2 As New OleDbCommand()
	command2.CommandText = " select Description from parts where VendorNo = @VendorNo"
	command2.Connection = conn
	Dim param As New OleDbParameter("@VendorNo", OleDbType.[Double])
	command2.Parameters.Add(param)
	conn.Open()
	Dim reader As OleDbDataReader = command.ExecuteReader()
	While reader.Read()
		Dim id As Double = reader.GetDouble(0)
		Dim item As PdfListItem = vendorList.Items.Add(reader.GetString(1))
		Dim subList As New PdfSortedList(font3)
		subList.Marker = marker2
		subList.Brush = brush3
		item.SubList = subList
		subList.TextIndent = 20
		command2.Parameters(0).Value = id
		Using reader2 As OleDbDataReader = command2.ExecuteReader()
			While reader2.Read()
				subList.Items.Add(reader2.GetString(0))
			End While
		End Using
		Dim maxNumberLabel As [String] = Convert.ToString(subList.Items.Count)
		subList.Indent = 30 - font3.MeasureString(maxNumberLabel).Width
	End While
End Using

Finally, if we want to make sure that PDF multi-level list fits the page perfectly, we need to set the PDF text layout. When doing this you can call this method: Spire.Pdf.Graphics.PdfLayoutResult.Draw(PdfPageBase page, PointF location, PdfTextLayout format); there are three parameters passed, one is to draw text in PDF page, another is to set text location, and the last one is to set text layout.

[C#]
            //Set PDF Layout and position
            PdfTextLayout textLayout = new PdfTextLayout();
            textLayout.Break = PdfLayoutBreakType.FitPage;
            textLayout.Layout = PdfLayoutType.Paginate;
            vendorList.Draw(page, new PointF(0, y), textLayout);
[VB.NET]
'Set PDF Layout and position
Dim textLayout As New PdfTextLayout()
textLayout.Break = PdfLayoutBreakType.FitPage
textLayout.Layout = PdfLayoutType.Paginate
vendorList.Draw(page, New PointF(0, y), textLayout)

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

page 282