How to draw online images in PDF tables 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 draw online images in PDF tables using C# and VB.NET.
Steps to draw online images in PDF tables programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.Designer.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Grid; using Syncfusion.Pdf.Graphics; using System.Drawing; using System.Net; using System.IO;
VB.NET
Imports System.Drawing Imports System.IO Imports System.Net Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Grid
- Use the following code snippet to draw online image in PDF table.
C#
//Create a PDF document PdfDocument doc = new PdfDocument(); //Add a page PdfPage page = doc.Pages.Add(); //Create a PdfGrid PdfGrid pdfGrid = new PdfGrid(); //Apply built-in table style pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4); DataTable dataTable = new DataTable(); dataTable.Columns.Add("Student"); dataTable.Columns.Add("Picked Up By"); dataTable.Columns.Add("Signed"); dataTable.Rows.Add(new object[] { "Aiden Leung", "Sharon Chen:Sharon Chen", "http://www.syncfusion.com/downloads/support/directtrac/general/Signature-2080342779" }); dataTable.Rows.Add(new object[] { "Daniel Hackett", "Timothy Hackett", "http://www.syncfusion.com/downloads/support/directtrac/general/Signature-2080342779" }); //Assign data source pdfGrid.DataSource = dataTable; pdfGrid.BeginCellLayout += new PdfGridBeginCellLayoutEventHandler(table_BeginCellLayout); //Draw grid to the page of PDF document pdfGrid.Draw(page, new PointF(10, 10)); //Save and close the document doc.Save("output.pdf"); doc.Close(true);
VB.NET
'Create a PDF document Dim doc As PdfDocument = New PdfDocument() 'Add a page Dim page As PdfPage = doc.Pages.Add() 'Create a PdfGrid Dim pdfGrid As PdfGrid = New PdfGrid() 'Apply built-in table style pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4) Dim dataTable As DataTable = New DataTable() dataTable.Columns.Add("Student") dataTable.Columns.Add("Picked Up By") dataTable.Columns.Add("Signed") dataTable.Rows.Add(New Object() {"Aiden Leung", "Sharon Chen:Sharon Chen", "http://www.syncfusion.com/downloads/support/directtrac/general/Signature-2080342779"}) dataTable.Rows.Add(New Object() {" Daniel Hackett", "Timothy Hackett", "http://www.syncfusion.com/downloads/support/directtrac/general/Signature-2080342779"}) 'Assign data source pdfGrid.DataSource = dataTable AddHandler pdfGrid.BeginCellLayout, New PdfGridBeginCellLayoutEventHandler(AddressOf table_BeginCellLayout) 'Draw grid to the page of PDF document pdfGrid.Draw(page, New PointF(10, 10)) 'Save and close the document doc.Save("output.pdf") doc.Close(True)
C#
//Event to read stream from online image public void table_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { if (args.RowIndex != -1 && args.CellIndex == 2) { //Check the cell value start with https if (args.Value.StartsWith("https:\") || args.Value.StartsWith("http:\")) { WebClient wc = new WebClient(); byte[] bytes = wc.DownloadData(args.Value); MemoryStream ms = new MemoryStream(bytes); PdfBitmap pBmp = new PdfBitmap(ms); PdfGrid grid = sender as PdfGrid; //Set the cell value as empty string grid.Rows[args.RowIndex].Cells[args.CellIndex].Value = ""; //Set the image as a background image args.Style.BackgroundImage = pBmp; } } }
VB.NET
'Event to read stream from online image Public Sub table_BeginCellLayout(ByVal sender As Object, ByVal args As PdfGridBeginCellLayoutEventArgs) If ((args.RowIndex <> -1) _ AndAlso (args.CellIndex = 2)) Then 'Check the cell value start with https If args.Value.StartsWith("https:\") Or args.Value.StartsWith("http:\") Then Dim wc As WebClient = New WebClient() Dim bytes() As Byte = wc.DownloadData(args.Value) Dim ms As MemoryStream = New MemoryStream(bytes) Dim pBmp As PdfBitmap = New PdfBitmap(ms) Dim grid As PdfGrid = CType(sender, PdfGrid) 'Set the cell value as empty string grid.Rows(args.RowIndex).Cells(args.CellIndex).Value = "" 'Set the image as a background image args.Style.BackgroundImage = pBmp End If End If End Sub
A complete working sample can be downloaded from DrawOnlineImagesInPDFTables.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with images, where you will find other options like inserting, replacing image in PDF document, image pagination, and image masking.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link to inserting images into PDF document.
An online sample link to replacing images in PDF document.
An online sample link to image extraction from PDF document.
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 draw online images in PDF tables using C# and VB.NET.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
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!