Merhaba arkadaşlar bu makalemizde Sql veritabanımızda ki verileri pdf dosyaya kaydedeceğiz. Bunun için ilk olarak Solution Explorer kısmında projemize sağ tıklayalım. Add kısmından Reference kısmına tıklayalım. Açılan Reference Manager penceresinde Browse kısmında PdfSharp.dll dosyasını seçip Ok butonuna basıp projemize ekleyelim.
Şekil 1
Şekil 2
Şekil 3
WebForm1.aspx.vb
Imports System.Data.SqlClient
Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf
Imports System
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
Dim conString As String
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim i As Integer
Dim sql As String
Dim point, point2 As Integer
Dim name As String
Dim sname As String
Dim contact As String
Dim title As String
'local sql veritabanimiza baglaniyoruz.
conString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\person.mdf;Integrated Security=True"
sql = "Select Name,Surname,Contact From [dbo].[person]"
con = New SqlConnection(conString)
con.Open()
cmd = New SqlCommand(sql, con)
da.SelectCommand = cmd
da.Fill(ds)
con.Close()
Dim pdf As PdfDocument = New PdfDocument
pdf.Info.Title = "Database to Pdf"
Dim pdfPage As PdfPage = pdf.AddPage
Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage)
'pdf dosyaya aktarilacak kayitlarin fontunu ayarliyoruz.
Dim font As XFont = New XFont("Tahoma", 12, XFontStyle.Regular)
'title basligimizin fontunu ayarliyoruz.
Dim font2 As XFont = New XFont("Tahoma", 24, XFontStyle.Bold)
'kayitlarin pdf dosyada y koordinatini belirliyoruz
point = point + 100
'title basligimizin pdf dosyada y koordinatini belirliyoruz
point2 = point2 + 40
'title basligimiz
title = "List of Person"
For i = 0 To ds.Tables(0).Rows.Count - 1
'db deki satirlari stringe aktariyoruz
name = ds.Tables(0).Rows(i).Item(0)
sname = ds.Tables(0).Rows(i).Item(1)
contact = ds.Tables(0).Rows(i).Item(2)
'title basligimizi pdf dosyaya belirlenen koordinata yaziyoruz
graph.DrawString(title, font2, XBrushes.Black,
New XRect(200, point2, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
'db datalarimizi pdf dosyaya belirlenen koordinata yaziyoruz
graph.DrawString(name, font, XBrushes.Black,
New XRect(50, point, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
graph.DrawString(sname, font, XBrushes.Black,
New XRect(200, point, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
graph.DrawString(contact, font, XBrushes.Black,
New XRect(350, point, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
point = point + 40
Next
'db datalarimizi pdf dosya olarak kaydediyoruz.
Dim pdfFilename As String = "D:\save_db.pdf"
pdf.Save(pdfFilename)
Process.Start(pdfFilename)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
WebForm.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="write_database_to_pdf.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSave" runat="server" Text="Save" Width="77px" />
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN
0 comments:
Yorum Gönder