DocumentPrinting Event


Subscribe to the DocumentPrinting event to be notified when Revit is just about to print a view or ViewSet of the document.

Namespace: Autodesk.Revit.ApplicationServices
Assembly: RevitAPI (in RevitAPI.dll) Version: 17.0.0.0 (17.0.1090.0)
Since: 2010

Syntax

C#
public event EventHandler<DocumentPrintingEventArgs> DocumentPrinting
Visual Basic
Public Event DocumentPrinting As EventHandler(Of DocumentPrintingEventArgs)
Visual C++
public:
 event EventHandler<DocumentPrintingEventArgs^>^ DocumentPrinting {
	void add (EventHandler<DocumentPrintingEventArgs^>^ value);
	void remove (EventHandler<DocumentPrintingEventArgs^>^ value);
}

Remarks

This event is raised when Revit is just about to print a view or ViewSet of the document.

Handlers of this event are permitted to make modifications to any document (including the active document), except for documents that are currently in read-only mode.

Event is cancellable. To cancel it, call the 'Cancel()' method of event's argument to True. Your application is responsible for providing feedback to the user about the reason for the cancellation.

The following API functions are not available for current document during this event:

Exception InvalidOperationException will be thrown if any of the above methods is called during this event.

After this event, for each view being printed, ViewPrinting and ViewPrinted events will be raised. Another event DocumentPrinted will be raised immediately after document printing is finished.

Examples

Copy C#
public class Application_DocumentPrinting : IExternalApplication
{
    /// <ExampleMethod>
    /// <summary>
    /// Implement the OnStartup method to register events when Revit starts.
    /// </summary>
    public Result OnStartup(UIControlledApplication application)
    {
        // Register related events
       application.ControlledApplication.DocumentPrinting += new EventHandler<Autodesk.Revit.DB.Events.DocumentPrintingEventArgs>(AppDocumentPrinting);
        return Result.Succeeded;
    }

    /// <summary>
    /// Implement this method to unregister the subscribed events when Revit exits.
    /// </summary>
    public Result OnShutdown(UIControlledApplication application)
    {
        // unregister events
       application.ControlledApplication.DocumentPrinting -= new EventHandler<Autodesk.Revit.DB.Events.DocumentPrintingEventArgs>(AppDocumentPrinting);
        return Result.Succeeded;
    }
    /// <TrivialCode>
    /// Code ID: 501
    /// For DocumentPrinting class description
    /// </TrivialCode>


    /// <summary>
    /// Handler method for DocumentPrinting event, it will display some event arguments.
    /// </summary>
    public void AppDocumentPrinting(object sender, Autodesk.Revit.DB.Events.DocumentPrintingEventArgs args)
    {
        StringBuilder info = new StringBuilder();
        info.AppendLine("DocumentPrintingEventArgs Parameters ------>");

        info.AppendLine("    Event Cancellable   : " + args.Cancellable);
        info.AppendLine("    Views to be printed : "); // Views
        Application app = sender as Application;
        DumpViewsInfo(app, args.GetViewElementIds(), "    ", ref info);

        TaskDialog.Show("Revit",info.ToString());
    }


    /// <summary>
    /// Dump information of views: ViewType, Id and ViewName.
    /// </summary>
    /// <param name="views">Views to be displayed in message box.</param>
    /// <param name="prefix">Prefix mark for each line added to message box.</param>
    /// <param name="info">String where data is stored for display</param>
    private static void DumpViewsInfo(Application app, System.Collections.Generic.IList<ElementId> viewIds, String prefix, ref StringBuilder info)
    {
        int index = 0;
        UIApplication uiApp = new UIApplication(app);
        foreach (ElementId curViewId in viewIds)
        {
            Autodesk.Revit.DB.View curView = uiApp.ActiveUIDocument.Document.GetElement(curViewId) as Autodesk.Revit.DB.View;
            DumpViewInfo(curView, String.Format("{0}#{1}", prefix, index++), ref info);
        }
    }

    /// <summary>
    /// Dump information of single view: ViewType, Id and ViewName.
    /// </summary>
    /// <param name="view">View element to be displayed in message box.</param>
    /// <param name="prefix">Prefix mark for each line sent to message box.</param>
    /// <param name="info">String where data is stored for display</param>
    private static void DumpViewInfo(Autodesk.Revit.DB.View view, String prefix, ref StringBuilder info)
    {
        info.AppendLine(String.Format("{0} Id: {1}, ViewName: {2}, ViewType: {3}",
            prefix, view.Id.IntegerValue, view.ViewName, view.ViewType));
    }
    /// </ExampleMethod>
}
Copy VB.NET
Public Class Application_DocumentPrinting
    Implements IExternalApplication
    ' <ExampleMethod>
    ' <summary>
    ' Implement the OnStartup method to register events when Revit starts.
    ' </summary>
    Public Function OnStartup(application As UIControlledApplication) As Autodesk.Revit.UI.Result Implements IExternalApplication.OnStartup
        ' Register related events
        AddHandler application.ControlledApplication.DocumentPrinting, AddressOf AppDocumentPrinting
        Return Result.Succeeded
    End Function

    ' <summary>
    ' Implement this method to unregister the subscribed events when Revit exits.
    ' </summary>
    Public Function OnShutdown(application As UIControlledApplication) As Autodesk.Revit.UI.Result Implements IExternalApplication.OnShutdown
        ' unregister events
        RemoveHandler application.ControlledApplication.DocumentPrinting, AddressOf AppDocumentPrinting
        Return Result.Succeeded
    End Function
    ' <TrivialCode>
    ' Code ID: 501
    ' For DocumentPrinting class description
    ' </TrivialCode>



    ' <summary>
    ' Handler method for DocumentPrinting event, it will display some event arguments.
    ' </summary>
    Public Sub AppDocumentPrinting(sender As Object, args As Autodesk.Revit.DB.Events.DocumentPrintingEventArgs)
        Dim info As New StringBuilder()
        info.AppendLine("DocumentPrintingEventArgs Parameters ------>")

        info.AppendLine("    Event Cancellable   : " & Convert.ToString(args.Cancellable))
        info.AppendLine("    Views to be printed : ")
        ' Views
        Dim app As Application = TryCast(sender, Application)
        DumpViewsInfo(app, args.GetViewElementIds(), "    ", info)

        TaskDialog.Show("Revit", info.ToString())
    End Sub


    ' <summary>
    ' Dump information of views: ViewType, Id and ViewName.
    ' </summary>
    ' <param name="views">Views to be displayed in message box.</param>
    ' <param name="prefix">Prefix mark for each line added to message box.</param>
    ' <param name="info">String where data is stored for display</param>
    Private Shared Sub DumpViewsInfo(app As Application, viewIds As System.Collections.Generic.IList(Of ElementId), prefix As [String], ByRef info As StringBuilder)
        Dim index As Integer = 0
        Dim uiApp As New UIApplication(app)
        For Each curViewId As ElementId In viewIds
            Dim curView As Autodesk.Revit.DB.View = TryCast(uiApp.ActiveUIDocument.Document.GetElement(curViewId), Autodesk.Revit.DB.View)
            DumpViewInfo(curView, [String].Format("{0}#{1}", prefix, System.Math.Max(System.Threading.Interlocked.Increment(index), index - 1)), info)
        Next
    End Sub

    ' <summary>
    ' Dump information of single view: ViewType, Id and ViewName.
    ' </summary>
    ' <param name="view">View element to be displayed in message box.</param>
    ' <param name="prefix">Prefix mark for each line sent to message box.</param>
    ' <param name="info">String where data is stored for display</param>
    Private Shared Sub DumpViewInfo(view As Autodesk.Revit.DB.View, prefix As [String], ByRef info As StringBuilder)
        info.AppendLine([String].Format("{0} Id: {1}, ViewName: {2}, ViewType: {3}", prefix, view.Id.IntegerValue, view.ViewName, view.ViewType))
    End Sub
    ' </ExampleMethod>
End Class

See Also