How to export Chart and Grid to one PDF page using C# and VB.NET
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can export Chart and Grid to one PDF page using C# and VB.NET.
Steps to export Chart and Grid to one PDF page programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.GridHelperClasses.Windows NuGet package as reference to your .NET Framework application from NuGet.org.
- Open your form in designer. Add the Syncfusion controls to your VS.NET toolbox, if you have not done it already (the install will automatically do this unless you select not to complete toolbox integration during installation).
- Drag and drop the ChartControl and GridControl into the form.
- Refer to the documentation for Chart and Grid controls, where you will find the options for populating data and other customizations of Chart and Grid.
- Include the following namespaces in the Form1.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Graphics; using Syncfusion.GridHelperClasses; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf.Graphics Imports Syncfusion.GridHelperClasses Imports System.Drawing
- Use the following code snippet in the click event of button to find the position and dimension of PDF form fields.
C#
//Create a new instance of PDF document PdfDocument document = new PdfDocument(); document.PageSettings.Margins.All = 0; //Create PDF converter GridPDFConverter pdfConverter = new GridPDFConverter(); //Export the content of Grid to the PDF file pdfConverter.ExportToPdf(document, this.gridControl1); MemoryStream ms = new MemoryStream(); //Save PDF into memory stream document.Save(ms); //Load PDF document PdfLoadedDocument ldoc = new PdfLoadedDocument(ms); //Create template from loaded PDF document PdfTemplate template = (ldoc.Pages[0] as PdfLoadedPage).CreateTemplate(); //Save chart as image chartControl1.SaveImage("Chart.jpg"); //Create PDF image from saved image PdfImage image = new PdfBitmap("Chart.jpg"); //Initialize an object for the PDF document document = new PdfDocument(); //Create a page PdfPage page = document.Pages.Add(); //Draw chart as image page.Graphics.DrawImage(image, new RectangleF(0, 20, 300, 250)); //Draw grid as template page.Graphics.DrawPdfTemplate(template, new PointF(30, 280)); //Save and close PDF document document.Save("ChartAndGridInSinglePage.pdf"); document.Close(true);
VB.NET
'Create a new instance of PDF document Dim document As New PdfDocument() document.PageSettings.Margins.All = 0 'Create PDF converter Dim pdfConverter As New GridPDFConverter() 'Export the content of Grid to the PDF file. pdfConverter.ExportToPdf(document, Me.gridControl1) Dim ms As New MemoryStream() 'Save PDF into memory stream document.Save(ms) 'Load PDF document Dim ldoc As New PdfLoadedDocument(ms) 'Create template from loaded PDF document Dim template As PdfTemplate = TryCast(ldoc.Pages(0), PdfLoadedPage).CreateTemplate() 'Save chart as image chartControl1.SaveImage("Chart.jpg") 'Create PDF image from saved image Dim image As PdfImage = New PdfBitmap("Chart.jpg") 'Initialize an object for the PDF document document = New PdfDocument() 'Create a page Dim page As PdfPage = document.Pages.Add() 'Draw chart as image page.Graphics.DrawImage(image, New RectangleF(0, 20, 300, 250)) 'Draw grid as template page.Graphics.DrawPdfTemplate(template, New PointF(30, 280)) 'Save and close the PDF document document.Save("ChartAndGridInSinglePage.pdf") document.Close(True)
A complete working sample can be downloaded from ExportChartAndGridToOnePDFPage.zip.
By executing the program, you will get the PDF document as follows.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to export Chart and Grid to one PDF page using C# and VB.NET.
You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our 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 other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!