Sử dụng iTextSharp để chèn Image vào file PDF trong Asp.net
(How to add image in PDF file using iTextSharp in ASP.NET ) - Bài viết dưới đây, thủ thuật tin học sẽ giới thiệu với các bạn cách sử dụng thư viện iTextSharp để chèn ảnh vào file PDF.
- B2: Download thư viện iTextSharp tại đây
http://pdfviewernet.googlecode.com/svn-history/r55/trunk/PDFView/PDFView/lib/itextsharp.dll
- B3: References itextsharp.dll trong thư mục vừa giải nén vào Project.
- B4: Download các file asp_net.jpg, Copy vào các thư mục Images của Project
- B5: Mở file Default.aspx dưới dạng HTML và nhập mã HTML
C#
<%@ Page
Title="How to add
image in PDF file using iTextSharp in ASP.NET" Language="C#"
MasterPageFile="~/Site.master"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="AddImageinPDFUsingiTextSharp._Default"
%>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table cellpadding="3"
cellspacing="5"
border="0"
width="50%">
<tr>
<td>
<div
class="panel
panel-default">
<div
class="panel-heading">
<asp:label id="lblHeader" runat="server" Text="Insert Image in PDF file using iTextSharp in
ASP.NET"></asp:label>
</div>
<div
class="modal-footer">
<div class="btn-group">
<asp:LinkButton id="cmdInsert"
runat="server"
CssClass="btn
btn-small" OnClick="cmdInsert_Click" Causesvalidation="true">
<i class="icon-insert"></i> <asp:label id="lblInsert" runat="server" Text="Insert"></asp:label>
</asp:LinkButton>
</div>
</div>
</div>
</td>
</tr>
</table>
</asp:Content>
VB.NET
<%@ Page
Title="How to add
image in PDF file using iTextSharp in ASP.NET" Language="vb"
MasterPageFile="~/Site.Master"
AutoEventWireup="false"
CodeBehind="Default.aspx.vb"
Inherits="AddImageinPDFUsingiTextSharp._Default"
%>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table cellpadding="3"
cellspacing="5"
border="0"
width="50%">
<tr>
<td>
<div
class="panel
panel-default">
<div
class="panel-heading">
<asp:label id="lblHeader" runat="server" Text="Insert Image in PDF file using iTextSharp in
ASP.NET"></asp:label>
</div>
<div
class="modal-footer">
<div class="btn-group">
<asp:LinkButton id="cmdInsert"
runat="server"
CssClass="btn
btn-small" Causesvalidation="true">
<i class="icon-insert"></i> <asp:label id="lblInsert" runat="server" Text="Insert"></asp:label>
</asp:LinkButton>
</div>
</div>
</div>
</td>
</tr>
</table>
</asp:Content>- B6: Viết Code cho file Default.aspx
C# Code
//Visit http://www.laptrinhdotnet.com
for more ASP.NET Tutorials
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Web.UI;
using System.Web;
using iTextSharp.text.html;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
namespace AddImageinPDFUsingiTextSharp
{
public partial class _Default :
System.Web.UI.Page
{
#region
"Insert Image"
private void
InsertImageToPDF(string FileName)
{
Document oDoc = new
Document(PageSize.A4,
10f, 10f, 5f, 0f);
System.IO.MemoryStream msReport = new System.IO.MemoryStream();
string
ImagePath = Server.MapPath("Images\\asp_net.jpg");
try
{
PdfWriter writer = PdfWriter.GetInstance(oDoc, msReport);
oDoc.AddAuthor("Thu thuat lap
trinh");
oDoc.AddSubject("Export to PDF");
oDoc.Open();
Chunk cBreak = new Chunk(Environment.NewLine);
Phrase pBreak = new Phrase();
Paragraph paBreak = new Paragraph();
Paragraph para = new Paragraph("Thu thuat lap trinh Asp.net");
para.IndentationLeft = 140;
oDoc.Add(para);
//Image
float PositionX = 35f;
float PositionY = 780f;
if (File.Exists(ImagePath))
{
Image pdfImage = Image.GetInstance(ImagePath);
pdfImage.ScaleToFit(100, 50);
pdfImage.Alignment = iTextSharp.text.Image.HEADER;
pdfImage.SetAbsolutePosition(PositionX, PositionY);
oDoc.Add(pdfImage);
}
}
catch
{
}
oDoc.Close();
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=" + FileName
+ ".pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(msReport.ToArray());
Response.End();
}
#endregion
#region
"Event Handles"
protected void
cmdInsert_Click(object sender, System.EventArgs e)
{
InsertImageToPDF("InsertImage");
}
#endregion
}
}
'Visit http://www.laptrinhdotnet.com
for more ASP.NET Tutorials
Imports iTextSharp.text.html
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports System.IO
Namespace AddImageinPDFUsingiTextSharp
Public Class _Default
Inherits System.Web.UI.Page
#Region "Insert
Image"
Private Sub
InsertImageToPDF(ByVal FileName As String)
Dim oDoc As New Document(PageSize.A4.Rotate,
20, 20, 30, 20)
Dim msReport As New System.IO.MemoryStream()
Dim ImagePath As String = Server.MapPath("Images\asp_net.jpg")
Try
Dim writer As
PdfWriter = PdfWriter.GetInstance(oDoc,
msReport)
oDoc.AddAuthor("Thu thuat lap
trinh")
oDoc.AddSubject("Export to PDF")
oDoc.Open()
Dim cBreak As
New Chunk(Environment.NewLine)
Dim pBreak As
New Phrase()
Dim paBreak As
New Paragraph()
Dim para As
New Paragraph("Thu thuat lap trinh Asp.net")
para.IndentationLeft = 140
oDoc.Add(para)
'Image
Dim PositionX As
Integer = 35
Dim PositionY As
Integer = 530
If File.Exists(ImagePath)
Then
Dim pdfImage As
Image = Image.GetInstance(ImagePath)
pdfImage.ScaleToFit(100, 50)
pdfImage.Alignment = iTextSharp.text.Image.HEADER
pdfImage.SetAbsolutePosition(PositionX, PositionY)
oDoc.Add(pdfImage)
End If
Catch e As Exception
Console.Error.WriteLine(e.Message)
End Try
oDoc.Close()
Response.Clear()
Response.AddHeader("content-disposition",
"attachment;filename=" &
FileName & ".pdf")
Response.ContentType = "application/pdf"
Response.BinaryWrite(msReport.ToArray())
Response.End()
End Sub
#End Region
#Region "Event
Handles"
Private Sub cmdInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles cmdInsert.Click
InsertImageToPDF("InsertImage")
End Sub
#End Region
End Class
End Namespace
Chúc các bạn thành công!
Quang Bình
No Comment to " Sử dụng iTextSharp để chèn Image vào file PDF trong Asp.net "