ASP.NET Pdf Insert Image ตัวอย่างการเขียน ASP.NET สร้างเอกสาร PDF ทำการแทรกรูปภาพ (Insert Images) ลงในเอกสาร PDF
Language Code : VB.NET || C#
ASP.NET Add Reference
Framework : 1,2,3,4
Instance NameSpace
VB.NETImports PdfSharp
C#Using PdfSharp;
AspNetPDFInsertImages.aspx
<%@ Import Namespace="PdfSharp"%>
<%@ Import Namespace="PdfSharp.Drawing"%>
<%@ Import Namespace="PdfSharp.Pdf"%>
<%@ Import Namespace="PdfSharp.Pdf.IO"%>
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
' Create a new PDF document
Dim DocPDF As PdfDocument = New PdfDocument
' Create an empty page
Dim objPage As PdfPage = DocPDF.AddPage
' Get an XGraphics object for drawing
Dim gfx As XGraphics = XGraphics.FromPdfPage(objPage)
' Create a font
Dim font1 As XFont = New XFont("Verdana", 20, XFontStyle.Bold)
Dim font2 As XFont = New XFont("Tahoma", 8, XFontStyle.Bold)
' Draw the text
gfx.DrawString(" Welcome To www.ThaiCreate.Com", font1, XBrushes.Black, _
New XRect(0, 50, objPage.Width.Point, objPage.Height.Point), XStringFormats.TopCenter)
Dim strString As String = "STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING"
' Draw the text
gfx.DrawString(strString, font2, XBrushes.Black, 65, 80, XStringFormats.TopLeft)
' Draw the text
gfx.DrawString(strString, font2, XBrushes.Black, 65, 90, XStringFormats.TopLeft)
' Draw the text
gfx.DrawString(strString, font2, XBrushes.Black, 65, 100, XStringFormats.TopLeft)
' Draw the text
gfx.DrawString(strString, font2, XBrushes.Black, 65, 110, XStringFormats.TopLeft)
' Draw the text
gfx.DrawString(strString, font2, XBrushes.Black, 65, 120, XStringFormats.TopLeft)
' Draw the text
gfx.DrawString(strString, font2, XBrushes.Black, 65, 130, XStringFormats.TopLeft)
' Draw the text
gfx.DrawString(strString, font2, XBrushes.Black, 65, 140, XStringFormats.TopLeft)
gfx.DrawImage(XImage.FromFile(Server.MapPath("thaicreate.png")),260 , 160)
Dim i As Integer
For i = 250 To 500 Step 10
gfx.DrawString(strString, font2, XBrushes.Black, 65, i + 10, XStringFormats.TopLeft)
Next
' Save the document...
Dim FileName As String = "MyPDF/PdfDoc.pdf"
DocPDF.Save(Server.MapPath(FileName))
DocPDF.Close()
DocPDF = Nothing
Me.lblText.Text = "PDF Created <a href=" & FileName & ">click here</a> to view"
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Send Mail</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
Screenshot
|