Content not rotated when printing in wpf - wpf

I am printing an XPS document using the System.Windows.Controls.PrintDialog. When I choose landscape orientation in the print dialog the resulting page is rotated to landscape but the actual content stays in portrait mode and is clipped.
This is the way I print. I also tried to use the AddJob method on PrintDialog.PrintQueue and the overloads on PrintQueue.CreateXpsDocumentWriter(...).Write(...) all with the same or worse result. And I tried to set DocumentPaginator.PageSize, the printDialog.PrintTicket.PageMediaSize and the width and height of th first FixedPage to the correct lanscape size with no result. PrintDialog.PrintTicket.PageOrientation is on landscape and PrintDialog.PrintableAreaWidth and PrintDialog.PrintableAreaHeight is as it should be when lanscape is selected after the PrintDialog was shown.
var printDialog = new PrintDialog
{
MaxPage = (uint)pageCount,
MinPage = 1,
PageRange = new PageRange(1, pageCount),
UserPageRangeEnabled = true
};
if (printDialog.ShowDialog() != true) return;
using (var doc = new XpsDocument(filename, FileAccess.Read))
{
var paginator = doc.GetFixedDocumentSequence().DocumentPaginator;
printDialog.PrintDocument(fds.paginator , "myPrintJob");
}

Related

WPF print - scale content to page visible area

I have to implement printing in a WPF application and I have to scale printable content to page visible area.
This is my code:
PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog().Value)
{
FixedDocument fixedDocument = new FixedDocument();
Canvas canvas = new Canvas();
PrintCapabilities pc = dialog.PrintQueue.GetPrintCapabilities();
System.Windows.Shapes.Rectangle rc = new System.Windows.Shapes.Rectangle();
rc.Width = pc.PageImageableArea.ExtentWidth;
rc.Height = pc.PageImageableArea.ExtentHeight;
rc.Fill = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 0, 0));
canvas.Children.Add(rc);
Canvas.SetLeft(rc, pc.PageImageableArea.OriginWidth);
Canvas.SetTop(rc, pc.PageImageableArea.OriginHeight);
FixedPage fixedPage = new FixedPage();
fixedPage.Width = dialog.PrintableAreaWidth;
fixedPage.Height = dialog.PrintableAreaHeight;
fixedPage.Children.Add(canvas);
PageContent pageContent = new PageContent();
((IAddChild)pageContent).AddChild(fixedPage);
fixedDocument.Pages.Add(pageContent);
dialog.PrintDocument(fixedDocument.DocumentPaginator, "Demo");
}
The red rectangle is set to fill the page printable area.
The problem is the PrintCapabilities.PageImageableArea always returns the same values no matter what paper (Letter, Legal, etc) is selected.
If I run the code and don't change anything in the print dialog (default paper size is Letter) I get the print below:
The red rectangle fills the entire Letter page.
If I run the code and select Legal paper size in the print dialog then I get the print below:
The PrintCapabilities.PageImageableArea property still returns a Letter sized area no matter I selected Legal paper in print dialog.
I tested the code above with "Microsoft Print to PDF" printer which does not have unprintable margins (this is why the red rectangle fills the entire page) but the problem persists also with a physical printer.
What am I doing wrong?

Unable to capture WPF livechart in bitmap

I'm trying to write a function that will write the results of a test (which have both text output and chart output, stored in a textbox and a livecharts chart respectively) to a pdf file. There are no issues with the text output, but I haven't been able to get the charts to save unless I extract each chart from its parent stackpanel (each test has a stackpanel that is shown when the test is selected) and then display the chart in a separate window. There is a lot of code around the problematic lines, so I'm going to paste a shortened version below.
The issue seems to be calling "_saveWindow.Show()" instead of ".ShowDialog". I need this to open and close without user input, but the "Show()" doesn't draw the chart in the window at all.
Any ideas why this is happening?
foreach (TestSequenceItem tsi in resultsToSave)
{
tsi.Instances[0].ChartStackPanel.Visibility = Visibility.Visible;
for (int i=0; i<tsi.Instances[0].StackChartList.Count; i++)
{
Chart _saveChart = tsi.Instances[0].StackChartList[i];
tsi.Instances[0].ChartStackPanel.Children.Remove(_saveChart);
ScrollViewer panel = new ScrollViewer { Content = _saveChart };
Window _saveWindow = new Window { Content = panel };
_saveWindow.Show();
var encoder = new PngBitmapEncoder();
RenderTargetBitmap bmp = new RenderTargetBitmap((int)tsiChartDoc.DefaultPageSetup.PageWidth, 600, 96, 96, PixelFormats.Pbgra32);
bmp.Render(_saveChart);
encoder.Frames.Add(BitmapFrame.Create(bmp));
using (MemoryStream stm = new MemoryStream())
{
encoder.Save(stm);
string fileName = "base64:" + Convert.ToBase64String(stm.ToArray());
// Adding a heading to the pdf
tsiChartDoc.LastSection.AddParagraph(tsi.Instances[0].StackTitleList[i].Text, "Heading2");
tsiChartDoc.LastSection.AddImage(fileName);
}
_saveWindow.Close();
panel.Content = null;
tsi.Instances[0].ChartStackPanel.Children.Insert(chartIdx, _saveChart);
}
}

WPF event within a frame stored on a stackpanel

I need to recreate a program similar to whatsapp that can send and receive messages, images videos and audio. I have created a WPF form to show messages that looks like this:
I have a stack panel that contains text bubbles on them. Text messages work fine but if I send an image I want the user to be able to click on the image text bubble and it must become full screen. The image text bubble consists of a label that has a frame in it and then within that frame the image is stored. The label was used since we could resize the label.
However, because the image is done like this dynamically, we cannot seem to register an on-click event on this image bubble. If you have any better ways that we can display the image or how to log this event it would be much appreciated. Here is the method used to add the image.
public void AddMessage_Image(string path, string displayName, int role, string date = "")
{
//Create an image from the path
ImageBrush image = new ImageBrush();
image.ImageSource = new BitmapImage(new Uri(path, UriKind.Absolute));
image.Stretch = Stretch.Uniform;
//Create a frame in which to place the image
Frame fr = new Frame();
fr.Background = image;
fr.MinHeight = 120;
fr.MinWidth = 160;
//Ensure scalabilty of the image
Viewbox vb = new Viewbox();
vb.Child = fr;
vb.Stretch = Stretch.Uniform;
//Place the image in a sizable container
Label lbl = new Label();
lbl.MinHeight = 10;
lbl.MinWidth = 10;
lbl.MaxHeight = 300;
lbl.MaxWidth = 400;
lbl.Content = vb;
if (role == (int) Role.Sender)
lbl.HorizontalAlignment = HorizontalAlignment.Right;
else
lbl.HorizontalAlignment = HorizontalAlignment.Left;
lbl.Background = Brushes.Black;
//Place the image in the chat
chatbox.Children.Add(lbl);
}

WPF multipage printing for FixedDocument (Visual C# 2010)

I have a question about multipage FixedPage. I have a Grid created programmatically and the Grid exceeds one A4 page. Now I want to print the Grid in several FixedPage with print margin. But on my way, I create the Grid repeatedly and offset the LeftTop point in the fixedPage Arrange function. I meet a problem that I cannot set print margin in fixedPage, because I set the print margin to the fixedPage and then the first page will have print margin and the next pages will be blank.
How do print multipage of FixedDocument for a large grid want to print?
PrintDialog pd = new System.Windows.Controls.PrintDialog();
if (pd.ShowDialog() == false)
{
return;
}
var pageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
var document = new FixedDocument();
document.DocumentPaginator.PageSize = pageSize;
for (int nPage = 0; nPage < MaxPage; nPage++)
{
Grid tempGrid = LoadControlMotherInit();
tempGrid.Width = GridWidth;
tempGrid.Height = GridActualHeight;
Point leftTop = new Point();
leftTop.X = 10;
leftTop.Y = -nPage * pageSize.Height;
// Create FixedPage
var fixedPage = new FixedPage();
fixedPage.Width = pageSize.Width;
fixedPage.Height = pageSize.Height;
fixedPage.Margin = new Thickness(0, 0, 0, 96);
fixedPage.Children.Add((UIElement)tempGrid);
fixedPage.Measure(pageSize);
fixedPage.Arrange(new Rect(leftTop, pageSize));
fixedPage.UpdateLayout();
// Add page to document
var pageContent = new PageContent();
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
document.Pages.Add(pageContent);
}
pd.PrintDocument(document.DocumentPaginator, "My Document");
From looking at your example,
PrintDialog.PrintDocument method takes in a DocumentPaginator, which could come from a multitude of source.
that being said, you can inherit from DocumentPaginator and take control of everything from PageSize, PageCount to the actual DocumentPage being returned.
Imagine your DocumentPage as a sliding window over your UIElement; but instead of sliding your DocumentPage, you slide your UIElement using its RenderTransform.

Dynamic printable content area

I am using WPF Flowdocument to print content in a Table with Header and Footer.
However, when the data occupies only couple of rows in the table, footer section still reflects at the last of the page and the whole page gets printed, leaving half of the page blank.
Can I expect the content (Header + Content + Footer) to occupy only half of the page if the data is less and one full page (A4/Letter page) if the data is more than half page? And if the data is more than a full page, it should span to the second page too.
Thanks..
I am able to solve this by overriding OnPrintCommand for DocumentViewer
public class CustomDocumentViewer : DocumentViewer
{
protected override void OnPrintCommand()
{
// get a print dialog, defaulted to default printer and default printer's preferences.
PrintDialog printDialog = new PrintDialog();
printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket;
// get a reference to the FixedDocumentSequence for the viewer.
FixedDocumentSequence docSeq = this.Document as FixedDocumentSequence;
if (Globals.PRINT_COMMAND_PAGEHEIGHT == "FULL")
printDialog.PrintTicket.PageMediaSize = new PageMediaSize(960, 1152);
else
printDialog.PrintTicket.PageMediaSize = new PageMediaSize(960, 576);
if (printDialog.ShowDialog() == true)
{
// set the print ticket for the document sequence and write it to the printer.
docSeq.PrintTicket = printDialog.PrintTicket;
XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);
writer.WriteAsync(docSeq, printDialog.PrintTicket);
}
}
}
Window_Activated function:
//Set Page Height and Width dynamically
MemoryStream mem = new MemoryStream();
byte[] buf = Encoding.UTF8.GetBytes(strXmlData);
mem.Write(buf, 0, buf.Length);
mem.Position = 0;
FlowDocument res = XamlReader.Load(mem) as FlowDocument;
//1152 = 12in * 96 DPI
if (Utils.GetPageHeight(strOrderNo.Substring(0, 2)) == "FULL")
{
res.PageHeight = Globals.FULL_PAGE_HEIGHT;
Globals.PRINT_COMMAND_PAGEHEIGHT = "FULL";
}
else
{
res.PageHeight = Globals.HALF_PAGE_HEIGHT;
Globals.PRINT_COMMAND_PAGEHEIGHT = "HALF";
}
res.PageWidth = Globals.PAGE_WIDTH;
reportDocument.XamlData = XamlWriter.Save(res);
in the DocumentViewer xaml file:
<Grid>
<spu:CustomDocumentViewer x:Name="documentViewer" />
</Grid>
and in the Flowdocument Template:
PageHeight="0.0cm" PageWidth="0.0cm" ColumnWidth="21.0cm"
Thanks,
~ Ravi

Resources