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