Microsoft Print to PDF not generating correct page size from PrintDialog

  • Thread starter Thread starter Mike Whalley
  • Start date Start date
M

Mike Whalley

Guest
I have a C# WPF grid (grdPrintingGrid in the code below) containing a number of text boxes, labels and a datagrid (dgdIncomeTransactions in the code below). The datagrid often contains more rows than can fit onto a single page and I am using a for loop to iterate through the data to print separate pages using PrintVisual.

The PrintVisual command works fine with a printer and everything prints correctly. However, with Microsoft Print to PDF (Windows 10), the page size is not inherited from PrintDialog correctly, and the page is not A4 (see illustration below). Using Adobe PDF Writer, the page size is set to A4 correctly, but the datagrid is similarly truncated, cutting off a number of rows of the datagrid. In both cases, Size is correctly set to A4 dimensions.

The code is:

private void mnuPrint_Click(object sender, RoutedEventArgs e)
{
PrintDialog printDlg = new PrintDialog();
printDlg.PrintTicket.PageOrientation = PageOrientation.Portrait;
if ((bool)printDlg.ShowDialog().GetValueOrDefault())
{
dplPastTransactions.Visibility = Visibility.Hidden;
dgdIncomeTransactions.SelectedIndex = -1;
int rowCount = dgdIncomeTransactions.Items.Count;
int actualRowCount = rowCount - SortAnnualTransactions.BlankLines;
int pageRowCount = 0;
int pageNumber = 1;
grdPrintingGrid.RenderTransform = Transform.Identity;
grdPrintingGrid.RowDefinitions[0].Height = new GridLength(20);
grdPrintingGrid.RowDefinitions[3].Height = new GridLength(25);
scvAnnualScrollViewer.ScrollToTop();
scvAnnualScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
dgdIncomeTransactions.SelectedIndex = -1;
grdPrintingGrid.UpdateLayout();
grdPrintingGrid.LayoutTransform = new ScaleTransform(0.9, 0.9);

int lengthPages = actualRowCount < (firstPageLines+1) ? 1 : ((int)(actualRowCount - firstPageLines) / 48) + 2;
Size pageSize = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight);

for (pageNumber = 1; pageNumber <= lengthPages; pageNumber++)
{
if (pageNumber>1)
{
//Expand PrintingGrid so that it includes all necessary datagrid rows
grdPrintingGrid.RowDefinitions[2].Height = new GridLength(975);
grdPrintingGrid.UpdateLayout();
collapseHeaders(true, pageNumber);
grdIncomeTransactions.Height = (48 * 20)+10;
grdPrintingGrid.UpdateLayout();
dgdIncomeTransactions.ScrollIntoView(dgdIncomeTransactions.Items[rowCount-1]);//Go to end
dgdIncomeTransactions.UpdateLayout();
dgdIncomeTransactions.ScrollIntoView(dgdIncomeTransactions.Items[pageRowCount]);//Back up to top of page
}
else
{
grdPrintingGrid.RowDefinitions[2].Height = new GridLength(975);
grdIncomeTransactions.Height = (firstPageLines * 20) + 10;
grdPrintingGrid.UpdateLayout();
}
grdPrintingGrid.UpdateLayout();
grdPrintingGrid.Measure(pageSize);
grdPrintingGrid.Arrange(new Rect(5, 25, pageSize.Width, pageSize.Height ));
if (pageRowCount<actualRowCount)
{
printDlg.PrintVisual(grdPrintingGrid, "PMM Annual Report page "+pageNumber.ToString());
}
pageRowCount += pageNumber==1? firstPageLines: 48;

}
collapseHeaders(false, pageNumber);
grdPrintingGrid.RowDefinitions[0].Height = new GridLength(50);
dplPastTransactions.Visibility = Visibility.Visible;
dgdIncomeTransactions.ScrollIntoView(dgdIncomeTransactions.Items[0]);
grdIncomeTransactions.Height = ((dgdIncomeTransactions.Items.Count - SortAnnualTransactions.BlankLines) * 20) + 10;
grdPrintingGrid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
scvAnnualScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
grdPrintingGrid.UpdateLayout();
scvAnnualScrollViewer.ScrollToTop();
grdPrintingGrid.RenderTransform = new ScaleTransform(1.1111, 1.1111);
}
}
and the resulting PDFs are, for Microsoft Print to PDF (page size not correct)


1359136.png


and for Adobe PDF (page size correct but datagrid truncated):

1359137.png

Can you suggest why the page is not printing to PDF correctly in each case?




Mike Whalley

Continue reading...
 
Back
Top