Hi,
I was wondering if there is a possibility to print, save as pdf SfHeatMap.
Thank you for your help,
Nicola
Hi Nicola,
Requirement: Need to print or save SfHeatMap as a PDF file.
Currently, we do not have support to print or save our SfHeatMap control as a PDF file. We will try to achieve your requirements at the sample level. We will update you with more details on April 17, 2024.
Regards,
Karkuvel Rajan S
Hi Nicola,
Requirement: Need to print or save SfHeatMap as a PDF file.
We have prepared a simple sample to meet your requirements. In the sample, we used a visual brush and fixed document to create an XPS file and then converted that XPS file into a PDF document using an XPSToPDF converter. Please find the code snippet below and we have attached the sample for your reference.
Code Snippet:
|
// Create a FixedDocument System.Windows.Documents.FixedDocument fixedDocument = new System.Windows.Documents.FixedDocument();
// Add a FixedPage System.Windows.Documents.FixedPage fixedPage = new System.Windows.Documents.FixedPage(); fixedPage.Width = heatmapgrid.ActualWidth; fixedPage.Height = heatmapgrid.ActualHeight;
// Convert Button to a VisualBrush System.Windows.Media.VisualBrush visualBrush = new System.Windows.Media.VisualBrush(heatmapgrid);
// Draw the VisualBrush onto the FixedPage Rectangle rectangle = new Rectangle(); rectangle.Width = fixedPage.Width; rectangle.Height = fixedPage.Height; rectangle.Fill = visualBrush; fixedPage.Children.Add(rectangle);
// Add the FixedPage to the FixedDocument System.Windows.Documents.PageContent pageContent = new System.Windows.Documents.PageContent(); ((IAddChild)pageContent).AddChild(fixedPage); fixedDocument.Pages.Add(pageContent);
string tempXpsFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "temp.xps"); using (FileStream xpsStream = new FileStream(tempXpsFilePath, FileMode.Create)) {
Package package = Package.Open(xpsStream, FileMode.Create, FileAccess.ReadWrite); XpsDocument doc = new XpsDocument(package); XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc); System.Windows.Documents.FixedDocument fx = fixedDocument; writer.Write(fx); doc.Close(); package.Close(); }
var converter = new XPSToPdfConverter { };
var document = new PdfDocument();
document = converter.Convert(tempXpsFilePath); document.Save("pdfdoc.pdf"); document.Close(true);
File.Delete(tempXpsFilePath); // Cleanup
|
Regards,
Karkuvel Rajan S
Hi Karkuvel Rajan S,
your solution works, but there is a problem if the HeatMap is in a ScrollViewer. Your solution prints only the visible part of the HeatMap.
I have tried different approach, but got the same result (prints just visible part of the HeatMap). How can I print the whole HeatMap?
:
Grid layoutGrid;
ScrollViewer hmScrollViewer;
Syncfusion.UI.Xaml.HeatMap.ScrollViewer chart;
Border child = VisualTreeHelper.GetChild(sfheatMapUserResourcesShort, 0) as Border;
layoutGrid = VisualTreeHelper.GetChild(child, 0) as Grid;
hmScrollViewer = VisualTreeHelper.GetChild(layoutGrid, 0) as ScrollViewer;
chart = (ScrollViewer)hmScrollViewer.FindName("PART_ScrollViewer");
layoutGrid.Children.Remove(hmScrollViewer);
System.Windows.Controls.Canvas innerCanvas = new System.Windows.Controls.Canvas();
innerCanvas.Children.Add(hmScrollViewer);
innerCanvas.Arrange(new Rect());
innerCanvas.UpdateLayout();
hmScrollViewer.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
sfheatMapUserResourcesShort.Arrange(new Rect());
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(
(int)hmScrollViewer.ActualWidth, (int)hmScrollViewer.ActualHeight, 96, 96, PixelFormats.Default);
renderTargetBitmap.Render(hmScrollViewer);
PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
FileStream stream = new FileStream("grid.jpg", FileMode.OpenOrCreate, FileAccess.Write);
bitmapEncoder.Save(stream);
stream.Close();
innerCanvas.Children.Remove(hmScrollViewer);
layoutGrid.Children.Add(hmScrollViewer);
BitmapFrame frame1 = BitmapFrame.Create(new Uri("grid.jpg", UriKind.Relative), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
drawingContext.DrawImage(frame1, new Rect(0, 0, frame1.PixelWidth, frame1.PixelHeight));
}
RenderTargetBitmap bitmap = new RenderTargetBitmap(frame1.PixelWidth, frame1.PixelHeight, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(drawingVisual);
bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(bitmap));
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
SaveFileDialog saveFileDialog = new SaveFileDialog
{
FileName = "Untited",
DefaultExt = "PDF",
FilterIndex = 1
};
PdfDocument doc = new PdfDocument
{
PageSettings = new PdfPageSettings(new SizeF(frame1.PixelWidth, frame1.PixelHeight))
};
PdfPage page = doc.Pages.Add();
PdfGraphics graphics = page.Graphics;
if (saveFileDialog.ShowDialog() == true)
{
using (Stream saveStream = saveFileDialog.OpenFile())
{
encoder.Save(saveStream);
bitmapEncoder.Save(saveStream);
saveStream.Seek(0, SeekOrigin.Begin);
graphics.DrawImage(new PdfBitmap(saveStream), 0, 0);
saveStream.Close();
}
}
doc.Save(saveFileDialog.FileName);
doc.Close(true);
Process.Start(saveFileDialog.FileName);
Thank you for your help.
Nicola
Hi Nicola,
Reported Issue: Only the visible area gets printed in the pdf document once we render the SfHeatMap in a ScrollViewer.
We have prepared a simple sample with a large number of data in a heatmap and rendered that heatmap in a scroll viewer. However, we are unable to reproduce the reported issue. In the printed PDF document, we can see all the heatmap content which are not visible in the view. We have attached the same sample for your reference.
Please try to reproduce the reported issue in the attached sample or revert us with a modified one to reproduce the reported issue on our end.
Regards,
Karkuvel Rajan S
Hi Karkuvel Rajan,
I have checked your solution, and it does work. Placing SfHeatMap in a ScrollViewer does work. However if the SfHeatMap is not having enough of space and it does "activate" its own scrolling, printed page will not contain the whole HeatMap.
So the solution is placing SfHeatMap in a ScrollViewer, then the printing is working.
Kind regards,
Nic
Hi Nicola Dellea,
Thank you for confirming that the reported issue is resolved at your end. Please let us know if you need further assistance. As always, we are happy to help you out.
Regards,
Preethi R