Click here to Skip to main content
14,971,215 members
Articles / Programming Languages / Visual Basic
Article
Posted 31 Mar 2018

Tagged as

Stats

9K views
242 downloads
4 bookmarked

Printing with CodeDom Scripts

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
5 Apr 2018CPOL5 min read
Every programmer, sooner or later, has the same problem: how can I print quickly and simply?

Introduction

I'm a developer since several years and when I developed my software, each time I had the same problems about print management.

Microsoft's print method/object are really difficult to use... in my opinion, they are not programmer-friendly and they don't have native methods to make pdf.

In the past, I built a kind of script engine, based on VBScript that makes a print through a vbscript file definition. It was developed in VB6 and doesn't make PDF files.

In the last few weeks, I developed a new engine based on CodeDom scripts, ItextSharp and barCodeLib, to make PDF files from a script.

Background

The architecture of my engine is complex and is not simple to describe, but for using it, you can be a beginner programmer in VB.NET. You don't need to have particular skills. When you are able to develop in VB.NET, it is enough.

What Is This Library

My library is a tool that allows you to print easily from each software that you've developed. I made a library that you can add, to your own project and deploy with it.

It consists in a kind of "script based engine", able to produce PDF files using the commands that I've implemented. In this way, you don't need to write print subroutines inside your source code... you can manage prints with scripts outside your source code. So it should be very easy to manage it.

Using the Code

Now we can start with the first example:

Make a text files (script) with Notepad and write into file:

VB.NET
sub Settings()
    gp_print.PageFormat = "A4"
    gp_print.orientation="Horiz"
end sub 

sub Head()

end sub

sub Body()
   gp_Print.Text (10,30,"SIMPLE TEXT")
end sub

sub Foot()

end sub

Save your file as Example.Prt for example in C:\Temp folder.

Ok, first step is done, now create a new Windows form project in VB.NET.

Add GP_PrintEngine as reference (remember that GP_PrintEngine contains references to iTextSharp and BarCodeLib that must be copied in the same folder of GP_PrintEngine.DLL).

Then, in form1, we add a simple button, and write this code:

VB.NET
Public Class Form1

   Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
      Dim myEngine As New GP_PrintEngine.GP_PrintEngine
      myEngine.MakePdf("c:\temp", "example.prt", "c:\temp", "mytest")
   End Sub
 
End Class

The parameters of MakePDF are:

  • Path of script file
  • Name of script
  • Output PDF path
  • PDF file name

Now we can run the application and after clicking over the button, a PDF will be created into C:\Temp folder.

The output PDF file will contain a text "SIMPLE TEXT" printed in position x,y (10mm, 30mm from the upper-left corner of your page).

Now we can update our script with others instructions, for example, we can set set font name and font size..

VB.NET
sub Settings()
    gp_print.PageFormat = "A4"
    gp_print.orientation="Horiz"
end sub 

sub Head()

end sub

sub Body()
  gp_Print.fontname="arialbd"
  gp_Print.FontSize=10
  gp_Print.Text (10,30,"SIMPLE TEXT")
end sub

sub Foot()

end sub

Well, like you can see in the sample script, we defined 4 subroutines: Settings, Head, Body and Foot. These subroutines are a kind of "system subroutines" that must be defined in your script, and the GP_PrintEngine executes the script running these subroutines in sequence.

You can try to change the script by creating and calling others subroutines like you want, the only thing that you must define is Setting sub in which you must define paper information.

For example:

VB.NET
sub Settings()
 gp_print.PageFormat = "A4"
 gp_print.orientation="Horiz"
end sub 

sub Head() 
   gp_Print.fontname="arialbd"  
   gp_Print.FontSize=15
   gp_Print.Text (10,30,"THIS IS HEAD OF THE PAGE") 
end sub 

sub Body() 
   gp_Print.fontname="arial"  
   gp_Print.FontSize=10 
   gp_Print.Text (10,80,"this is a body") 

   call myOwnSub()

end sub 


Sub MyOwnSub ()
   for i as integer = 0 to 9
      gp_Print.Text (50,100 + (i*4),"Example Line " & i) 
   next 
end sub 


sub Foot() 
end sub

If you run Windows Form project with this script again, you'll have head text on the top of the page, the text "this is a body" and 9 text rows in the center of the page.

Now I think that the concept is clear... In your script file, you can write using VB.NET language, calling sub and functions, and using all commands that I built to print lines, grids, etc. Into download, I included a PDF file with list of supported commands. Here is another sample script full of sample commands:

VB.NET
sub Settings()
    gp_print.PageFormat = "A4"
    gp_print.orientation="Horiz"
end sub 
sub head ()
    
    gp_print.fontname="ARIALBD"
    gp_print.fontsize=40
   
    gp_Print.Text (10,30,"Demo Text")
    call PageDemo()
    call TabeleTest()
end sub 

Sub TableTest()
    gp_print.newpage ("Vertical")
    gp_Print.InitGrid (20,20,30,5,5,10,70,49,1,2,1)
    gp_Print.InitGrid_Header (      0,   50,  "Col 1",     
    "",  220,  220,  220,  "arial",    8,     0,    0,    0,    0,      
    "arial",         8,          ,       0,        0,        0)
    gp_Print.InitGrid_Header (      1,   80,  "Col 2",     
    "",  220,  220,  220,"arialBD",    9,     0,    0,    0,    0,      
    "arial",         8,          ,       0,        0,        0)
    
    gp_Print.GridData_cleardata
    
    for i as integer=0 to 20
        gp_Print.GridData_SetValue  (i,0,"value c1-r" & i ,"Arial")
        gp_Print.GridData_SetValue  (i,1,"value c2-r" & i ,"Arial")
    next 

    gp_Print.PrintGrid
End Sub

sub PageDemo()

    gp_Print.fontname="arialbd"
    gp_Print.FontSize=10
    gp_Print.text (50,5,"HEAD",1,255,0,0)
    gp_Print.fontname="arialI"
    gp_Print.FontSize=14
    gp_Print.text (50,50,"arialI",0)
        
    gp_Print.fontname="arialbd"
    gp_Print.FontSize=12
    gp_Print.text (50,55,"calibriB",1,255,0,0)
    
    gp_Print.fontname="arial"
    gp_Print.FontSize=10
    gp_Print.text (gp_Print.xmax,55,"calibriB",2)
    gp_Print.line (0,0,gp_Print.xmax,gp_Print.ymax)
    gp_Print.line (0,gp_Print.ymax,gp_Print.xmax,0,0,255,0)

    gp_Print.RotText (cint(gp_Print.xmax/2),cint(gp_Print.ymax/2),"rotated text",90)
    gp_Print.text (100,155,"row1",0)
    gp_Print.texty ("Row2",0,0,0,255)
    gp_Print.texty ("Row3 bla bla bla",2,0,255,255)
    
    gp_Print.box (80,80,100,10,0,255,255,0,0)
    gp_Print.box (80,120,100,10,255,0,255,1,0)
    
    gp_Print.imgfile ("c:\test.jpg",20,130,50,30,1,0)
    gp_Print.imgfile ("c:\test.jpg",20,130,50,30,4,9)

    gp_Print.barcode ("39",1,180,80,10,"BarCode")

end sub

Now we are ready to do the next step: how can we send application data to the script?

The answer is really simple: GP_PrintEngine contains a generic class GP_Interface that can be implemented in your source code.

Here is an example:

VB.NET
Public Class MyClass

   Public Function Getvalue() As String
      Return "Test value"
   End Function

   Public Function GetSumResult( sum1 as single, sum2 as single) As single
      dim ret as single = sum1+sum2
      Return ret
   End Function

End Class

To connect your Class (myClass) to the GP_Interface class, you need to use InitEngine command:

VB.NET
Public Class Form1 Private 
   Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click 
      Dim myCls As New MyClass
      Dim myEngine As New GP_PrintEngine.GP_PrintEngine   
      myEngine.InitEngine(myCls)
      myEngine.MakePdf("c:\temp", "example.prt", "c:\temp", "mytest") 
   End Sub 
end Class

Public Class MyClass    
   Public Function Getvalue() As String       
      Return "Test value"    
   End Function   
 
   Public Function GetSumResult( sum1 as single, sum2 as single) As single 
      dim ret as single = sum1+sum2       
      Return ret    
   End Function 
End Class

Here, you have a script example that recalls the property of myClass.

VB.NET
sub Settings()
    gp_print.PageFormat = "A4"
    gp_print.orientation="Vertical"
end sub

sub head ()
    
    gp_print.fontname="ARIALBD"
    gp_print.fontsize=20

    'we can call the sub and functions of MyClass
 
    dim myText as string = "This is a calling of MyClass " & gp_interface.getvalue()
    gp_Print.Text (10,30,aa)

    dim myValue as single = gp_interface.GetSumResult( 12, 7) 
    gp_Print.Text (10,50, myvalue)
   
end sub 

I think that now you have all the information to start work with my engine. In linked archive, you have a PDF file with all commands that the engine supports now. At the moment, the description is in Italian language, but soon I'll translate it, and however the commands are really easy to understand. I think that in future, I'll implement the commands supported.

I hope this article and my library are useful for all, and I await your feedback and considerations.

Points of Interest

I think that the library that I made will be appreciated by a lot of programmers, from my point of view, it is a new way to approach Print management for developed applications. When you have a print definition outside your EXE, it is very simple to make all changes or customizations that the customer asks you.

Another interesting thing found during my development was CodeDom technology that I used for running a file script. It was a bit hard for me, to use this technology for the first time, but when you understood the working flow is also simple, implement it. I will work around errors script management, because sometimes, the errors made writing the scripts difficult to debug quickly and you need to put a lot of messages to debug it.

History

If other programmers will appreciate my work, I will be happy to share my new implementations.

Thank you for reading!

www.linkedin.com/in/gianpietro-tomasella-27328374

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

giamp_72
Italy Italy
No Biography provided

Comments and Discussions

 
Questiongreat Pin
ronkyone6-Apr-18 4:29
Memberronkyone6-Apr-18 4:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.