Having trouble with PDF.Save(pdfStream)

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.

Btw, I’m using Aspose.Pdf version 1.5.23.0. I know it’s not the latest, but was there a Save(Stream) method issue that was fixed between 1.5 and 2.0?

I just upgraded to 2.0.4.0 and am still having the same problem. I don’t know, maybe I’m just doing something very stupid - which is quite a possibility. [*-)] So, I’m still looking for help. Thanks.

Dear pecos,

Thank you for considering Aspose.

System.IO.Stream is an abstract class. You can save the PDF document to a System.IO.MemoryStream object.

Dear Tommy, I have the same problem (I am using memorystream). As I noticed the problem is in the document format. If I am using text only - everything is fine, however if doc. includes a list of items (number list) it raises “index was out of range” error :frowning:

forever,

Ah, that did it! See… I knew it was something silly that I was doing.

In case anyone else is interested, I needed to save to a stream to load into a byte array and pass it from a web service behind the firewall to the web page that actually displays the PDF.

And all it took was this:

objPDF.Save(pdfStream)
pdfByteArray = pdfStream.GetBuffer()

Thanks for your help, forever.

Dear PolarCat,

Thank you for considering Aspose.

Can you please send me an example that can reproduce this error?