Hi everyone
I am posting a An Example that Describe that how to update parameter and export the webi report in pdf.with new parameters
/*-----------------------------------------------------------------------------------------------------*/
Imports System.Windows.Forms
Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports System.Security.Permissions
Imports System.Security.Cryptography
Imports System.Diagnostics
Imports System.Security
Imports System.Security.AccessControl
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Net
Imports iTextSharp.text.pdf
Module Module1
Sub Main()
' UpdateParam()
Dim args() As String = System.Environment.GetCommandLineArgs()
For i As Integer = 1 To args.Length - 1
If (args(1) = "PARAMETER" Or args(1) = "parameter") Then
GenerateParameter(args(2))
Else
sendRequest(args(i))
End If
Next
End Sub
Public Sub sendRequest(ByVal value As String)
Console.WriteLine("Request Assigned")
Dim query As String = "select Account_Name from bi.DM_Account"
Dim dsQ As DataSet = GetDataSet(query, "QueryResult")
If dsQ.Tables("QueryResult").Rows.Count > 0 Then
value = value.Replace("#", " ")
writeParam(value)
exportReport(value)
End If
End Sub
Public Function GenerateParameter(ByVal value As String)
Dim userName As String = "connect"
Dim password As String = "abc@321"
Dim auth As String = "secEnterprise"
Dim baseURL As String = "http://xxx:6405/biprws/"
Dim LogonURI As String = baseURL & Convert.ToString("logon/long")
Dim Logonof As String = "http://xxx:6405/biprws/logoff"
Dim InfoStoreURI As String = baseURL & Convert.ToString("raylight/v1/documents/" & value & "/parameters")
'260515 is the document id
Dim rwsLogonToken As String
Try
Dim myWebRequest As WebRequest = WebRequest.Create(LogonURI)
myWebRequest.ContentType = "application/xml"
myWebRequest.Method = "GET"
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
Dim sr As New StreamReader(myWebResponse.GetResponseStream())
Dim output As String = sr.ReadToEnd()
Dim doc As New XmlDocument()
doc.LoadXml(output)
Dim nodelist As XmlNodeList = doc.GetElementsByTagName("attr")
For Each node As XmlNode In nodelist
If node.Attributes("name").Value = "userName" Then
node.InnerText = userName
End If
If node.Attributes("name").Value = "password" Then
node.InnerText = password
End If
If node.Attributes("name").Value = "auth" Then
node.InnerText = auth
End If
Next
Dim myWebRequest1 As WebRequest = WebRequest.Create(LogonURI)
myWebRequest1.ContentType = "application/XML"
myWebRequest1.Method = "POST"
Dim reqBodyBytes As Byte() = System.Text.Encoding.[Default].GetBytes(doc.OuterXml)
Dim reqStream As Stream = myWebRequest1.GetRequestStream()
reqStream.Write(reqBodyBytes, 0, reqBodyBytes.Length)
reqStream.Close()
Try
Dim myWebResponse1 As WebResponse = myWebRequest1.GetResponse()
rwsLogonToken = myWebResponse1.Headers("X-SAP-LogonToken").ToString()
Dim pXML As New XmlDocument()
pXML.Load("D:\New folder\Amit Sharma\Temp\Export report\Export report\bin\Release\param.xml")
Dim dataByte As Byte() = System.Text.Encoding.[Default].GetBytes(pXML.OuterXml)
Dim POSTRequest As HttpWebRequest = DirectCast(WebRequest.Create(InfoStoreURI), HttpWebRequest)
POSTRequest.Method = "PUT"
POSTRequest.ContentType = "application/xml"
POSTRequest.Headers.Add("X-SAP-LogonToken", rwsLogonToken)
POSTRequest.Timeout = 500000
POSTRequest.KeepAlive = False
POSTRequest.ContentLength = dataByte.Length
Dim POSTstream As Stream = POSTRequest.GetRequestStream()
POSTstream.Write(dataByte, 0, dataByte.Length)
POSTstream.Close()
Dim POSTResponse As HttpWebResponse = DirectCast(POSTRequest.GetResponse(), HttpWebResponse)
Dim reader As New StreamReader(POSTResponse.GetResponseStream(), System.Text.Encoding.UTF8)
Console.WriteLine("Response from Server:")
Console.WriteLine(reader.ReadToEnd().ToString())
Dim myWebRequest2 As HttpWebRequest = HttpWebRequest.Create(InfoStoreURI)
myWebRequest2.Accept = "application/xml"
myWebRequest2.Headers.Add("X-SAP-LogonToken", rwsLogonToken)
myWebRequest2.Method = "GET"
Dim myWebResponse2 As WebResponse = myWebRequest2.GetResponse()
Dim stream As New FileStream("D:\New folder\Amit Sharma\Temp\Export report\Export report\bin\Release\Parameters.xml", FileMode.Create)
myWebResponse2.GetResponseStream().CopyTo(stream)
stream.Close()
Dim myWebRequestof As WebRequest = WebRequest.Create(Logonof)
myWebRequestof.ContentType = "application/xml"
myWebRequestof.Method = "POST"
Dim myWebResponseof As WebResponse = myWebRequestof.GetResponse()
Dim srof As New StreamReader(myWebResponseof.GetResponseStream())
Dim outputof As String = srof.ReadToEnd()
Console.WriteLine(outputof)
Catch generatedExceptionName As Exception
Console.WriteLine("Error in communicating with Server" & generatedExceptionName.Message)
End Try
Catch ex As Exception
Console.Write("<b>" + ex.Message + "</b>")
End Try
Return 0
End Function
Public Function writeParam(ByVal param As String)
Console.WriteLine("Write Parameter Start")
Dim pXML As New XmlDocument()
pXML.Load("D:\New folder\Amit Sharma\Temp\Export report\Export report\bin\Release\param.xml")
Dim child_nodes As XmlNodeList = pXML.GetElementsByTagName("value")
Console.WriteLine("Parameter :" & child_nodes.Count.ToString)
For Each child As XmlNode In child_nodes
child.InnerText = param
Next child
Dim writer As XmlWriter = XmlWriter.Create("D:\New folder\Amit Sharma\Temp\Export report\Export report\bin\Release\param.xml")
pXML.WriteContentTo(writer)
writer.Close()
' pXML.Save("D:\Amit Sharma\Temp\Export report\Export report\bin\Release\param.xml")
Console.WriteLine("Write Parameter End")
Return 0
End Function
'---------------------------PDF Encryption----------------------------
Sub EncryptPdf(ByVal sInFilePath As String, ByVal sOutFilePath As String, ByVal sPassword As String)
Dim oPdfReader As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(sInFilePath)
Dim oPdfDoc As New iTextSharp.text.Document()
Dim oPdfWriter As PdfWriter = PdfWriter.GetInstance(oPdfDoc, New FileStream(sOutFilePath, FileMode.Create))
oPdfWriter.SetEncryption(PdfWriter.STRENGTH40BITS, sPassword, sPassword, PdfWriter.AllowCopy)
oPdfDoc.Open()
oPdfDoc.SetPageSize(iTextSharp.text.PageSize.LEDGER.Rotate())
Dim oDirectContent As iTextSharp.text.pdf.PdfContentByte = oPdfWriter.DirectContent
Dim iNumberOfPages As Integer = oPdfReader.NumberOfPages
Dim iPage As Integer = 0
Do While (iPage < iNumberOfPages)
iPage += 1
oPdfDoc.SetPageSize(oPdfReader.GetPageSizeWithRotation(iPage))
oPdfDoc.NewPage()
Dim oPdfImportedPage As iTextSharp.text.pdf.PdfImportedPage = oPdfWriter.GetImportedPage(oPdfReader, iPage)
Dim iRotation As Integer = oPdfReader.GetPageRotation(iPage)
If (iRotation = 90) Or (iRotation = 270) Then
oDirectContent.AddTemplate(oPdfImportedPage, 0, -1.0F, 1.0F, 0, 0, oPdfReader.GetPageSizeWithRotation(iPage).Height)
Else
oDirectContent.AddTemplate(oPdfImportedPage, 1.0F, 0, 0, 1.0F, 0, 0)
End If
Loop
oPdfDoc.Close()
End Sub
'---------------------------PDF Encryption----------------------------
Public Function GetDataSet(ByVal SQL As String, ByVal TableName As String) As DataSet
Try
Dim Cnn As New SqlClient.SqlConnection
Cnn.ConnectionString = GetConnectionString()
Cnn.Open()
Dim ds As New DataSet
Dim cmd As New SqlClient.SqlCommand
cmd.Connection = Cnn
cmd.CommandText = SQL
Dim adp As New SqlClient.SqlDataAdapter(cmd)
adp.Fill(ds, TableName)
adp.Dispose()
cmd.Dispose()
Cnn.Close()
Cnn.Dispose()
Return ds
Catch ex As Exception
Throw New ApplicationException(ex.Message)
Exit Function
End Try
End Function
Public Function GetConnectionString() As String
'GetConnectionString = "server=172.16.1.250;uid=sa;pwd=admin@123;database=sample;Pooling=False"
GetConnectionString = "data source=172.15.1.6;Initial catalog=DW_sample;persist security info=False;user id=connect;password=connect; pooling=false"
End Function
Public Function exportReport(ByVal param As String)
Dim userName As String = "connect"
Dim password As String = "abc@321"
Dim auth As String = "secEnterprise"
Dim baseURL As String = "http://xxx:6405/biprws/"
Dim LogonURI As String = baseURL & Convert.ToString("logon/long")
Dim Logonof As String = "http://xxx:6405/biprws/logoff"
Dim InfoStoreURI As String = baseURL & Convert.ToString("raylight/v1/documents/6558/parameters")
Dim InfoStoreURI2 As String = baseURL & Convert.ToString("raylight/v1/documents/6558")
'260515 is the document id
Dim rwsLogonToken As String
Try
Console.WriteLine("Update Parameter 1")
Dim myWebRequest As WebRequest = WebRequest.Create(LogonURI)
myWebRequest.ContentType = "application/xml"
myWebRequest.Method = "GET"
myWebRequest.Timeout = 60000
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
Dim sr As New StreamReader(myWebResponse.GetResponseStream())
Dim output As String = sr.ReadToEnd()
Dim doc As New XmlDocument()
doc.LoadXml(output)
Dim nodelist As XmlNodeList = doc.GetElementsByTagName("attr")
For Each node As XmlNode In nodelist
If node.Attributes("name").Value = "userName" Then
node.InnerText = userName
End If
If node.Attributes("name").Value = "password" Then
node.InnerText = password
End If
If node.Attributes("name").Value = "auth" Then
node.InnerText = auth
End If
Next
Console.WriteLine("Update Parameter 2")
Dim myWebRequest1 As WebRequest = WebRequest.Create(LogonURI)
myWebRequest1.ContentType = "application/XML"
myWebRequest1.Method = "POST"
Dim reqBodyBytes As Byte() = System.Text.Encoding.[Default].GetBytes(doc.OuterXml)
Dim reqStream As Stream = myWebRequest1.GetRequestStream()
reqStream.Write(reqBodyBytes, 0, reqBodyBytes.Length)
reqStream.Close()
Try
Dim myWebResponse1 As WebResponse = myWebRequest1.GetResponse()
rwsLogonToken = myWebResponse1.Headers("X-SAP-LogonToken").ToString()
myWebRequest1.Timeout = 60000
Dim pXML As New XmlDocument()
pXML.Load("D:\New folder\Amit Sharma\Temp\Export report\Export report\bin\Release\param.xml")
Dim dataByte As Byte() = System.Text.Encoding.[Default].GetBytes(pXML.OuterXml)
Dim POSTRequest As HttpWebRequest = DirectCast(WebRequest.Create(InfoStoreURI), HttpWebRequest)
Console.WriteLine(dataByte.ToString)
POSTRequest.Method = "PUT"
POSTRequest.ContentType = "application/xml"
POSTRequest.Headers.Add("X-SAP-LogonToken", rwsLogonToken)
Console.WriteLine(rwsLogonToken.ToString)
POSTRequest.Timeout = 600000
Try
Dim POSTstream As Stream = POSTRequest.GetRequestStream()
Console.WriteLine(POSTRequest.ToString)
POSTstream.Write(dataByte, 0, dataByte.Length)
POSTstream.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Dim POSTResponse As HttpWebResponse = DirectCast(POSTRequest.GetResponse(), HttpWebResponse)
Dim reader As New StreamReader(POSTResponse.GetResponseStream(), System.Text.Encoding.UTF8)
Console.WriteLine("Response from Server:")
Console.WriteLine(reader.ReadToEnd().ToString())
Dim myWebRequest2 As HttpWebRequest = HttpWebRequest.Create(InfoStoreURI2)
myWebRequest2.Accept = "application/PDF"
myWebRequest2.Headers.Add("X-SAP-LogonToken", rwsLogonToken)
myWebRequest2.Method = "GET"
Dim myWebResponse2 As WebResponse = myWebRequest2.GetResponse()
Dim stream As New FileStream("D:\New folder\Amit Sharma\Temp\Export report\Export report\bin\Release\" & param & ".pdf", FileMode.Create)
myWebResponse2.GetResponseStream().CopyTo(stream)
stream.Close()
EncryptPdf("D:\New folder\Amit Sharma\Temp\Export report\Export report\bin\Release\" & param & ".pdf", "D:\New folder\Amit Sharma\Temp\Export report\Export report\bin\Release\" & param & "_encrypt.pdf", "ram@123")
Console.WriteLine("Check Report Now")
' File.Delete("D:\Amit Sharma\Temp\Export report\Export report\bin\Release\" & param & ".pdf")
Catch generatedExceptionName As Exception
Console.WriteLine("Error in communicating with Server" & generatedExceptionName.Message)
End Try
Catch ex As Exception
End Try
Return 0
End Function
End Module
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 |