How to create multicolumn PDF in C# and VB.NET
The Syncfusion Essential® PDF is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.
Using this library, you can create a multicolumn PDF in C# and VB.NET.
Steps to create a multicolumn PDF programmatically in C#:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespace in Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using System; using System.Drawing;
VB.NET
Imports System.Drawing Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics
- You can draw the text on a PDF document, in either single column or multicolumn using Draw method of PdfTextElement.
- Draw text on a PDF document in single column.
C#
// Draw the text in single column textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height));
VB.NET
'Draw the text in single column textElement.Draw(page, New RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height))
- Simple logic to draw text on a PDF document in two columns is to divide the width.
C#
// Draw the text in first column textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height)); // Draw the text in second column textElement.Draw(page, new RectangleF(page.GetClientSize().Width / 2, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height));
VB.NET
'Draw the text in first column textElement.Draw(page, New RectangleF(0, 0, (page.GetClientSize().Width / 2), page.GetClientSize().Height)) 'Draw the text in second column textElement.Draw(page, New RectangleF((page.GetClientSize().Width / 2), 0, (page.GetClientSize().Width / 2), page.GetClientSize().Height))
- Logic to draw the text on a PDF document in multicolumn is,
C#
// Get number of columns Console.WriteLine("Enter number of columns : "); int n = Convert.ToInt16(Console.ReadLine()); for (int i = 0; i < n; i++) { // Draw the text textElement.Draw(page, new RectangleF(i * page.GetClientSize().Width / n, 0, page.GetClientSize().Width / n, page.GetClientSize().Height)); }
VB.NET
'Get number of columns Console.WriteLine("Enter number of columns : ") Dim n As Integer = Convert.ToInt16(Console.ReadLine) Dim i As Integer For i = 0 To n - 1 Step 1 'Draw the text textElement.Draw(page, New RectangleF((i * (page.GetClientSize().Width / n)), 0, (page.GetClientSize().Width / n), page.GetClientSize().Height)) Next
- Include the following code snippet in main method of Program.CS file, to create a multicolumn PDF document.
C#
// Create a new PDF document PdfDocument document = new PdfDocument(); // Add a page to the document PdfPage page = document.Pages.Add(); // Create PDF Graphics for the page PdfGraphics graphics = page.Graphics; // Set the standard font PdfFont standardFont = new PdfStandardFont(PdfFontFamily.Helvetica, 12); // Load the text into PdfTextElement PdfTextElement textElement = new PdfTextElement("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.", standardFont); // Get number of columns Console.WriteLine("Enter number of columns : "); int n = Convert.ToInt16(Console.ReadLine()); for (int i = 0; i < n; i++) { // Draw the text textElement.Draw(page, new RectangleF(i * page.GetClientSize().Width / n, 0, page.GetClientSize().Width / n, page.GetClientSize().Height)); } // Save the PDF document document.Save("MultiColumnPDF.pdf"); // Close the instance of PdfDocument document.Close(true);
VB.NET
'Create a new PDF document Dim document As PdfDocument = New PdfDocument 'Add a page to the document Dim page As PdfPage = document.Pages.Add 'Create PDF Graphics for the page Dim graphics As PdfGraphics = page.Graphics 'Set the standard font Dim standardFont As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12) 'Load the text into PdfTextElement Dim textElement As PdfTextElement = New PdfTextElement(("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base."))), standardFont) 'Get number of columns Console.WriteLine("Enter number of columns : ") Dim n As Integer = Convert.ToInt16(Console.ReadLine) Dim i As Integer For i = 0 To n - 1 Step 1 'Draw the text textElement.Draw(page, New RectangleF((i * (page.GetClientSize().Width / n)), 0, (page.GetClientSize().Width / n), page.GetClientSize().Height)) Next 'Save the PDF document document.Save("MultiColumnPDF.pdf") 'Close the instance of PdfDocument document.Close(True)
A complete work sample to create a multicolumn PDF document can be downloaded from MultiColumnPDF.zip.
By executing the program, you will get a console window asking for number of columns.
On entering the number of columns as 5, you will get the PDF document with text in 5 columns as follows.
Take a moment to peruse the documentation, where you will find other options like drawing right-to-left text and multicolumn text, consuming TrueType fonts, Standards fonts, CJK fonts. Also, the features like PDF form filling, extract text or images from PDF, protect PDF documents with code examples.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to create multicolumn PDF in C# and VB.NET.
You can refer to our WinForms PDF’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms PDF documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms PDF and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!