View previous topic :: View next topic |
Author |
Message |
JohnSummit2
Joined: 18 Jan 2011 Posts: 12
|
Posted: Sat Jan 04, 2014 8:12 pm Post subject: VirtualTreePrintDocument : scale to fit page? |
|
|
Can you give me some tips how to scale the page to fit the printed page? Currently, the last two columns spills over to the next page.
Code: |
VirtualTreePrintDocument pd = new VirtualTreePrintDocument(virtualTree1);
pd.StartRowIndex = 0;
pd.EndRowIndex = 10;
pd.DocumentName = strFileName;
pd.DefaultPageSettings = pageSettings1;
pd.PrinterSettings = printerSettings1;
PrintPreviewDialog previewDialog = new PrintPreviewDialog();
previewDialog.Document = pd;
previewDialog.ShowDialog(this);
|
I'd like the preview / printed image to be "scaled to fit" such that all columns fit on the same page. The rows should then span multiple pages, if needed.
I'm using version 3.11.2.0
Thanks. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Sun Jan 05, 2014 10:47 pm Post subject: |
|
|
Virtual Tree doesn't currently support Scale to Page for printing. Like Excel, if the columns won't fit on the page then it will print them to an additional page. Developing a useful general purpose scale to page function for printing is quite difficult because if you have many columns the text becomes illegible or small anyway.
For a specific application you can modify the size of columns before printing so that they all fit on one page eg to reduce all the columns sizes by 50%
Code: |
_virtualTree.SuspendLayout();
Hashtable columnWidth = new Hashtable();
foreach (Column column in _virtualTree.Columns)
{
columnWidth[column] = column.Width;
column.Width = (int)(column.Width * 0.5F);
}
VirtualTreePrintDocument pd = new VirtualTreePrintDocument(_virtualTree);
pd.Print();
// restore the original column widths
//
foreach (Column column in _virtualTree.Columns)
{
column.Width = (int)columnWidth[column];
}
_virtualTree.ResumeLayout(); |
You may also need to change the font to enable the text to fit. _________________ Infralution Support |
|
Back to top |
|
|
JohnSummit2
Joined: 18 Jan 2011 Posts: 12
|
Posted: Tue Jan 07, 2014 8:28 pm Post subject: |
|
|
Thanks for the response. I think it would be beneficial in my application to scale the width of the tree to the width of the paper, regardless of the paper size and orientation. If I just change the column width and font size then my row icons and row height will not change so it might look strange?
Would it be possible to 'fool' the VirtualTreePrintDocument to draw to a larger Graphics surface and then scale the resulting page image down to the extent of the printed page? Perhaps by creating a new Graphics object that draws to a bitmap?
If not, I'm considering rolling my own PrintDocument.PrintPage. It should hopefully not be too hard to print the rows that are visible / expanded. However, I would have to spend quite a lot of time reading in on GDI+ details so I would prefer to utilize the existing VirtualTreePrintDocument class and somehow tweak things using a PageScale or similar GDI+ feature.
Any tips you could give me are greatly appreciated. I would otherwise have to make a deep detour into GDI+ land...
Thanks! |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Tue Jan 07, 2014 11:46 pm Post subject: |
|
|
You can change the graphics.PageScale factor by overriding AdjustPageScale to scale all the graphics. However this affects the calculations of the number of rows and columns to display and so you would also have to change the PrintDocument methods to handle this.
I'd be happy to provide you with the source code for the VirtualTreePrintDocument if you email support@infralution.com and request it. The VirtualTree.Print method handles the bulk of the logic for actually printing the tree - the VirtualTreePrintDocument really just handles pagination - which is the logic you want to change. _________________ Infralution Support |
|
Back to top |
|
|
JohnSummit2
Joined: 18 Jan 2011 Posts: 12
|
Posted: Thu Jan 09, 2014 7:24 am Post subject: |
|
|
Thanks. For reference, I have inserted my finished VirtualTreePrintDocumentEx subclass. This automatically scales the printed output such that the width will fit any paper size and orientation.
Code: |
public class VirtualTreePrintDocumentEx : VirtualTreePrintDocument
{
Hashtable m_htColumnWidths = new Hashtable();
Font m_originalFont;
public VirtualTreePrintDocumentEx(VirtualTree virtualTree)
: base(virtualTree)
{
}
protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
{
//////////////////////////////////////////////////////////
// Calculate how much we need to scale the column widths
// to fit in the available drawing surface width.
//////////////////////////////////////////////////////////
float xcolumns = 0;
foreach (Column column in this.Columns)
{
xcolumns += column.Width;
}
float xpage = this.DefaultPageSettings.Bounds.Width - this.DefaultPageSettings.Margins.Left - this.DefaultPageSettings.Margins.Right;
float xscale = xpage / xcolumns;
//////////////////////////////////////////////////////////
// Scale the column widths. Save original widths
// so we can restore them in OnEndPrint.
//////////////////////////////////////////////////////////
foreach (Column column in this.Columns)
{
m_htColumnWidths[column] = column.Width;
column.Width = (int)(column.Width * xscale);
}
//////////////////////////////////////////////////////////
// Scale the font used by the tree.
//////////////////////////////////////////////////////////
m_originalFont = this.Tree.Font;
this.Tree.Font = new Font(m_originalFont.FontFamily, m_originalFont.SizeInPoints * xscale);
base.OnBeginPrint(e);
}
protected override void OnEndPrint(System.Drawing.Printing.PrintEventArgs e)
{
foreach (Column column in this.Columns)
{
column.Width = (int)m_htColumnWidths[column];
}
this.Tree.Font = m_originalFont;
base.OnEndPrint(e);
}
| [/code] |
|
Back to top |
|
|
|