Hello everyone,
first i have to say sorry for my bad english and i hope my problem description is understandable.
In one of my VB.net projects i create daily reports for my company and these reports contain a very simple frontpage (just one picture, two lines of text under the picture and one line footer-text).
The following example demonstrates the simple layout:
Attachment:
frontpage.JPG [ 11.01 KiB | Viewed 20531 times ]
The most times (9/10) the frontpage ist generated fine, but at the 10th time the PDF-file becomes corrupt. Instead of one frontpage the file displays the frontpage as two empty pages with a height of 1px and Adobe reader fires a warning (page out of range). iphones totally refuse to open the document.
Attachment:
Defekt.JPG [ 15.59 KiB | Viewed 20531 times ]
The following pages are more complex (toc, tables with calculated pagebreaks and so on) but everything works fine here. If you scroll down in the "corrupt" file the following pages are displayed correctly.
The following code sample shows how i create the document.
Code:
Private Sub Generate()
Dim Started As DateTime =DateTime.Now
Try
_Logger.Info("started") 'Log4Net Logger
GetValues()
CreateDoc()
AddFrontPage() '<-- Here the Problem seems to occure
AddTOC()
AddTable1()
AddTable2()
AddTable3()
AddTable4()
AddTable5()
RenderAndSendMail()
_Logger.Info(_Rec.ShopGroupToProcess.GroupDescription & " - generated in " & DateTime.Now.Subtract(Started).TotalSeconds.ToString("#,##0") & " seconds")
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
Code:
Private Sub CreateDoc()
Try
_Doc = New Document
_Doc.Info.Title = "Report for " & _Rec.ShopGroupToProcess.GroupDescription & " " & _MaxDate.ToString("dd.MM.yyyy")
_Doc.Info.Author = "Automated report generator by..."
Catch ex As Exception
Throw New Exception(".CreateDoc - " & ex.Message)
End Try
End Sub
Code:
Private Sub AddFrontPage()
Try
Dim Sec As Section = _Doc.AddSection
Dim Setup As PageSetup = _Doc.DefaultPageSetup.Clone
With Setup
.TopMargin = Unit.FromCentimeter(3.5)
.LeftMargin = Unit.FromCentimeter(1)
.RightMargin = Unit.FromCentimeter(1)
.BottomMargin = Unit.FromCentimeter(1.5)
.Orientation = Orientation.Landscape
.PageFormat = PageFormat.A4
End With
Sec.PageSetup = Setup
Dim Para As Paragraph = Sec.AddParagraph()
Para.Format.Alignment = ParagraphAlignment.Center
Para.Format.SpaceBefore = Unit.FromCentimeter(3)
Para.AddImage("Images\CompanyLogo.png")
Para = Sec.AddParagraph("Report for " & _MaxDate.ToString("dd.MM.yyyy"))
Para.Format.Font.Size = Unit.FromPoint(15)
Para.Format.Font.Color = Colors.DarkGreen
Para.Format.Font.Name = "Arial Black"
Para.Format.Alignment = ParagraphAlignment.Center
Para = Sec.AddParagraph(_Rec.ShopGroupToProcess.GroupDescription)
Para.Format.Font.Size = Unit.FromPoint(15)
Para.Format.Font.Color = Colors.DarkGreen
Para.Format.Font.Name = "Arial Black"
Para.Format.Alignment = ParagraphAlignment.Center
Para = Sec.Footers.Primary.AddParagraph("Generiert am: " & DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"))
Para.Format.Alignment = ParagraphAlignment.Right
Para.Format.Font.Size = Unit.FromPoint(8)
Catch ex As Exception
_Logger.Error(_Rec.ShopGroupToProcess.GroupDescription & ".AddFrontPage - " & ex.Message)
Throw New Exception
End Try
End Sub
Code:
Private Sub RenderAndSendMail()
Try
Dim Rend As New MigraDoc.Rendering.PdfDocumentRenderer
Rend.Document = _Doc
Rend.RenderDocument()
'No difference if true or false
If _PDFInMemory Then
Using Stream As New MemoryStream
Rend.Save(Stream, False)
SendMail(Stream)
End Using
Else
If Not Directory.Exists(_SaveFolder) Then
Directory.CreateDirectory(_SaveFolder)
End If
Rend.Save(_SavePath)
SendMail()
End If
Catch ex As Exception
_Logger.Error(_Rec.ShopGroupToProcess.GroupDescription & ".RenderAndSendMail - " & ex.Message)
Throw New Exception
End Try
End Sub
Could somebody help me with the problem? I'm a bit confused.
Thanks.