PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Problem with stream in MigraDoc
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1897
Page 1 of 1

Author:  dawnprojekt [ Fri Feb 03, 2012 2:56 pm ]
Post subject:  Problem with stream in MigraDoc

I ve got a problem with a Migradoc Pdf Document rendered in a stream.
In IE 9 adobe reader send a message that my pdf has got error and cannot be opened
but in the adobe application i can open it without any problem...
Do you have any idea of what could be the problem.

(Sorry for my poor english)

here is my generation Code :
Code:
   Dim idBL As Integer
    If Integer.TryParse(Request.QueryString("IdBL"), idBL) Then
      '1- on charge les infos du BL
      Dim oDtBL As DataTable = AccesBD.loadBL(idBL)
      If Not oDtBL Is Nothing Then
        If Not oDtBL.Rows.Count = 0 Then
          Dim row As DataRow = oDtBL.Rows(0)
          Dim idSociete As Integer = CInt(row.Item(1))
          Dim sClient As String = AccesBD.getNomSociete(idSociete)
          Dim idDestinataire As Integer = CInt(row.Item(2))
          Dim bDestCRM As Boolean = CBool(row.Item(3))
          Dim sNoSuivi As String = CStr(row.Item(4))
          Dim sTypeSuivi As String = CStr(row.Item(5))
          Dim sDateBL As String = CStr(CDate(row.Item(6)).ToShortDateString)
          Dim sCommentaire As String = CStr(row.Item(7))
          Dim sContactOrnicar As String = CStr(row.Item(9))
          '2 - on récupére les infos du destinataire
          Dim oDtDestinataire As DataTable = AccesBD.loadDestinataireBL(idDestinataire, bDestCRM)
          If Not oDtDestinataire Is Nothing Then
            If Not oDtDestinataire.Rows.Count = 0 Then
              row = oDtDestinataire.Rows(0)
              Dim sPrenom As String = CStr(row.Item(0))
              Dim sNom As String = CStr(row.Item(1))
              Dim sTitreDest As String = CStr(Items(2))
              Dim sTelephone As String = CStr(row.Item(3))
              Dim sAdresse As String = CStr(row.Item(4))
              Dim sCP As String = CStr(row.Item(5))
              Dim sVille As String = CStr(row.Item(6))
              Dim oDtLignes = AccesBD.getLignesBl(idBL)
              If Not oDtLignes Is Nothing Then
                If Not oDtLignes.Rows.Count = 0 Then
                  '3 - on peut commmencer a formatter le PDF
                  Dim doc As Document = New Document()
                  ' info du document
                  doc.Info.Title = "Bon de Livraison n°" & idBL & " du" & sDateBL & " Client : " & sClient
                  doc.Info.Author = "Ornicar"
                  'marges du document
                  doc.DefaultPageSetup.LeftMargin = 40
                  doc.DefaultPageSetup.RightMargin = 40
                  doc.DefaultPageSetup.BottomMargin = 15
                  doc.DefaultPageSetup.TopMargin = 30
                  Dim styl As Style = doc.Styles("Normal")
                  styl.Font.Name = "Arial"
                  styl.Font.Size = 10
                  Dim style_logo As Style = doc.AddStyle("logo", "Normal")
                  style_logo.Font.Size = 5
                  style_logo.Font.Name = "Arial"
                  style_logo.Font.Color = Black
                  'partie logo et date
                  Dim main_Section As Section = doc.AddSection()
                  Dim tab_Logo As Table = main_Section.AddTable
                  '  tab_Logo.Borders.Color = Colors.Violet
                  Dim col_tabl_Logo_Left As Column = tab_Logo.AddColumn
                  With col_tabl_Logo_Left
                    .Width = 150
                  End With
                  Dim col_tab_Logo_Empty As Column = tab_Logo.AddColumn
                  With col_tab_Logo_Empty
                    .Width = 200
                  End With
                  Dim col_tabl_Logo_Right As Column = tab_Logo.AddColumn
                  With col_tabl_Logo_Right
                    .Width = 150
                  End With
                  Dim tab_Logo_Row As Row = tab_Logo.AddRow
                  With tab_Logo_Row
                    .Height = 75
                  End With
                  Dim para As Paragraph = tab_Logo_Row.Cells(0).AddParagraph
                  tab_Logo_Row.Cells(0).Borders.Color = Colors.Black
                  With para
                    .AddImage(Parametres.CHEMINIMAGE & "Logo_Ornicar_n&b_120px.jpg")
                    .Style = "logo"
                    .AddLineBreak()
                    .AddText("ORNICAR SAS au capital de 219600 €")
                    .AddLineBreak()
                    .AddText("441 433 075 RCS GRENOBLE")
                    .AddLineBreak()
                    .AddText("450 avenue de l'Europe 38330 MONTBONNOT")
                    .Format.Alignment = ParagraphAlignment.Center
                  End With
                  Dim paraDate As Paragraph = tab_Logo_Row.Cells(2).AddParagraph
                  With paraDate
                    .AddText("Bon de Livraison N° : " & idBL)
                    .AddLineBreak()
                    .AddText("Date : " & sDateBL)
                    .AddLineBreak()
                    .Format.Alignment = ParagraphAlignment.Left
                  End With
                  ' partie adresse
                  Dim tab_adresse As Table = main_Section.AddTable
                  Dim col_adr1 As Column = tab_adresse.AddColumn
                  With col_adr1
                    .Width = 170
                  End With
                  Dim col_adr2 As Column = tab_adresse.AddColumn
                  With col_adr2
                    .Width = 250
                    .Format.SpaceAfter = 50
                  End With
                  Dim tab_adresse_row As Row = tab_adresse.AddRow
                  Dim par_adr1 As Paragraph = tab_adresse_row.Cells(0).AddParagraph
                  With par_adr1
                    .AddText("Contact : " & sContactOrnicar)
                    .AddLineBreak()
                    .AddText("Tel : 04 76 04 16 07")
                    .AddLineBreak()
                    .AddText("Client : " & sClient)
                  End With
                  Dim par_adr2 As Paragraph = tab_adresse_row.Cells(1).AddParagraph
                  With par_adr2
                    .AddText("Adresse de Livraison :")
                    .AddLineBreak()
                    If bDestCRM Then
                      .AddText(sTitreDest)
                      .AddLineBreak()
                      .AddText("A l'attention de " & sPrenom & sNom)
                    Else
                      .AddText(sNom)
                    End If
                    .AddLineBreak()
                    .AddText(sAdresse)
                    .AddLineBreak()
                    .AddText(sCP & " " & sVille)
                    .AddLineBreak()
                    .AddText("Tel du contact : " & sTelephone)
                    .Format.Font.Size = 13
                  End With
                  'Dim style_header As Style = doc.AddStyle("header", "Normal")
                  Dim paraTitreBL As Paragraph = main_Section.AddParagraph
                  With paraTitreBL
                    .Format.Shading.Color = Colors.Black
                    .Format.Font.Color = Colors.White
                    .Format.Alignment = ParagraphAlignment.Center
                    .AddText("BON DE LIVRAISON")
                    .Format.SpaceAfter = 25
                  End With
                  Dim paraTransporteur As Paragraph = main_Section.AddParagraph
                  With paraTransporteur
                    .AddText(sTypeSuivi & " : " & sNoSuivi)
                    .Format.Alignment = ParagraphAlignment.Center
                    .Format.Font.Color = Colors.Red
                    .Format.SpaceAfter = 25
                  End With
                  'table des Lignes de BL
                  Dim tabDetail As Table = main_Section.AddTable
                  Dim col_det1 As Column = tabDetail.AddColumn
                  With col_det1
                    .Width = 200
                  End With
                  Dim col_det2 As Column = tabDetail.AddColumn
                  With col_det2
                    .Width = 200
                  End With
                  Dim col_det3 As Column = tabDetail.AddColumn
                  With col_det3
                    .Width = 50
                  End With
                  Dim col_det4 As Column = tabDetail.AddColumn
                  With col_det4
                    .Width = 50
                  End With
                  Dim tabDetail_Header As Row = tabDetail.AddRow
                  With tabDetail_Header
                    .Format.Shading.Color = Colors.Black
                    .Format.Font.Color = Colors.White
                  End With
                  tabDetail_Header.Cells(0).AddParagraph("DESIGNATION")
                  tabDetail_Header.Cells(1).AddParagraph("REF.")
                  tabDetail_Header.Cells(2).AddParagraph("QTE")
                  tabDetail_Header.Cells(3).AddParagraph("RàL")
                  ' on rajoute les lignes
                  Dim iTotal As Integer = 0
                  Dim iTotalRal As Integer = 0
                  Dim _color As MigraDoc.DocumentObjectModel.Color = Colors.WhiteSmoke
                  For Each oRow As DataRow In oDtLignes.Rows
                    If _color = Colors.WhiteSmoke Then
                      _color = Colors.White
                    Else
                      _color = Colors.WhiteSmoke
                    End If
                    Dim sDesignation As String = CStr(oRow.Item(0))
                    Dim sRef As New String("")
                    If Not IsDBNull(oRow.Item(1)) Then sRef = CStr(oRow.Item(1))
                    Dim iQte As Integer = CInt(oRow.Item(2))
                    Dim iRal As Integer = CInt(oRow.Item(3))
                    Dim sCom As New String("")
                    If Not IsDBNull(oRow.Item(4)) Then sCom = CStr(oRow.Item(4))

                    Dim myrow As Row = tabDetail.AddRow
                    With myrow
                      .Shading.Color = _color
                    End With
                    myrow.Cells(0).AddParagraph(sDesignation)
                    myrow.Cells(1).AddParagraph(sRef)
                    With myrow.Cells(2)
                      .AddParagraph(iQte)
                      .Format.Alignment = ParagraphAlignment.Center
                    End With
                    With myrow.Cells(3)
                      .AddParagraph(iRal)
                      .Format.Alignment = ParagraphAlignment.Center
                    End With
                    iTotal += iQte
                    iTotalRal += iRal
                    If Not String.IsNullOrEmpty(sCom) Then
                      Dim rowCom As Row = tabDetail.AddRow
                      With rowCom
                        .Shading.Color = _color
                      End With
                      rowCom.Cells(0).AddParagraph(sCom)
                      rowCom.Cells(0).MergeRight = 3
                    End If
                  Next



                  Dim rowTotal As Row = tabDetail.AddRow
                  With rowTotal
                    .Format.Alignment = ParagraphAlignment.Right
                    .Shading.Color = Colors.Black
                    .Format.Font.Color = Colors.White
                  End With
                  With rowTotal.Cells(1)
                    .AddParagraph("Total :")
                  End With
                  rowTotal.Cells(2).AddParagraph(iTotal)
                  rowTotal.Cells(2).Format.Alignment = ParagraphAlignment.Center
                  rowTotal.Cells(3).AddParagraph(iTotalRal)
                  rowTotal.Cells(3).Format.Alignment = ParagraphAlignment.Center
                  'Commentaire éventuel
                  Dim parCom As Paragraph = main_Section.AddParagraph
                  With parCom
                    .Format.Alignment = ParagraphAlignment.Center
                    .AddText(sCommentaire)
                    .Format.SpaceAfter = 10
                  End With
                  Dim tableCachet As Table = main_Section.AddTable
                  Dim col_tableCachet As Column = tableCachet.AddColumn
                  With col_tableCachet
                    .Width = 170
                  End With
                  Dim row_tableCachet As Row = tableCachet.AddRow
                  With row_tableCachet
                    .Height = 100
                    With .Cells(0)
                      .Borders.Color = Colors.Black
                      .Format.Alignment = ParagraphAlignment.Left
                      Dim parCachet As Paragraph = .AddParagraph
                      With parCachet
                        .AddText("Reçu le :")
                        .AddLineBreak()
                        .AddText("Nom et cachet :")
                      End With
                    End With
                  End With
                  Dim paraArenvoyer As Paragraph = main_Section.AddParagraph
                  With paraArenvoyer
                    .Format.Font.Size = 10
                    .AddText("A renvoyer par fax au 04.76.04.16.00")
                  End With
                  'Footer
                  Dim paFooter As Paragraph = main_Section.Footers.Primary.AddParagraph
                  With paFooter
                    .AddText("MERCI DE PROCEDER A UNE VERIFICATION D'USAGE DES LA RECEPTION.")
                    .AddLineBreak()
                    .AddText("NOUS NOUS RESERVONS LA PROPRIETE DES MARCHANDISES, MEMES LIVREES, JUSQU'À LEUR PAIEMENT INTEGRAL.")
                    .AddLineBreak()
                    .AddLineBreak()
                    .AddText("ORNICAR 450, avenue de l'Europe - ZIRST - 38330 Montbonnot-St-Martin - Tél. 04 76 04 16 07 - Fax 04 76 04 16 00 - www.ornicar.net - contact@ornicar.net")
                    .Format.Alignment = ParagraphAlignment.Center
                    .Format.Font.Size = 8
                  End With
                  'XX Génération du pdf
                  Dim docRenderer As New PdfDocumentRenderer(False, PdfFontEmbedding.Default)
                  docRenderer.Document = doc
                  docRenderer.RenderDocument()
                  'on va regarder si un repertoire pour le client existe
                  If Not Directory.Exists(Parametres.CHEMINBL & sClient) Then
                    Directory.CreateDirectory(Parametres.CHEMINBL & sClient)
                  End If
                  docRenderer.Save(Parametres.CHEMINBL & sClient & "/" & sDateBL.Replace("/", "") & idBL & ".pdf")

                  Dim stream As MemoryStream = New MemoryStream
                  docRenderer.Save(stream, False)
                  stream.Position = 0
                  Response.Clear()
                  Response.ContentType = "application/pdf"
                  Response.AddHeader("content_length", stream.Length.ToString())
                  Response.BinaryWrite(stream.ToArray())
                  Response.Flush()
                  stream.Close()
                  Response.End()

Author:  () => true [ Fri Feb 03, 2012 6:34 pm ]
Post subject:  Re: Problem with stream in MigraDoc

Hi!
You call "docRenderer.Save" twice, thus wasting a lot of CPU time.
IIRC there is a bug with calling Save twice. If you change your code so that Save is called only once, it should work - and much faster, too.
If you need a local copy of the PDF, just write stream.ToArray to a file.

Author:  dawnprojekt [ Sun Feb 05, 2012 10:18 am ]
Post subject:  Re: Problem with stream in MigraDoc

okay,
thank y a lot ou for your reply
:D
i ll save my file to a a stream and then write a file and send the response.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/