[WPF-IMPRESSION]Imprimer avec ou sans colonne un flowdocument
Bonjour à tous,
J'ai un petit, ou gros je sais pas encore, souci.
Je suis en vb.net WPF, et j'ai besoin d'imprimer plusieurs choses, j'ai réussi à imprimer un tableau provenant d'un historique d'alarme et d'autre du même style. Cela prend facilement la page A4 entière en largeur (portrait).
J'ai créé mon flowdocument avec dans l'ordre:
1)J'ai une image centré en haut.
2)Un titre centré, plus gros et en gras,
3)Des informations tel que la date, l'origine des alarmes etc.
4)Les données avec le tableau avec largeur de colonne défini ou non. Et avec une couleur de fond changeante suivant la gravité de l'alarme.
Après le PrintDialogue je définie la taille imprimable (max), ainsi que la colonne du FlowDocument sinon ma page était coupé en 2. Ce que j'aimerai du coup faire, mais que sur la partie des données pas sur mon image, titre et infos, je vous explique cela en dessous.
Mon souci c'est que maintenant j'ai besoin d'imprimé un document diffèrent avec des données assez courte pouvant facilement tenir en 2 colonnes sur une page A4, mais j'aimerai gardé l'image, le titre et les infos, sur la largeur complète.
Je pensais pouvoir le faire avec "Section", mais pour le moment je n'ai pas trouvé.
Je vous met le code qui fonctionne pour mes donnée en forme de tableau ci-dessous:
Code appelé:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
Public Function PrintHystoryFlow(ByVal Title As String, ByVal ColInfo As Collection, ByVal ColTitle As Collection, ByVal colWidth As Collection, ByVal colGen As ObservableCollection(Of MyHistory)) As Boolean
Try
Dim fd As New FlowDocument
fd = TableauFlow(Title, ColInfo, ColTitle, colWidth, colGen)
Dim pd As New PrintDialog
If Not pd.ShowDialog Then
Return False
End If
fd.PageHeight = pd.PrintableAreaHeight
fd.PageWidth = pd.PrintableAreaWidth
fd.ColumnWidth = pd.PrintableAreaWidth
Dim idocument As IDocumentPaginatorSource = TryCast(fd, IDocumentPaginatorSource)
pd.PrintDocument(idocument.DocumentPaginator, "Printing Flow Document...")
PrintHystoryFlow = True
Catch ex As Exception
PrintHystoryFlow = False
End Try
End Function |
Ici je passe donc en paramètre un titre, une collection contenant les infos, une collection contenant la 1ere ligne des colonnes du tableau, une collection contenant la largeur des colonnes du tableau, et ma collection contenant toutes les données, c'est mon Observable collection qui me sert à l'affichage, je sais pas si c'est bien propre de passer une observable collection, mais cela fonctionne.
Code de TableauFlow:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
Private Function TableauFlow(ByVal Title As String, ByVal colInfo As Collection, ColTitle As Collection, ByVal colWidth As Collection, ByVal coll As ObservableCollection(Of MyHistory)) As FlowDocument
Dim myFlowDocument As New FlowDocument()
Dim Sect As New Section
'Image
Dim Img As New Image()
Dim bimg As New BitmapImage()
bimg.BeginInit()
bimg.UriSource = New Uri(ApplicationStartupPath() & "\MonImage.png", UriKind.Absolute)
bimg.EndInit()
Img.Source = bimg
Img.Stretch = Stretch.None
Img.HorizontalAlignment = HorizontalAlignment.Center
Sect.Blocks.Add(New BlockUIContainer(Img))
'Title
Dim myParagraph As New Paragraph()
myParagraph.Inlines.Add(New Bold(New Run(Title)))
myParagraph.Margin = New Thickness(50)
myParagraph.TextAlignment = TextAlignment.Center
myParagraph.FontSize = 20
myParagraph.FontFamily = New FontFamily("Arial, Century Gothic")
Sect.Blocks.Add(myParagraph)
'Information
Dim myList As New List()
For Each txt As String In colInfo
Dim paragraphListItem1 As New Paragraph(New Run(txt))
paragraphListItem1.FontFamily = New FontFamily("Arial, Century Gothic")
myList.ListItems.Add(New ListItem(paragraphListItem1))
Next
Sect.Blocks.Add(myList)
'Tableau
Dim t As New Table()
'Définition de la largeur des colonnes
t.BorderBrush = Brushes.Black
t.BorderThickness = New Thickness(2)
For Each taille As String In colWidth
If taille = "-1" Then
t.Columns.Add(New TableColumn() With {.Width = GridLength.Auto})
Else
Dim glc As New GridLengthConverter
t.Columns.Add(New TableColumn() With {.Width = glc.ConvertFromString(taille)})
End If
Next
Dim trg As New TableRowGroup()
'Deffinition de la 1ere ligne du tableau (le titre des colonnes)
Dim currentRow As New TableRow()
currentRow.FontFamily = New FontFamily("Arial, Century Gothic")
currentRow.Background = Brushes.Black
currentRow.Foreground = Brushes.White
currentRow.FontWeight = FontWeights.Bold
For Each txt As String In ColTitle
currentRow.Cells.Add(New TableCell(New Paragraph(New Run(txt))))
Next
trg.Rows.Add(currentRow)
'Remplissage du tableau
For Each mCol As MyHistory In coll
currentRow = New TableRow()
currentRow.FontFamily = New FontFamily("Arial, Century Gothic")
currentRow.Foreground = Brushes.Black
currentRow.FontWeight = FontWeights.Normal
currentRow.Cells.Add(New TableCell(New Paragraph(New Run(mCol.mIdx))))
currentRow.Cells.Add(New TableCell(New Paragraph(New Run(mCol.mName1))))
currentRow.Cells.Add(New TableCell(New Paragraph(New Run(mCol.mName2))))
currentRow.Cells.Add(New TableCell(New Paragraph(New Run(mCol.mSeverity))))
Dim d As String = Strings.Format(mCol.mMyDate, "dd/MM/yyyy HH:mm:ss")
currentRow.Cells.Add(New TableCell(New Paragraph(New Run(d))))
currentRow.Cells.Add(New TableCell(New Paragraph(New Run(mCol.mType))))
Select Case mCol.mSeverityLevel
Case ALARM_LEVEL.vbUrgent
currentRow.Background = AppOptions.HistoAlarmUrgentColor
Case ALARM_LEVEL.vbNonUrgent
currentRow.Background = AppOptions.HistoAlarmNoUrgentColor
Case ALARM_LEVEL.vbInfo
currentRow.Background = AppOptions.HistoAlarmInfoColor
Case ALARM_LEVEL.vbEvent
currentRow.Background = AppOptions.HistoAlarmEventColor
Case Else
currentRow.Background = Brushes.White
End Select
trg.Rows.Add(currentRow)
Next
t.RowGroups.Add(trg)
Sect.Blocks.Add(t)
myFlowDocument.Blocks.Add(Sect)
Return myFlowDocument
End Function |
Ce code fonctionne comme je le souhaite, mais sur ce que je souhaite faire maintenant a la place du tableau je veux juste des lignes assez courte. mais j'aimerais qu'il puisse être sur 2 colonne afin de limité le nombre de page à imprimer.
Voila ce que j'ai fait pour le moment, cela fonctionne mais les lignes sont les une à la suite des autres. Et puis faut que je remplace la liste par un paragraphe classique car j'ai j'ai un point a chaque ligne, c'est inutile dans mon cas.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
Private Function PrintConfigFlow(ByVal Title As String, ByVal InfoCol As Collection, ByVal CfgCol As Collection) As FlowDocument
Dim myFlowDocument As New FlowDocument()
Dim Sect As New Section
Dim Img As New Image()
Dim bimg As New BitmapImage()
bimg.BeginInit()
bimg.UriSource = New Uri(ApplicationStartupPath() & "\MonImage.png", UriKind.Absolute)
bimg.EndInit()
Img.Source = bimg
Img.Stretch = Stretch.None
Img.HorizontalAlignment = HorizontalAlignment.Center
Sect.Blocks.Add(New BlockUIContainer(Img))
'Title
Dim myParagraph As New Paragraph()
' Add some Bold text to the paragraph
myParagraph.Inlines.Add(New Bold(New Run(Title)))
myParagraph.Margin = New Thickness(50)
myParagraph.TextAlignment = TextAlignment.Center
myParagraph.FontSize = 20
Sect.Blocks.Add(myParagraph)
'Information
'Information
Dim myList As New List()
For Each txt As String In InfoCol
Dim paragraphListItem1 As New Paragraph(New Run(txt))
paragraphListItem1.FontFamily = New FontFamily("Arial, Century Gothic")
myList.ListItems.Add(New ListItem(paragraphListItem1))
Next
Sect.Blocks.Add(myList)
'Sa c'est a changer car j'ai pas besoin d'un point en face chaque ligne
Dim myList2 As New List()
For Each txt As String In CfgCol
Dim paragraphListItem2 As New Paragraph(New Run(txt))
paragraphListItem2.FontFamily = New FontFamily("Arial, Century Gothic")
myList2.ListItems.Add(New ListItem(paragraphListItem2))
Next
Sect.Blocks.Add(myList2)
myFlowDocument.Blocks.Add(Sect)
Return myFlowDocument
End Function |
Merci de votre aide.