VB.NET ile 3. Parti yazılımlar kullanarak PDF yaratma, işleme ve çeşitli özelliklerle PDF dosyaları üzerinde yapılan işlemleri örnek kodlar eşliğinde anlatmaya çalışıyoruz.
---------------------------------------------------------------------------------------------------
Makalemde, sizlere VB.NET ile PDF yaratma ve PDF işleme ile ilgili bilgiler sunacağım. Projemize 3. Parti bir DLL ekleme zorunluluğumuz var bu dll’i http://www.tallcomponents.com/default.aspx?id=tallpdf3-download adresinden de çekebilirsiniz. Piyasada sınırsız bu işi yapan DLL ler varken bu firmanın dll’ini seçmemdeki amaç tamamen sınırsız kullanıma açık olmasıdır. Belli bir zaman kısıtlaması olmamasına rağmen deneme sürümü olduğunu uygulamamızda belirtiyor.
Buna ek olarak tamamen ücretsiz zLib’i de önerebilirim bu dll ise tamamen ücretsiz TallComponents’e göre doküman eksikliği biraz var ama source’ları açık olduğu için istediğiniz atraksiyonu yapabilirsiniz. zLib içinde
http://www.componentace.com/zlib_.NET.htm adresini kullanabilirsiniz. Bu makalemde zLib örneklerine değinmeyeceğim.
Projemize referans olarak:
TallComponents.PDF.Layout.DLL dosyasını eklemeniz gerekli.
Ardından çeşitli atraksiyonları PDF üzerinde gerçekleştirebiliriz.
Bir PDF dosyası yaratmak için aşağıdaki kodu kullanabilirsiniz:
Imports System
Imports System.IO
Imports TallComponents.PDF.Layout
Imports TallComponents.PDF.Layout.Paragraphs
Namespace TallComponents.Samples.TallPDF
Public Class HelloWorld
Shared Sub Main()
' Using
Dim file As FileStream = New FileStream("..\..\HelloWorld.pdf", FileMode.Create, FileAccess.Write)
Try
Dim document As Document = New Document
Dim section As Section = document.Sections.Add
Dim textParagraph As TextParagraph = New TextParagraph
section.Paragraphs.Add(textParagraph)
Dim fragment As Fragment = New Fragment
fragment.Text = "Hello world!"
textParagraph.Fragments.Add(fragment)
document.Write(file)
Finally
CType(file, IDisposable).Dispose()
End Try
End Sub
End Class
End Namespace
Hazır bulunan bir text dosyası veya text üzerinde biçimsel işlemler yaparak PDF olarak kaydetmek çok basit metodlarla mümkün kodun açıklaması kodun içinde yapılmıştır.
Imports System
Imports System.IO
Imports TallComponents.PDF.Layout
Imports TallComponents.PDF.Layout.Paragraphs
Namespace TallComponents.Samples.TallPDF
Public Class TextFormatting
Const text As String = "With TallPDF.NET you can create PDF documents " + "by building an instance of the TallPDF.NET Document " + "Object Model. The TallComponents team has produced " + "a DOM that is extremely intuitive and yet powerful. " + "This allows you to create both simple and complex " + "documents fast."
Shared Sub Main()
' Using
Dim file As FileStream = New FileStream("..\..\TextFormatting.pdf", FileMode.Create, FileAccess.Write)
Try
Dim document As Document = New Document
Dim section As Section = document.Sections.Add
section.PageSize = PageSize.A4
Dim textParagraph As TextParagraph
section.Paragraphs.Add(CreateTextParagraph("TextParagraph with default properties:"))
section.Paragraphs.Add(CreateTextParagraph(text, System.Drawing.Color.Blue))
section.Paragraphs.Add(CreateTextParagraph("textParagraph.HorizontalAlignment = HorizontalAlignment.Right"))
textParagraph = CreateTextParagraph(text, System.Drawing.Color.Blue)
textParagraph.HorizontalAlignment = HorizontalAlignment.Right
section.Paragraphs.Add(textParagraph)
section.Paragraphs.Add(CreateTextParagraph("textParagraph.HorizontalAlignment = HorizontalAlignment.Center"))
textParagraph = CreateTextParagraph(text, System.Drawing.Color.Blue)
textParagraph.HorizontalAlignment = HorizontalAlignment.Center
section.Paragraphs.Add(textParagraph)
section.Paragraphs.Add(CreateTextParagraph("textParagraph.Justified = true"))
textParagraph = CreateTextParagraph(text, System.Drawing.Color.Blue)
textParagraph.Justified = True
section.Paragraphs.Add(textParagraph)
section.Paragraphs.Add(CreateTextParagraph("textParagraph.LineSpacing = 5"))
textParagraph = CreateTextParagraph(text, System.Drawing.Color.Blue)
textParagraph.LineSpacing = 5
section.Paragraphs.Add(textParagraph)
section.Paragraphs.Add(CreateTextParagraph("textParagraph.FirstLineIndentation = 20"))
textParagraph = CreateTextParagraph(text, System.Drawing.Color.Blue)
textParagraph.FirstLineIndentation = 20
section.Paragraphs.Add(textParagraph)
section.Paragraphs.Add(CreateTextParagraph("textParagraph.HangIndentation = 20"))
textParagraph = CreateTextParagraph(text, System.Drawing.Color.Blue)
textParagraph.HangIndentation = 20
section.Paragraphs.Add(textParagraph)
document.Write(file)
Finally
CType(file, IDisposable).Dispose()
End Try
End Sub
Private Shared Function CreateTextParagraph(ByVal text As String) As TextParagraph
Return CreateTextParagraph(text, System.Drawing.Color.Black)
End Function
Private Shared Function CreateTextParagraph(ByVal text As String, ByVal textcolor As System.Drawing.Color) As TextParagraph
Dim paragraph As TextParagraph = New TextParagraph
Dim textFragment As Fragment = New Fragment(text)
textFragment.TextColor = textcolor
paragraph.Fragments.Add(textFragment)
paragraph.SpacingAfter = 10
Return paragraph
End Function
End Class
End Namespace
Aynı zamanda XML dosyalarınıda PDF formatına çevirebiliyoruz.
Örnek XML:
'<'document author="Volkan"'>'
'<'section pagesize="Letter"'>'
'<'!-- İçerik... --'>'
'<'/section'>'
'<'/document'>'
XML dosyamızı PDF çeviren kod bloğu,
Dim xml As XmlDocument = New XmlDocument
xml.Load("topdf.xml")
Dim document As Document = New Document
document.Read(xml.DocumentElement)
Dim file As FileStream = New FileStream("out.pdf", FileMode.Create)
document.Write(file)
Burada sayamayacağımız kadar çeşitli örneklerle kütüphane kullanımımızı ve yapacaklarımızı genişletebiliriz. Makalemin başında verdiğim linkten çektiğiniz zLib ile ilgili bir örnek yapmanın önemliliğini vurgulamak isterim her componenet değişik arayüzlerle gelmekte. Yine zLib’i referans olarak eklememiz gerekiyor.
Volkan Atasever