Nevron Forum

NPrinterManager - PrintOnSinglePage

https://www.nevron.com/Forum/Topic7270.aspx

By Mohamed Koker - Thursday, January 3, 2013

Hi All,

I upgraded to version 12.12.17.12 recently and found the following code no longer compiles:

NPrintManager printManager = new NPrintManager(document);
printManager.PrintOnSinglePage = true;

I would be grateful for any help in getting this code to compile.

Thanks in advance

-Mohamed
By Trond Borg - Thursday, March 14, 2013

I am having the same issue, and would also appreciate a fix for this
By Mohamed Koker - Thursday, March 14, 2013

Please try the following:


NPrintManager printManager = new NPrintManager(document);
printManager.Layout = PagedLayout.FitToPages;
printManager.PageColumns = 1;
printManager.PageRows = 1;
By Trond Borg - Thursday, March 14, 2013

What I was trying to do was to autoselect "Fit To Pages" in the print preview dialog that pops up from the standard toolbar. That didnt work with this code either.

Is that not possible?
By Nevron Support - Friday, March 15, 2013

The print preview diagram command executes this code:

if (Commander == null)
 return;

NDrawingDocument document = Commander.Document;
if (document == null)
 return;

NPrintManager manager = new NPrintManager(document);
try
{
 manager.ShowPrintPreview();
}
catch (Exception ex)
{
 MessageBox.Show("Failed to show the print preview dialog. Exception was: " + ex.Message);
}

So as you can see you need to override the command in order to preselect any settings of the newly created print manager, that is created each time you click the PrintPreview command, like this:

public class MyPrintPreviewCommand : NPrintPreviewCommand
{
    public override void Execute()
    {
        if (Commander == null)
            return;

        NDrawingDocument document = Commander.Document;
        if (document == null)
            return;

        NPrintManager manager = new NPrintManager(document);
        manager.Layout = Nevron.GraphicsCore.PagedLayout.FitToPages;
        try
        {
            manager.ShowPrintPreview();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed to show the print preview dialog. Exception was: " + ex.Message);
        }
    }

    pubilc static void UpdateCmdBarManager(NDiagramCommandBarsManager cmdBarManager)
    {
        NDiagramCommand oldCommand = cmdBarManager.Commander.Commands.GetCommandFromId((int)DiagramCommand.PrintPreview);
        cmdBarManager.Commander.Commands.Remove(oldCommand);
        cmdBarManager.Commander.Commands.Add(new MyPrintPreviewCommand());

        cmdBarManager.Recreate();
    }
}

Last thing to do is to invoke the UpdateCmdBarManager by passing a reference to your command bars manager.

 

By Trond Borg - Monday, March 18, 2013

Unfortunately this is not working... when calling the UpdateCmdBarManager, it throws an exception on the first line:
"NDiagramCommand oldCommand = cmdBarManager.Commander.Commands.GetCommandFromId((int)DiagramCommand.PrintPreview);"

The errormessage is:

"Item has already been added. Key in dictionary: '5' Key being added: '5'"

Seems very strange as I would think the GetCommandFromId does nothing, but fetch the command...
By Trond Borg - Tuesday, March 19, 2013

I was able to workaround but am now getting another exception.

The workaround is;

public static void UpdateCmdBarManager(NDiagramCommandBarsManager cmdBarManager)
{
NDiagramCommand oldCommand = null;
foreach (NDiagramCommand command in cmdBarManager.Commander.Commands)
if (command.Id == (int) DiagramCommand.PrintPreview)
oldCommand = command;
if (oldCommand != null)
cmdBarManager.Commander.Commands.Remove(oldCommand);
cmdBarManager.Commander.Commands.Add(new MyPrintPreviewCommand());
cmdBarManager.Recreate();
}

However, now I am getting an exception: "Arithmetic operation resulted in an overflow." when executing manager.ShowPrintPreview();
By Nevron Support - Tuesday, March 19, 2013

Hi,

This is a complete working sample that substitudes the print preview command.

using System;

using System.Windows.Forms;

using Nevron.Diagram;

using Nevron.Diagram.Extensions;

using Nevron.Diagram.WinForm;

using Nevron.Diagram.WinForm.Commands;

namespace WindowsFormsApplication15

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

NDrawingView view = new NDrawingView();

view.Dock = DockStyle.Fill;

Controls.Add(view);

view.Document = new NDrawingDocument();

NDiagramCommandBarsManager manager = new NDiagramCommandBarsManager();

manager.ParentControl = this;

manager.View = view;

MyPrintPreviewCommand.UpdateCmdBarManager(manager);

}

}

public class MyPrintPreviewCommand : NPrintPreviewCommand

{

public override void Execute()

{

if (Commander == null)

return;

NDrawingDocument document = Commander.Document;

if (document == null)

return;

NPrintManager manager = new NPrintManager(document);

manager.Layout = Nevron.GraphicsCore.PagedLayout.FitToPages;

try

{

manager.ShowPrintPreview();

}

catch (Exception ex)

{

MessageBox.Show("Failed to show the print preview dialog. Exception was: " + ex.Message);

}

}

public static void UpdateCmdBarManager(NDiagramCommandBarsManager cmdBarManager)

{

NDiagramCommand oldCommand = cmdBarManager.Commander.Commands.GetCommandFromId((int)DiagramCommand.PrintPreview);

cmdBarManager.Commander.Commands.Remove(oldCommand);

cmdBarManager.Commander.Commands.Add(new MyPrintPreviewCommand());

cmdBarManager.Recreate();

}

}

}

Parhaps there is another reason for the code not to work in your enviroment.