Hi,
I’m trying to save the PDF to a stream, so that I can pass it in the form of a byte array (using VB.Net
).
My code looks like this:
Dim objPDF As Pdf
Dim filestream As StreamReader
Dim xmlDoc As New System.Xml.XmlDocument
Dim pdfStream As IO.Stream
Dim pdfByteArray As Byte()
Try
Dim str_CertTemplatePath As String = Trim(ConfigurationSettings.AppSettings(“str_CertTemplatePath”) & “”)
'Get PDF file
objPDF = New Pdf(Trim(ConfigurationSettings.AppSettings(“str_AsposeLicensePath”) & “”))
filestream = New StreamReader(str_CertTemplatePath)
Dim pdfString As String = filestream.ReadToEnd().ToString()
filestream.Close()
filestream = Nothing
xmlDoc.LoadXml(pdfString)
objPDF.BindXML(xmlDoc, Nothing)
objPDF.Save(pdfStream) ‘Exception is thrown on this line’
pdfStream.Read(pdfByteArray, 0, pdfStream.Length)
Return pdfByteArray
Catch objEx As Exception
lng_StatusCode = -1
str_StatusDesc = objEx.ToString()
Return pdfByteArray
End Try
The exception thrown looks like this:
?objEx
{System.NullReferenceException}
[System.NullReferenceException]: {System.NullReferenceException}
HelpLink: Nothing
InnerException: Nothing
Message: “Object reference not set to an instance of an object.”
Source: “Aspose.Pdf”
StackTrace: " at Aspose.Pdf.Pdf.Save(Stream stream)
at TermShareCert.view.RenderPDF(String str_MemberNum, String int_Share, Int64& lng_StatusCode, String& str_StatusDesc) in c:\inetpub\wwwroot\TermShareCert\view.aspx.vb:line 92"
TargetSite: {System.Reflection.RuntimeMethodInfo}
I know that my XML is valid, because I can save the pdf to a file, but I’m unable to save it to a stream. I tried looking at the documentation here: Document.Save | Aspose.Words for .NET, but when I click on the “public void Save(Stream);” link it takes me here: Document.Save | Aspose.Words for .NET - that being the reference for Pdf.Save Method (String, SaveType, HttpResponse), which is not what I expect to see there.
So basically there’s almost zero help on the Pdf.Save(Stream) method. I noticed that my pdfStream object is Nothing right before I try to call objPDF.Save(pdfStream), but I figured that the Save() method would return the instantiated stream object. But it throws an exception instead.
Any help would be greatly appreciated.
Thanks in advance.