Unable to print the page after pdf generation in vb.net - javascript

Unable to print the window after pdf generation. Only pdf generation screens gets loaded. Please find the code below,
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fileData As Byte() = Nothing
Dim sJscript As String
If (Session("PrintDestinationStream") IsNot Nothing) Then
fileData = CType(Session("PrintDestinationStream"), Byte())
Session("PrintDestinationStream") = Nothing
End If
Response.Clear()
Response.ContentType = application/pdf
Response.BinaryWrite(fileData)
Response.Flush()
Response.Close()
If Session("Print") IsNot Nothing AndAlso Session("Print").ToString() = "PrintDoc" Then
Dim s As String = "<script language=""javascript"">"
s = s & "window.focus();window.print();"
s = s & "</script" & ">"
Page.ClientScript.RegisterStartupScript(Me.GetType(), "printdoc", s)
Session("Print") = Nothing
End If
End Sub
To open pdf file and to print the document

Related

Classic ASP error on function Base64_HMACSHA1

I have a function Base64_HMACSHA1 and am getting the error Expected ')'. The full code is:
Public Function Base64_HMACSHA1(ByVal sTextToHash As String, ByVal sSharedSecretKey As String)
Dim asc As Object, enc As Object
Dim TextToHash() As Byte
Dim SharedSecretKey() As Byte
Set asc = CreateObject("System.Text.UTF8Encoding")
Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")
TextToHash = asc.Getbytes_4(sTextToHash)
SharedSecretKey = asc.Getbytes_4(sSharedSecretKey)
enc.Key = SharedSecretKey
Dim bytes() As Byte
bytes = enc.ComputeHash_2((TextToHash))
Base64_HMACSHA1 = EncodeBase64(bytes)
Set asc = Nothing
Set enc = Nothing
End Function
Private Function EncodeBase64(ByRef arrData() As Byte) As String
Dim objXML As MSXML2.DOMDocument
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument
' byte array to base64
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
EncodeBase64 = objNode.Text
Set objNode = Nothing
Set objXML = Nothing
End Function
I've tried adding an ) in multiple places but I still get an error. The error message is for Line: 84, Column:51, which is this line: Public Function Base64_HMACSHA1(ByVal sTextToHash As String, ByVal sSharedSecretKey As String)
The code for the url is:
Dim objOAuth : Set objOAuth = New cLibOAuth
objOAuth.ConsumerKey = "0b57d617-7a92-4504-a5e1-25273e3b0384"
objOAuth.ConsumerSecret = "joSPols5B8uyKQqYzkk8uiwHrJ7nq3VwravLnTdJTFXMqSAq0KSBvPVoLETAmUiS"
objOAuth.EndPoint = "https://login.windstream.com/as/token.oauth2"
objOAuth.RequestMethod = OAUTH_REQUEST_METHOD_POST
objOAuth.TimeoutURL = "authenticate.asp"
'objOAuth.Parameters.Add "username", Request.Cookies("username")
'objOAuth.Parameters.Add "password", Request.Cookies("password")
objOAuth.Parameters.Add "oauth_callback", "callback.asp"
objOAuth.Send()
Dim strResponse : strResponse = _
objOAuth.Get_ResponseValue(access_token)

The use of pageNum property in Acrobat Type Library 10.0 JSObject returns RunTime error 438

I need to get the page number in order to extract text from that specific page in a .PDF document. I am using Excel VBA function that makes use of the JSObject from the Acrobat Type Library 10.0
Here is the code snippet and the code hicks up on when I am trying to reference the pageNum property from Doc object. I am trying to avoid the AV Layer and use the PD Layer only, so my macro runs in the background only and doesn't invoke Acrobat Application.
Function getTextFromPDF_JS(ByVal strFilename As String) As String
Dim pdDoc As New AcroPDDoc
Dim pdfPage As Acrobat.AcroPDPage
Dim pdfBookmark As Acrobat.AcroPDBookmark
Dim jso As Object
Dim BookMarkRoot As Object
Dim vBookmark As Variant
Dim objSelection As AcroPDTextSelect
Dim objHighlight As AcroHiliteList
Dim currPage As Integer
Dim strText As String
Dim BM_flag As Boolean
Dim count As Integer
Dim word As Variant
strText = ""
If (pdDoc.Open(strFilename)) Then
Set jso = pdDoc.GetJSObject
Set BookMarkRoot = jso.BookMarkRoot
vBookmark = jso.BookMarkRoot.Children
'Add a function call to see if a particular bookmark exists within the .PDF
Set pdfBookmark = CreateObject("AcroExch.PDBookmark")
BM_flag = pdfBookmark.GetByTitle(pdDoc, "Title Page")
If (BM_flag) Then
For i = 0 To UBound(vBookmark)
If vBookmark(i).Name = "Title Page" Then
vBookmark(i).Execute
jso.pageNum
Set pdfPage = pdDoc.AcquirePage(pageNum)
Set objHighlight = New AcroHiliteList
objHighlight.Add 0, 10000 ' Adjust this up if it's not getting all the text on the page
Set objSelection = pdfPage.CreatePageHilite(objHighlight)
If Not objSelection Is Nothing Then
For tCount = 0 To objSelection.GetNumText - 1
strText = strText & objSelection.GetText(tCount)
Next tCount
End If
Exit For
End If
pdDoc.Close
End If
End If
getTextFromPDF_JS = strText
End Function
jso.pageNum = 0; set a page number
pageNo = jso.pageNum; get a page number
edit: 3.3.19
Mmmh, it seems you have to work with AVDoc in order to get the current actual page via jso.pageNum . Also if you work with AVdoc the Acobat window stay hidden in the background. Example:
strFilename = "d:\Test2.pdf"
set avDoc = CreateObject("AcroExch.AVDoc")
If (avDoc.Open(strFilename,"")) Then
Set pdDoc = avDoc.getPDDoc()
Set jso = pdDoc.GetJSObject
pageNo = jso.pageNum
msgbox(pageNo)
end if

Save File created by Javascript IE using vba

I have a routine that logs in and calls a script from the page where you are logged in, but this report is available to save from the internet and I need it saved automatically without user intervention.
Sub ConnectWeb()
Dim ie As InternetExplorer
Dim C
Dim ULogin As Boolean, ieForm
Dim MyPass As String, MyLogin As String
Dim Linha As Integer
Dim PN As String
MyLogin = Application.InputBox("Por Favor entre com o Login", "Empresa", Default:="User", Type:=2)
MyPass = Application.InputBox("Por favor entre com a senha", "Empresa", Default:="Password", Type:=2)
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://url"
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
ie.Document.all("cuser").innerText = MyLogin
ie.Document.all("cpass").innerText = MyPass
ie.Document.getElementById("cent").Value = "BR"
ie.Document.forms(0).submit
Do While ie.Busy
DoEvents
Loop
PN = "D515005-5304"
'JavaScript to create file
ie.Document.parentWindow.execScript ("printPL('" & PN & "','N%2FC','no')")
End Sub
After the Java script call appears the message with the file generated and the option of Internet Explorer Save, at the moment I need the file to be saved automatically, and preferably in a specific path.
Since then, thanks for the help.
I chose to use chrome and found that javascript sends information byurl, so I used the following logic
Sub test()
Dim chromePath, PN As String
Dim Linha As Integer
chromePath = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"""
Linha = 2
Cells(Linha, "A").Select
Do While ActiveCell <> ""
PN = Cells(Linha, "A").Value
strURL = "http://cdroyal4.cdzodiac.com/cgi-bin/wspd_cgi.sh/WService=wslive/wpt/function.r?funct=print.pl&drawing-no=" & PN & "&prelim=no&rev=N%2FC&filename=" & PN
Shell (chromePath & strURL)
Linha = Linha + 1
Cells(Linha, "A").Select
Loop
MsgBox "Complete", vbInformation, "Empresa"
End Sub
With Chrome the download is done automatically, resulting in the expected result.
Thanks to all who have reviewed

PageMethods not working

I am trying to learn how to use PageMethods with VB.NET and I'm running into some issues. For some reason I can't manage to get the method called by PageMethods to run.
Here is the javascript function:
<script type="text/javascript">
function AddHouse() {
var address = document.getElementById("addrTxt").valueOf;
var city = document.getElementById("cityTxt").valueOf;
var state = document.getElementById("stateTxt").valueOf;
var zip = parseInt(document.getElementById("zipTxt").valueOf);
var firstName = document.getElementById("rFirstName").valueOf;
var lastName = document.getElementById("rLastName").valueOf;
var rent = parseInt(document.getElementById("rentAmt").valueOf);
PageMethods.InsertHouse(address, city, state, zip, firstName, lastName, rent);
}
</script>
And here's the VB.NET code:
Public Class _Default
Inherits Page
Private Shared dbConnection As String
Private file As String = "C:\\Projects\\HousingInfo\\HousingInfo\\bin\\housingInfo.db3"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dbConnection = String.Format("Data Source={0}", file)
CreateTables()
End Sub
Private Sub CreateTables()
Dim connection = New SQLiteConnection(dbConnection)
If Not My.Computer.FileSystem.FileExists("C:\\Projects\\HousingInfo\\HousingInfo\\bin\\housingInfo.db3") Then
SQLiteConnection.CreateFile("C:\\Projects\\HousingInfo\\HousingInfo\\bin\\housingInfo.db3")
End If
Using Query As New SQLiteCommand()
connection.ConnectionString = dbConnection
connection.Open()
With Query
.Connection = connection
.CommandText = "CREATE TABLE IF NOT EXISTS houses(id INTEGER PRIMARY KEY AUTOINCREMENT, address TEXT, city TEXT, state TEXT,
zipCode INTEGER, rent INTEGER, rFirstName TEXT, rLastName TEXT)"
End With
Query.ExecuteNonQuery()
connection.Close()
End Using
End Sub
<Services.WebMethod()>
Public Shared Sub InsertHouse(ByVal addr As String, ByVal city As String, ByVal state As String, ByVal zip As Integer, ByVal firstName As String,
ByVal lastName As String, ByVal rent As Integer)
Dim connection = New SQLiteConnection(dbConnection)
Using Query As New SQLiteCommand()
connection.ConnectionString = dbConnection
connection.Open()
With Query
.Connection = connection
.CommandText = String.Format("INSERT INTO houses(address, city, state, zipCode, rent, rFirstName, rLastName) VALUES ('{0}', '{1}', '{2}', {3}, {4}, '{5}', '{6}'",
addr, city, state, zip, firstName, lastName, rent)
End With
Query.ExecuteNonQuery()
connection.Close()
End Using
End Sub
End Class
You are using the wrong property to get the value of the controls, rename 'valueOf' to 'value' as follows:
Do this for all other occurrences of 'valueOf'
var address = document.getElementById("addrTxt").value;

Getting value from SQL query to textbox

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim cons, query As String
Dim con As OdbcConnection
Dim adpt As OdbcDataAdapter
'Dim num As Integer
cons = "dsn=Courier; UID=Courier; PWD=123;"
con = New OdbcConnection(cons)
con.Open()
query = "select Name from EMPLOYEE where EMPLOYEE_ID=" + DropDownList1.SelectedValue
Dim ds As DataSet
adpt = New OdbcDataAdapter(query, con)
ds = New DataSet
adpt.Fill(ds, "Courier")
' TextBox1.Text = ds
con.Close()
End Sub
I want to display the name of the employee in Textbox whoos ID is specified in query, what can I do for that?
You should use DataRow but to answer your question, try this.
TextBox1.Text = ds.Tables(0).Rows(0)("Name").ToString()
Since you only want one value back you should skip the dataset and adapter altogether.
query = "select Name from EMPLOYEE where EMPLOYEE_ID=" + DropDownList1.SelectedValue
Dim TempName As String = query.ExecuteScalar
TextBox1.Text = TempName
ExecuteScalar returns the first cell of the first row, that's all you need.
You should read about parameters as well.

Categories