Solved

Need your Kind Support with full detail

Posted on 2008-10-17
18
648 Views
Last Modified: 2008-10-21
Hello Friends,

I need to create a seperate class for convert a HTML file to PDF , and I've got the 3rd Part PDF converter(DLL object). its called ExpertPDF. also they have provided some sample code to create the conversion. In my project I have seperate set of my own Dlls AND Class files. I want to use that PDF dll without adding into my project. and also I need to create Class for that conversion in my project . How can I call that DLL. and also please let me know how can i create a common seperate class for that PDF converion (Because I need to use that conversion in several other projects in the future.
Here the sample code given by the 3rd Part PDF converter,

*************************************************************************************************************************
Imports System.Drawing
Imports ExpertPdf.HtmlToPdf

Partial Class DefaultVB
    Inherits System.Web.UI.Page

    Protected Sub BtnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnExport.Click
        Dim url As String = TxtURL.Text.Trim()
        Dim downloadName As String = "ExportedReport.pdf"

        If (Not Page.IsValid) Then
            Return
        End If

        Dim pdfConverter As PdfConverter = New PdfConverter

        pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4
        pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal
        pdfConverter.PdfDocumentOptions.ShowHeader = True
        pdfConverter.PdfDocumentOptions.ShowFooter = True
        pdfConverter.PdfDocumentOptions.LeftMargin = 5
        pdfConverter.PdfDocumentOptions.RightMargin = 5
        pdfConverter.PdfDocumentOptions.TopMargin = 5
        pdfConverter.PdfDocumentOptions.BottomMargin = 5
        pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = True

        pdfConverter.PdfDocumentOptions.ShowHeader = False
        'pdfConverter.PdfHeaderOptions.HeaderText = "Sample header: " + TxtURL.Text
        'pdfConverter.PdfHeaderOptions.HeaderTextColor = Color.Blue
        'pdfConverter.PdfHeaderOptions.HeaderDescriptionText = string.Empty
        'pdfConverter.PdfHeaderOptions.DrawHeaderLine = false

        pdfConverter.PdfFooterOptions.FooterText = ("Sample footer: " _
                    + (TxtURL.Text + ". You can change color, font and other options"))
        pdfConverter.PdfFooterOptions.FooterTextColor = Color.Blue
        pdfConverter.PdfFooterOptions.DrawFooterLine = False
        pdfConverter.PdfFooterOptions.PageNumberText = "Page"
        pdfConverter.PdfFooterOptions.ShowPageNumber = True

        'pdfConverter.LicenseKey = "put your license key here"
        Dim downloadBytes() As Byte = pdfConverter.GetPdfFromUrlBytes(url)
        Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response

        response.Clear()
        response.AddHeader("Content-Type", "binary/octet-stream")
        response.AddHeader("Content-Disposition", ("attachment; filename=" _
                        + (downloadName + ("; size=" + downloadBytes.Length.ToString))))
        response.Flush()
        response.BinaryWrite(downloadBytes)
        response.Flush()
        response.End()


    End Sub
End Class
/********************************************************************************************************************
0
Comment
Question by:s_maha1978
  • 9
  • 8
18 Comments
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22745081
If you dont want to add this dll into your project you can do either of the below
1. create a seperate class library  and write your class there, in this class library you can add reference to this dll and use it. in your main application you can reference your dll
2. you have to use dotnet reflections to do this
0
 

Author Comment

by:s_maha1978
ID: 22745210
Hi TechTiger007, could yu explain to me in detail by coding the correct, because I'm new to vb.net . Thanks for your valuable help.
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22745350
I have added explanation of code as comment over each line. Hope that helps :-) enjoy coding

'' importing the dll
Imports System.Drawing
Imports ExpertPdf.HtmlToPdf


''class declaration starts here
Partial Class DefaultVB
    Inherits System.Web.UI.Page

''Handler for export button click event starts
    Protected Sub BtnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnExport.Click

''get teh url from the text box
        Dim url As String = TxtURL.Text.Trim()

''set the name of the pdf file to be created
        Dim downloadName As String = "ExportedReport.pdf"

'if page is not valid (asp.net validation) then return
        If (Not Page.IsValid) Then
            Return
        End If

' create an object of type PdfConverter (should be the class used for conversion)
        Dim pdfConverter As PdfConverter = New PdfConverter

'set all the properties for the exported pdf file (property names are self explaining)
        pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4
        pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal
        pdfConverter.PdfDocumentOptions.ShowHeader = True
        pdfConverter.PdfDocumentOptions.ShowFooter = True
        pdfConverter.PdfDocumentOptions.LeftMargin = 5
        pdfConverter.PdfDocumentOptions.RightMargin = 5
        pdfConverter.PdfDocumentOptions.TopMargin = 5
        pdfConverter.PdfDocumentOptions.BottomMargin = 5
        pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = True

        pdfConverter.PdfDocumentOptions.ShowHeader = False
        'pdfConverter.PdfHeaderOptions.HeaderText = "Sample header: " + TxtURL.Text
        'pdfConverter.PdfHeaderOptions.HeaderTextColor = Color.Blue
        'pdfConverter.PdfHeaderOptions.HeaderDescriptionText = string.Empty
        'pdfConverter.PdfHeaderOptions.DrawHeaderLine = false

        pdfConverter.PdfFooterOptions.FooterText = ("Sample footer: " _
                    + (TxtURL.Text + ". You can change color, font and other options"))
        pdfConverter.PdfFooterOptions.FooterTextColor = Color.Blue
        pdfConverter.PdfFooterOptions.DrawFooterLine = False
        pdfConverter.PdfFooterOptions.PageNumberText = "Page"
        pdfConverter.PdfFooterOptions.ShowPageNumber = True
'end of property assignment


'assign the license key string you got for this pdf converter to this property. I think you should be uncommenting this
        'pdfConverter.LicenseKey = "put your license key here"

'this function converts the page in the url as pdf and returns as a byte array
        Dim downloadBytes() As Byte = pdfConverter.GetPdfFromUrlBytes(url)

The logic below is to download the byte array as pdf document to the client machine
        Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response

        response.Clear()
        response.AddHeader("Content-Type", "binary/octet-stream")
        response.AddHeader("Content-Disposition", ("attachment; filename=" _
                        + (downloadName + ("; size=" + downloadBytes.Length.ToString))))
        response.Flush()
        response.BinaryWrite(downloadBytes)
        response.Flush()
        response.End()
'download completes here

    End Sub
End Class
0
 

Author Comment

by:s_maha1978
ID: 22745433
Hi TechTiger007, How do you refer the external DLL in the beginning which is not in my project? (How to link that ?)
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 22745954
First, why don't you want to add it to your project?  Is it not a .NET dll or COM .Dll?  Because, if not, you'll have to use a different method of accessing it.
0
 

Author Comment

by:s_maha1978
ID: 22746615
Because as  cOMPANY POLICY , WE CAN'T ADD IT IN TO OUR PROJECT AND WE HAVE TO USE IT FROM EXTERNALLY. IT'S NOT NET.DLL OR COM.DLL..PLEASE LET ME KNOW...
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22747301
s_maha1978, it looks like a dot net dll with namespace "ExpertPdf.HtmlToPdf"
If you dont want to include it in your project, then create a separate class library project and include it inside that project. Your new class library should be working as a wrapper to the dll.
0
 

Author Comment

by:s_maha1978
ID: 22747819
Hello TechTiger007, I understood, could you explain the bit how can i include the another projects IF WE WANT? (if you can , pleaseshow in coding ) thanks for the great help
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22748080
1. Create a class library project
2. Add reference to the third party dll in this project
3. Create a method in the class that will export page to pdf file
4. Use the third party dll methods to export page to pdf inside this method
5. Build the class library, you will get your wrapper dll
6. Now in your main project add reference to this wrapper dll
7. Call the method in wrapper dll to export page to pdf (your wrapper dll will communicate with third party dll and do what is needed)

In this approach if you want to change the third party dll you dont have to modify code in your main project. All you have to do is to modify the wrapper class if needed.
This is the approach normally used when an application has to communicate with a third party application to make sure that interface between the applications remain same

Hope that helps

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:s_maha1978
ID: 22752254
Hi ! How to add a reference to the third party dll in the class ? can you please let me know by coding ..thanks
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22752332
When I said add reference it is not by coding. Goto solution explorer, right click on the class library project, select add reference, this will open a pop up window. click on browse tab in this window and choose the third party dll and click add. this will add the dll to your class library. Now you have to include the namespace as in the sample you gave and you can use the third party dll as any other dlls.
0
 

Author Comment

by:s_maha1978
ID: 22752352
As i said before I dont want to include that DLL into my reference , I want to refer the the dll by specifying the path (c:/hjmhjmj) ..its like calling DLL from another destination
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22752547
As :I said, the method I explained above is not adding dll to your main project and that is the approach we use most of the time.

Well if you want to load assembly through code and invoke methods on it then as I said earlier you should be using reflections to do this

find below samples on using reflections to achieve what you need

Loading assembly through code
http://www.java2s.com/Tutorial/VB/0140__Development/LoadAssemblyfromDLL.htm

Method invocation using reflection
http://www.dreamincode.net/forums/showtopic27135.htm
http://www.dotnetspider.com/resources/4634-Invoke-me-ods-dynamically-using-reflection.aspx

Another sample
http://www.devasp.net/net/articles/display/230.html

Hope that helps
0
 

Author Comment

by:s_maha1978
ID: 22752663
HI TechTiger007,
According to your first sample "loading assembly" , andHow do i called its members> could let me know...please...as a favour..thanks a lot
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22752718
The second and third sample shows you how to call members of an assembly.
0
 

Author Comment

by:s_maha1978
ID: 22753147
Hi TechTiger007,

I've declared the assembly like the below,

 Dim dalcAssembly As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom("c:\Expert\ephtmltopdf.dll")

Dim ctype s Type = dalcAssembly .GetType("ExpertPdf.HtmlToPdf.PdfDocumentOptions")

Now I need yo use the members of he above type and set the appropriate value to that members. How an do that ? from my first question you can see the members of PdfDocumentOptions. Thanks(Example :pdfpage size)
0
 
LVL 13

Accepted Solution

by:
TechTiger007 earned 500 total points
ID: 22760895
I have given a sample on how to start. There could be syntax errors since I cannot test this because I dont have this dll. But this is how you should be going on

dim obj1 as object = Activator.CreateInstance(ctype)
PropertyInfo pdfProperty = ctype.GetProperty("PdfDocumentOptions")

PropertyInfo license = ctype.GetProperty("LicenseKey")
license.SetValue(obj1,"<license>",null)

dim propType as PropertyInfo = pdfProperty.GetType()
PropertyInfo innerProp1 = proptype.GetProperty("ShowPageNumber")
innertProp1.SetValue(pdfProperty,true,null)

Here are links for your reference

setting property values
http://articles.techrepublic.com.com/5100-10878_11-6099345.html

Create object and invoke the methods
http://www.java2s.com/Code/CSharp/Development-Class/Createanobjectusingreflection.htm
http://www.c-sharpcorner.com/UploadFile/harishankar2005/Reflectionin.NET12032005045926AM/Reflectionin.NET.aspx


You should be able to take from here


0
 

Author Comment

by:s_maha1978
ID: 22762317
Hi Tiger ! Thanks for the spot help. also I just want a last help from you. that i've got something like this below,

  Dim downloadBytes() As Byte = pdfConverter.GetPdfFromUrlBytes(url) ++++ Its from the PDF guys+++

Now I need to get that " GetPdfFromUrlBytes " from the assembly type. please atlast please help me to find that and execute...thanks...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to get a file from SqlServer DB 4 29
reports, calling from code. 3 31
Duplicate a row 2 33
.Net Web Site Password specs 2 21
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now