Error while saving the PDF File

Hi,

I'm using aspose.pdf 5.3.0 dll's to import a datatable to a PDF in memory stream and print it.

The problem i'm facing is that when I instantiate the pdf object by passing the stream object it is able to build the pdf but when i call pdf.close() it says it is not in direct-to-file mode so please use save() instead of close. So i followed as instructed and this time i used pdf.save(outstream) and it was throwing please use close() instead of save() as you are in direct-to-file mode. I have no clue why is it doing this.

I'm copying the function which i'm using to build and print.

Please take a look at this and let me know what is that i'm doing wrong here.

Public Function Print(ByVal dtRpt As DataTable, ByVal strHeader As String, ByRef strErrMsg As String) As Boolean

Dim blnRetVal As Boolean = True

Dim boolLandscape As Boolean = False

Dim objInStream As Stream = Nothing

Dim objOutStream As Stream = Nothing

Dim objPDFPage As Pdf = Nothing

Dim objViewer As Kit.PdfViewer = Nothing

Try

Try

' Instantiate the License class

Dim license As Aspose.Pdf.License = New Aspose.Pdf.License()

'Pass only the name of the license file embedded in the assembly

license.SetLicense(Directory.GetCurrentDirectory +

"\Aspose.Total.lic")

Catch ex As Exception

End Try

' instance

objPDFPage =

New Pdf(objInStream)

'Create a section to hold the header, footer and data from the datatable

Dim secData As Aspose.Pdf.Section = objPDFPage.Sections.Add()

secData.IsLandscape =

True


secData.PageInfo.PageWidth = Aspose.Pdf.PageSize.A4Width

secData.PageInfo.PageHeight = Aspose.Pdf.PageSize.A4Height

Dim margin As Aspose.Pdf.MarginInfo = New Aspose.Pdf.MarginInfo()

margin.Top = 0

margin.Left = 10

margin.Right = 10

margin.Bottom = 60

secData.PageInfo.Margin = margin

secData.PageRotatingAngle = 270

'Add the header to the pdf

Dim headerFooter1 As Aspose.Pdf.HeaderFooter = New Aspose.Pdf.HeaderFooter(secData)

secData.OddHeader = headerFooter1

secData.EvenHeader = headerFooter1

headerFooter1.IsFirstPageOnly =

True

Dim text As Aspose.Pdf.Text = New Text(headerFooter1, strHeader & " " & vbCrLf & _

"Date:" & Now.ToShortDateString)

headerFooter1.Paragraphs.Add(text)

headerFooter1.DistanceFromEdge =

"10"

'add the additional odd header


Dim header2 As Aspose.Pdf.HeaderFooter = New Aspose.Pdf.HeaderFooter(secData)


secData.AdditionalOddHeader = header2

header2.IsSubsequentPagesOnly =

True

text =

New Aspose.Pdf.Text(header2, strHeader & " " & vbCrLf & _

"Date:" & Now.ToShortDateString)

header2.Paragraphs.Add(text)

header2.DistanceFromEdge =

"10"

'add the additional even header


Dim header3 As Aspose.Pdf.HeaderFooter = New Aspose.Pdf.HeaderFooter(secData)


secData.AdditionalEvenHeader = header3

header3.IsSubsequentPagesOnly =

True

text =

New Aspose.Pdf.Text(header3, strHeader & " " & vbCrLf & _

"Date:" & Now.ToShortDateString)

header3.Paragraphs.Add(text)

header3.DistanceFromEdge =

"10"

'Add the datatable to the pdf


'Create a table object


Dim tab1 As Aspose.Pdf.Table = New Aspose.Pdf.Table()


'Add the Table object in the paragraphs collection of the section


secData.Paragraphs.Add(tab1)

Dim colWidth As String = ""

'set all the column widths

For k As Integer = 0 To dtRpt.Columns.Count - 1

If colWidth = "" Then

colWidth =

"35"

Else

colWidth +=

" " & "35"

End If

tab1.ColumnAdjustment = ColumnAdjustmentType.AutoFitToContentNext

tab1.ColumnWidths = colWidth

'Set default cell border of the table using BorderInfo object

tab1.DefaultCellBorder =

New BorderInfo(CType(BorderSide.All, Integer), 0.1F)

'Import data into the Table object from the DataTable created above

tab1.ImportDataTable(dtRpt,

True, 0, 0, dtRpt.Rows.Count, dtRpt.Columns.Count)

'set the column widths for all the columns by getting the maximum width of the data from the column.

For i As Integer = 0 To dtRpt.Columns.Count - 1

tab1.SetColumnWidth(i, tab1.GetMaxColumnWidth(objPDFPage, i))

Next

tab1.IsFirstRowRepeated =

True


'tab1.IsBroken = False


tab1.IsRowBroken =

False

'Add the footer to the page to show the number


Dim Footer1 As Aspose.Pdf.HeaderFooter = New Aspose.Pdf.HeaderFooter(secData)


secData.OddFooter = Footer1

secData.EvenFooter = Footer1

Dim text1 As Aspose.Pdf.Text = New Text("Page: $P ")

'add text to header section of the pdf file

Footer1.Paragraphs.Add(text1)

Footer1.DistanceFromEdge =

"3"

'set the page border of the section using borderinfo object


'secData.PageInfo.PageBorder = New BorderInfo(BorderSide.All, 0.2)


'close the pdf as we are saving the pdf in direct save mode


objPDFPage.Close()

' ''Save the PDF to the memory stream

'objPDFPage.Save(objInStream)

'Now print the report


objViewer =

New Kit.PdfViewer


objViewer.OpenPdfFile(objInStream)

objViewer.PrintDocumentWithSetup()

objViewer.ClosePdfFile()

Catch ex As Exception

strErrMsg = ex.ToString

blnRetVal =

False

Finally

objInStream =

Nothing

objPDFPage =

Nothing

End Try

Return blnRetVal

End Function

Hi Sita Ramachandra,


Thanks for using our products. I have tested the scenario while using Aspose.Pdf for .NET 6.4.2 and I am unable to notice any problem. I have used the same code snippet that you have shared and it seems to be correct. In direct-to-file mode, Stream object is passed as an argument to Pdf constructor and we need to call Pdf.Close() method instead of Pdf.Save(). Can you please try using the latest release version and in case you still face any issue, please feel free to contact. We apologize for your inconvenience.

Please visit the following link for instructions on

Thanks for the reply. I would try the latest dll's and would let you know how it goes.

Regards,

Ram

Hi,

I can't use the latest version dll's as my license file is old and it is asking me to update my license. The only thing i'm worried about is that i had developed another application which is using the same exact code to print data from datatables, i dont have any complains with that application. It is using pdf version 4.7.0. When i use the same code now with this new application i'm getting this error. First thing whenever i close the pdf it asks me to save it. When i try to save it directly to a stream or pdf it is going to a wait mode and my code never continues from there. Could you please let me know the fix for this?

Thanks,

Ram.

Hi Sita Ramachandra,


I am not entirely certain about the reasons due to which you are facing such problems. However, you may download the latest release version and comment out the license initialization code lines and see if the latest version resolves your problem or not. If the problem still persists, please share the complete project so that we can test the scenario at our end. We are sorry for this inconvenience.

Nayyer Thanks for the reply. I had downloaded the latest dlls and also got the new license file but still getting the same error. I had already shared the code which is printing the report.

Here is the code for the function which calls the print report function

Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click

Dim objStream As MemoryStream = Nothing

Try

lblMsg.Visible = False

'Print the report

If dtRpt.Rows.Count > 0 Then

'Print the report

PrintReport(dtRpt, objStream, "Report", strErrMsg)

End If

Catch ex As Exception

HandleError(ex.ToString)

End Try

End Sub

This sub calls the print report function which i had already sent here is the code once again

Public Function PrintReport(ByVal dtRpt As DataTable, ByVal objInstream As Stream, ByVal strHeader As String, ByRef strErrMsg As String) As Boolean

Dim blnRetVal As Boolean = True

Dim boolLandscape As Boolean = False

Dim objPDFPage As Pdf = Nothing

Dim objViewer As Facades.PdfViewer = Nothing

Try

Try

' Instantiate the License class

Dim license As Aspose.Pdf.License = New Aspose.Pdf.License()

'Pass only the name of the license file embedded in the assembly

license.SetLicense(Directory.GetCurrentDirectory + "\Aspose.Total.lic")

Catch ex As Exception

End Try

Try

'Instantiate a new pdf instance

objPDFPage = New Pdf(objInstream)

'Create a section to hold the header, footer and data from the datatable

Dim secData As Section = objPDFPage.Sections.Add()

secData.IsLandscape = True

secData.PageInfo.PageWidth = Aspose.Pdf.Generator.PageSize.A4Width

secData.PageInfo.PageHeight = Aspose.Pdf.Generator.PageSize.A4Height

Dim margin As MarginInfo = New MarginInfo()

margin.Top = 0

margin.Left = 10

margin.Right = 10

margin.Bottom = 60

secData.PageInfo.Margin = margin

secData.PageRotatingAngle = 270

'Add the header to the pdf

Dim headerFooter1 As HeaderFooter = New HeaderFooter(secData)

secData.OddHeader = headerFooter1

secData.EvenHeader = headerFooter1

headerFooter1.IsFirstPageOnly = True

Dim text As Text = New Text(headerFooter1, strHeader & " " & vbCrLf & _

"Date:" & Now.ToShortDateString)

headerFooter1.Paragraphs.Add(text)

headerFooter1.DistanceFromEdge = "10"

'add the additional odd header

Dim header2 As HeaderFooter = New HeaderFooter(secData)

secData.AdditionalOddHeader = header2

header2.IsSubsequentPagesOnly = True

text = New Text(header2, strHeader & " " & vbCrLf & _

"Date:" & Now.ToShortDateString)

header2.Paragraphs.Add(text)

header2.DistanceFromEdge = "10"

'add the additional even header

Dim header3 As HeaderFooter = New HeaderFooter(secData)

secData.AdditionalEvenHeader = header3

header3.IsSubsequentPagesOnly = True

text = New Text(header3, strHeader & " " & vbCrLf & _

"Date:" & Now.ToShortDateString)

header3.Paragraphs.Add(text)

header3.DistanceFromEdge = "10"

'Add the datatable to the pdf

'Create a table object

Dim tab1 As Table = New Table()

'Add the Table object in the paragraphs collection of the section

secData.Paragraphs.Add(tab1)

Dim colWidth As String = ""

'set all the column widths

For k As Integer = 0 To dtRpt.Columns.Count - 1

If colWidth = "" Then

colWidth = "35"

Else

colWidth += " " & "35"

End If

tab1.ColumnAdjustment = ColumnAdjustmentType.AutoFitToContent

'If colWidth = "" Then

' colWidth = Math.Ceiling(100 / objRegardsModule.PrintFormTable.Columns.Count).ToString & "%"

'Else

' colWidth += " " & Math.Ceiling(100 / objRegardsModule.PrintFormTable.Columns.Count).ToString & "%"

'End If

Next

tab1.ColumnWidths = colWidth

'Set default cell border of the table using BorderInfo object

tab1.DefaultCellBorder = New BorderInfo(CType(BorderSide.All, Integer), 0.1F)

'Import data into the Table object from the DataTable created above

tab1.ImportDataTable(dtRpt, True, 0, 0, dtRpt.Rows.Count, dtRpt.Columns.Count)

'set the column widths for all the columns by getting the maximum width of the data from the column.

For i As Integer = 0 To dtRpt.Columns.Count - 1

tab1.SetColumnWidth(i, tab1.GetMaxColumnWidth(objPDFPage, i))

Next

tab1.IsFirstRowRepeated = True

'tab1.IsBroken = False

tab1.IsRowBroken = False

'Add the footer to the page to show the number

Dim Footer1 As HeaderFooter = New HeaderFooter(secData)

secData.OddFooter = Footer1

secData.EvenFooter = Footer1

Dim text1 As Text = New Text("Page: $P ")

'add text to header section of the pdf file

Footer1.Paragraphs.Add(text1)

Footer1.DistanceFromEdge = "3"

'set the page border of the section using borderinfo object

'secData.PageInfo.PageBorder = New BorderInfo(BorderSide.All, 0.2)

'close the pdf as we are saving the pdf in direct save mode

objPDFPage.Close()

''Save the PDF to the memory stream

'objPDFPage.Save(objInstream)

'Now print the report

objViewer = New PdfViewer

objViewer.OpenPdfFile(objInstream)

objViewer.PrintDocumentWithSetup()

objViewer.ClosePdfFile()

Catch ex As Exception

strErrMsg = ex.ToString

blnRetVal = False

Finally

objInstream = Nothing

objPDFPage = Nothing

End Try

Return blnRetVal

End Function