This question is locked. New answers and comments are not allowed.
Hi,
I' m generating a PDF file with several images that export from chartview.
I need create a custom header and a footer,
In header in da right side I need put a Image logo
In footer in the left side need put one string, and da right side a number of pages (document) .
Already managed to create a file but have the following problems:
- The logo in the header is cut
- Always generates a page at the beginning with only the header and footer
- How can I insert the number of pages type (1 of 5)
I used this code:
Best Regards,
Marcelo Videira
I' m generating a PDF file with several images that export from chartview.
I need create a custom header and a footer,
In header in da right side I need put a Image logo
In footer in the left side need put one string, and da right side a number of pages (document) .
Already managed to create a file but have the following problems:
- The logo in the header is cut
- Always generates a page at the beginning with only the header and footer
- How can I insert the number of pages type (1 of 5)
I used this code:
#region Export Pdf private void btnSavePdf_Click(object sender, RoutedEventArgs e) { SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = "pdf"; dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", "pdf", "Adobe PDF Document"); dialog.FilterIndex = 1; if (dialog.ShowDialog() == true) { using (Stream stream = dialog.OpenFile()) { busyWindow = new BusyWindow(Translator.GetTranslation("lblPleaseWait") + "..."); busyWindow.Show(); RadDocument document = new RadDocument(); #region Headers&Footers RadDocument headerDoc = new RadDocument(); RadDocument footerDoc = new RadDocument(); #region Headers Telerik.Windows.Documents.Model.Section hSection = new Telerik.Windows.Documents.Model.Section(); headerDoc.Sections.Add(hSection); headerDoc.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(15,15,15,15); Telerik.Windows.Documents.Model.Paragraph hparagraph = new Telerik.Windows.Documents.Model.Paragraph(); hSection.Blocks.Add(hparagraph); ImageInline image = new ImageInline(new Uri("......png", UriKind.Relative)); hparagraph.Children.Add(image); hparagraph.FlowDirection = System.Windows.FlowDirection.RightToLeft; #endregion #region Footers Telerik.Windows.Documents.Model.Section fSection = new Telerik.Windows.Documents.Model.Section(); footerDoc.Sections.Add(fSection); footerDoc.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(15,15,15,15); Telerik.Windows.Documents.Model.Paragraph fparagraph = new Telerik.Windows.Documents.Model.Paragraph(); fSection.Blocks.Add(fparagraph); Telerik.Windows.Documents.Model.Span fspan = new Telerik.Windows.Documents.Model.Span(ApplicationDomain.CurrentUser.Name + " - " + DateTime.Now); fspan.FontSize = 12; fparagraph.Inlines.Add(fspan); #endregion Header header = new Header(); Footer footer = new Footer(); header.Body = headerDoc; footer.Body = footerDoc; #endregion Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section(); section.Headers.Default = header; section.Footers.Default = footer; document.Sections.Add(section); document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(10, 10, 10, 10); document.HasDifferentEvenAndOddHeadersFooters = false; CreateDocumentGrid(document, tblCompetitiveIndex as RadGridView); CreateDocumemntChart(document, chChart1 as RadCartesianChart); CreateDocumemntChart(document, chChart2 as RadCartesianChart); CreateDocumemntChart(document, chChart3 as RadCartesianChart); document.LayoutMode = DocumentLayoutMode.Paged; document.SectionDefaultPageOrientation = PageOrientation.Landscape; document.Measure(RadDocument.MAX_DOCUMENT_SIZE); document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize)); PdfFormatProvider provider = new PdfFormatProvider(); provider.Export(document, stream); btnExport.IsOpen = false; Dispatcher.BeginInvoke(() => busyWindow.Close()); MessageBoxPopUp messageBox = new MessageBoxPopUp(); messageBox.Show("lblInfo", "lblFileExportedSuccessfully", Buttons.Ok, 200, 130, 90); } } } private void CreateDocumentGrid(RadDocument document, RadGridView grid) { List<GridViewBoundColumnBase> columns = (from c in grid.Columns.OfType<GridViewBoundColumnBase>() orderby c.DisplayIndex select c).ToList(); Telerik.Windows.Documents.Model.Table table = new Telerik.Windows.Documents.Model.Table(); Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section(); Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph(); if (grid.ShowColumnHeaders) { Telerik.Windows.Documents.Model.TableRow headerRow = new Telerik.Windows.Documents.Model.TableRow(); if (grid.GroupDescriptors.Count > 0) { Telerik.Windows.Documents.Model.TableCell indentCell = new Telerik.Windows.Documents.Model.TableCell(); indentCell.PreferredWidth = new TableWidthUnit(grid.GroupDescriptors.Count * 20); indentCell.Background = Color.FromArgb(255, 127, 127, 127); headerRow.Cells.Add(indentCell); } for (int i = 0; i < columns.Count; i++) { Telerik.Windows.Documents.Model.TableCell cell = new Telerik.Windows.Documents.Model.TableCell(); cell.Background = Color.FromArgb(255, 127, 127, 127); //AddCellValue(cell, columns[i].UniqueName); AddCellValue(cell, ((Telerik.Windows.Controls.GridViewDataColumn)columns[i]).Header.ToString()); cell.PreferredWidth = new TableWidthUnit((float)columns[i].ActualWidth); headerRow.Cells.Add(cell); } table.Rows.Add(headerRow); } if (grid.Items.Groups != null) { for (int i = 0; i < grid.Items.Groups.Count; i++) { AddGroupRow(table, grid.Items.Groups[i] as QueryableCollectionViewGroup, columns, grid); } } else { AddDataRows(table, grid.Items, columns, grid); } section.Blocks.Add(table); section.Blocks.Add(paragraph); document.Sections.Add(section); } private void CreateDocumemntChart(RadDocument document, RadCartesianChart chart) { Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section(); Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph(); using (MemoryStream ms = new MemoryStream()) { Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(chart, ms, new PngBitmapEncoder()); double imageWidth = chart.ActualWidth; double imageHeight = chart.ActualHeight; if (imageWidth > 625) { imageWidth = 625; imageHeight = chart.ActualHeight * imageWidth / chart.ActualWidth; } ImageInline image = new ImageInline(ms, new Size(imageWidth, imageHeight), "png"); paragraph.Inlines.Add(image); section.Blocks.Add(paragraph); document.Sections.Add(section); } } private void AddDataRows(Telerik.Windows.Documents.Model.Table table, IList items, IList<GridViewBoundColumnBase> columns, RadGridView grid) { for (int i = 0; i < items.Count; i++) { Telerik.Windows.Documents.Model.TableRow row = new Telerik.Windows.Documents.Model.TableRow(); if (grid.GroupDescriptors.Count > 0) { Telerik.Windows.Documents.Model.TableCell indentCell = new Telerik.Windows.Documents.Model.TableCell(); indentCell.PreferredWidth = new TableWidthUnit(grid.GroupDescriptors.Count * 20); indentCell.Background = Color.FromArgb(255, 251, 247, 255); row.Cells.Add(indentCell); } for (int j = 0; j < columns.Count; j++) { Telerik.Windows.Documents.Model.TableCell cell = new Telerik.Windows.Documents.Model.TableCell(); object value = columns[j].GetValueForItem(items[i]); AddCellValue(cell, value != null ? value.ToString() : string.Empty); cell.PreferredWidth = new TableWidthUnit((float)columns[j].ActualWidth); cell.Background = Color.FromArgb(255, 251, 247, 255); ; row.Cells.Add(cell); } table.Rows.Add(row); } } private void AddGroupRow(Telerik.Windows.Documents.Model.Table table, QueryableCollectionViewGroup group, IList<GridViewBoundColumnBase> columns, RadGridView grid) { Telerik.Windows.Documents.Model.TableRow row = new Telerik.Windows.Documents.Model.TableRow(); int level = GetGroupLevel(group); if (level > 0) { Telerik.Windows.Documents.Model.TableCell cell = new Telerik.Windows.Documents.Model.TableCell(); cell.PreferredWidth = new TableWidthUnit(level * 20); cell.Background = Color.FromArgb(255, 216, 216, 216); row.Cells.Add(cell); } Telerik.Windows.Documents.Model.TableCell aggregatesCell = new Telerik.Windows.Documents.Model.TableCell(); aggregatesCell.Background = Color.FromArgb(255, 216, 216, 216); aggregatesCell.ColumnSpan = columns.Count + (grid.GroupDescriptors.Count > 0 ? 1 : 0) - (level > 0 ? 1 : 0); AddCellValue(aggregatesCell, group.Key != null ? group.Key.ToString() : string.Empty); foreach (AggregateResult result in group.AggregateResults) { AddCellValue(aggregatesCell, result.FormattedValue != null ? result.FormattedValue.ToString() : string.Empty); } row.Cells.Add(aggregatesCell); table.Rows.Add(row); if (group.HasSubgroups) { foreach (var g in group.Subgroups) { AddGroupRow(table, g as QueryableCollectionViewGroup, columns, grid); } } else { AddDataRows(table, group.Items, columns, grid); } } private void AddCellValue(Telerik.Windows.Documents.Model.TableCell cell, string value) { Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph(); cell.Blocks.Add(paragraph); Telerik.Windows.Documents.Model.Span span = new Telerik.Windows.Documents.Model.Span(); if (value == null || value == "") { span.Text = " "; } else { span.Text = value; } paragraph.Inlines.Add(span); } private int GetGroupLevel(IGroup group) { int level = 0; IGroup parent = group.ParentGroup; while (parent != null) { level++; parent = parent.ParentGroup; } return level; } #endregionBest Regards,
Marcelo Videira