I want to print two pages per sheet .Because I have one GridView and only three columns are available in the GridView. So remaining part of the page are unuseful.
so that I set PrintDialog.PrintTicket.PagesPerSheet = 2 and PrintDialog.PrintQueue.DefaultPrintTicket.PagesPerSheet = 2. But its not working. And I am using following code for the Printing.
public static void Print(this GridViewDataControl source, bool showDialog, bool p_IsLandscape = true)
{
try
{
PrintDialog _PrintDialog = new PrintDialog();
_PrintDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
_PrintDialog.PrintTicket = _PrintDialog.PrintQueue.DefaultPrintTicket;
if (p_IsLandscape)
_PrintDialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
else
{
_PrintDialog.PrintTicket.PageOrientation = PageOrientation.Portrait;
_PrintDialog.PrintTicket.PagesPerSheet = 2;
_PrintDialog.PrintQueue.DefaultPrintTicket.PagesPerSheet = 2;
}
bool? dialogResult = showDialog ? _PrintDialog.ShowDialog() : true;
if (dialogResult == true)
{
DocumentViewer viewer = new DocumentViewer();
viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), _PrintDialog);
_PrintDialog.PrintDocument(viewer.Document.DocumentPaginator, null);
}
}
catch
{
throw _Exception;
}
}
static FixedDocument ToFixedDocument(FrameworkElement element, PrintDialog dialog)
{
PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
FixedDocument fixedDocument = new FixedDocument();
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Size pageSize = new Size(element.DesiredSize.Width, dialog.PrintableAreaHeight);
Size extentSize = new Size(element.DesiredSize.Width, capabilities.PageImageableArea.ExtentHeight);
double totalHeight = element.DesiredSize.Height;
double totalWidth = element.DesiredSize.Width;
double yOffset = 0d;
if (totalWidth < dialog.PrintableAreaWidth)
{
Size _Size = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
element.Arrange(new Rect(new Point(0, 0), _Size));
}
else
{
element.Arrange(new Rect(new Point(0, 0), element.DesiredSize));
}
while (yOffset < totalHeight)
{
VisualBrush brush = new VisualBrush(element);
brush.Stretch = Stretch.None;
brush.AlignmentX = AlignmentX.Left;
brush.AlignmentY = AlignmentY.Top;
brush.ViewboxUnits = BrushMappingMode.Absolute;
brush.TileMode = TileMode.None;
if (totalWidth < dialog.PrintableAreaWidth)
brush.Viewbox = new Rect(0, yOffset, dialog.PrintableAreaWidth, extentSize.Height);
else
brush.Viewbox = new Rect(0, yOffset, extentSize.Width, extentSize.Height);
PageContent pageContent = new PageContent();
FixedPage page = new FixedPage();
((IAddChild)pageContent).AddChild(page);
fixedDocument.Pages.Add(pageContent);
if (totalWidth < dialog.PrintableAreaWidth)
page.Width = dialog.PrintableAreaWidth;
else
page.Width = totalWidth;
page.Height = pageSize.Height;
Canvas canvas = new Canvas();
FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
if (totalWidth < dialog.PrintableAreaWidth)
canvas.Width = dialog.PrintableAreaWidth;
else
canvas.Width = extentSize.Width;
canvas.Height = extentSize.Height;
canvas.Background = brush;
page.Children.Add(canvas);
yOffset += extentSize.Height;
}
return fixedDocument;
}
Can you help
Thanks
so that I set PrintDialog.PrintTicket.PagesPerSheet = 2 and PrintDialog.PrintQueue.DefaultPrintTicket.PagesPerSheet = 2. But its not working. And I am using following code for the Printing.
public static void Print(this GridViewDataControl source, bool showDialog, bool p_IsLandscape = true)
{
try
{
PrintDialog _PrintDialog = new PrintDialog();
_PrintDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
_PrintDialog.PrintTicket = _PrintDialog.PrintQueue.DefaultPrintTicket;
if (p_IsLandscape)
_PrintDialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
else
{
_PrintDialog.PrintTicket.PageOrientation = PageOrientation.Portrait;
_PrintDialog.PrintTicket.PagesPerSheet = 2;
_PrintDialog.PrintQueue.DefaultPrintTicket.PagesPerSheet = 2;
}
bool? dialogResult = showDialog ? _PrintDialog.ShowDialog() : true;
if (dialogResult == true)
{
DocumentViewer viewer = new DocumentViewer();
viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), _PrintDialog);
_PrintDialog.PrintDocument(viewer.Document.DocumentPaginator, null);
}
}
catch
{
throw _Exception;
}
}
static FixedDocument ToFixedDocument(FrameworkElement element, PrintDialog dialog)
{
PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
FixedDocument fixedDocument = new FixedDocument();
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Size pageSize = new Size(element.DesiredSize.Width, dialog.PrintableAreaHeight);
Size extentSize = new Size(element.DesiredSize.Width, capabilities.PageImageableArea.ExtentHeight);
double totalHeight = element.DesiredSize.Height;
double totalWidth = element.DesiredSize.Width;
double yOffset = 0d;
if (totalWidth < dialog.PrintableAreaWidth)
{
Size _Size = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
element.Arrange(new Rect(new Point(0, 0), _Size));
}
else
{
element.Arrange(new Rect(new Point(0, 0), element.DesiredSize));
}
while (yOffset < totalHeight)
{
VisualBrush brush = new VisualBrush(element);
brush.Stretch = Stretch.None;
brush.AlignmentX = AlignmentX.Left;
brush.AlignmentY = AlignmentY.Top;
brush.ViewboxUnits = BrushMappingMode.Absolute;
brush.TileMode = TileMode.None;
if (totalWidth < dialog.PrintableAreaWidth)
brush.Viewbox = new Rect(0, yOffset, dialog.PrintableAreaWidth, extentSize.Height);
else
brush.Viewbox = new Rect(0, yOffset, extentSize.Width, extentSize.Height);
PageContent pageContent = new PageContent();
FixedPage page = new FixedPage();
((IAddChild)pageContent).AddChild(page);
fixedDocument.Pages.Add(pageContent);
if (totalWidth < dialog.PrintableAreaWidth)
page.Width = dialog.PrintableAreaWidth;
else
page.Width = totalWidth;
page.Height = pageSize.Height;
Canvas canvas = new Canvas();
FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
if (totalWidth < dialog.PrintableAreaWidth)
canvas.Width = dialog.PrintableAreaWidth;
else
canvas.Width = extentSize.Width;
canvas.Height = extentSize.Height;
canvas.Background = brush;
page.Children.Add(canvas);
yOffset += extentSize.Height;
}
return fixedDocument;
}
Can you help
Thanks