Hi saranjoe,
Check this example. Now please take its reference and correct your code.
Database
I have used the table YoutubeVideos with the schema as follow.
CREATE TABLE [dbo].[YoutubeVideos](
[VideoId] [int] NOT NULL,
[VideoUrl] [nvarchar](1000) NOT NULL,
CONSTRAINT [PK_YoutubeVideos] PRIMARY KEY CLUSTERED
(
[VideoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO YoutubeVideos VALUES(1,'https://www.youtube.com/watch?v=uL4eoxZ4Y8E')
INSERT INTO YoutubeVideos VALUES(2,'https://www.youtube.com/watch?v=PmvmtC8lDA4')
INSERT INTO YoutubeVideos VALUES(3,'https://www.youtube.com/watch?v=CF3FjliwbYk')
INSERT INTO YoutubeVideos VALUES(4,'https://www.youtube.com/watch?v=qKsOGdiyPOs')
INSERT INTO YoutubeVideos VALUES(5,'https://www.youtube.com/watch?v=YnhKi66Q2tk')
INSERT INTO YoutubeVideos VALUES(6,'https://www.youtube.com/watch?v=3LghkqetlBY')
HTML
C#
<asp:DataList ID="dlVideos" runat="server" AutoGenerateColumns="False" CssClass="table" RepeatColumns="3">
<ItemTemplate>
<asp:HiddenField ID="hfUrl" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.VideoUrl").ToString().Split(new string[] {"v="}, StringSplitOptions.None)[1] %>' />
<iframe id="video" width="200" height="200" frameborder="0" allowfullscreen
src='<%# "https://www.youtube.com/embed/" + DataBinder.Eval(Container, "DataItem.VideoUrl").ToString().Split(new string[] {"v="}, StringSplitOptions.None)[1] %>'></iframe>
</ItemTemplate>
</asp:DataList>
<br />
<asp:Button Text="Export" runat="server" OnClick="ExportToPDF" />
VB.Net
<asp:DataList ID="dlVideos" runat="server" AutoGenerateColumns="False" CssClass="table" RepeatColumns="3">
<ItemTemplate>
<asp:HiddenField ID="hfUrl" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.VideoUrl").ToString().Split(New String() {"v="}, StringSplitOptions.None)(1) %>' />
<iframe id="video" width="200" height="200" frameborder="0" allowfullscreen
src='<%# "https://www.youtube.com/embed/" & DataBinder.Eval(Container, "DataItem.VideoUrl").ToString().Split(New String() {"v="}, StringSplitOptions.None)(1) %>'></iframe>
</ItemTemplate>
</asp:DataList>
<br />
<asp:Button Text="Export" runat="server" OnClick="ExportToPDF" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Net;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Imports System.Net
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT VideoId, VideoUrl FROM YoutubeVideos", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
this.dlVideos.DataSource = dt;
this.dlVideos.DataBind();
}
}
}
}
}
protected void ExportToPDF(object sender, EventArgs e)
{
PdfPTable pdfTable = new PdfPTable(this.dlVideos.RepeatColumns);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 70;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.DefaultCell.BorderWidth = 2;
foreach (DataListItem item in this.dlVideos.Items)
{
string id = (item.FindControl("hfUrl") as HiddenField).Value;
byte[] imageBytes = new WebClient().DownloadData(this.GetYoutubeVideoThumbnailUrl(id));
PdfPCell cell = ImageCell(imageBytes, 30f, PdfPCell.ALIGN_CENTER);
pdfTable.AddCell(cell);
}
using (MemoryStream memoryStream = new MemoryStream())
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Video.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
}
}
private PdfPCell ImageCell(byte[] bytes, float scale, int align)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(bytes);
image.ScalePercent(scale);
PdfPCell cell = new PdfPCell(image);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 10f;
cell.PaddingTop = 10f;
return cell;
}
public string GetYoutubeVideoThumbnailUrl(string videoId)
{
return string.Format("https://img.youtube.com/vi/{0}/mqdefault.jpg", videoId);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT VideoId, VideoUrl FROM YoutubeVideos", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.dlVideos.DataSource = dt
Me.dlVideos.DataBind()
End Using
End Using
End Using
End If
End Sub
Protected Sub ExportToPDF(ByVal sender As Object, ByVal e As EventArgs)
Dim pdfTable As PdfPTable = New PdfPTable(Me.dlVideos.RepeatColumns)
pdfTable.DefaultCell.Padding = 3
pdfTable.WidthPercentage = 70
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.DefaultCell.BorderWidth = 2
For Each item As DataListItem In Me.dlVideos.Items
Dim id As String = TryCast(item.FindControl("hfUrl"), HiddenField).Value
Dim imageBytes As Byte() = New WebClient().DownloadData(Me.GetYoutubeVideoThumbnailUrl(id))
Dim cell As PdfPCell = ImageCell(imageBytes, 30.0F, PdfPCell.ALIGN_CENTER)
pdfTable.AddCell(cell)
Next
Using memoryStream As MemoryStream = New MemoryStream()
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0F)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
pdfDoc.Open()
pdfDoc.Add(pdfTable)
pdfDoc.Close()
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Video.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.End()
End Using
End Sub
Private Function ImageCell(ByVal bytes As Byte(), ByVal scale As Single, ByVal align As Integer) As PdfPCell
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bytes)
image.ScalePercent(scale)
Dim cell As PdfPCell = New PdfPCell(image)
cell.BorderColor = BaseColor.WHITE
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 10.0F
cell.PaddingTop = 10.0F
Return cell
End Function
Public Function GetYoutubeVideoThumbnailUrl(ByVal videoId As String) As String
Return String.Format("https://img.youtube.com/vi/{0}/mqdefault.jpg", videoId)
End Function
Screenshot