DocumentPrinted Event


Subscribe to the DocumentPrinted event to be notified immediately after Revit has finished printing a view or ViewSet of the document.

Namespace: Autodesk.Revit.ApplicationServices
Assembly: RevitAPI (in RevitAPI.dll) Version: 19.0.0.0 (19.0.0.405)
Since: 2010

Syntax

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

Remarks

This event is raised immediately after Revit has finished printing a view or ViewSet of the document. It is raised even when document printing failed or was cancelled (during DocumentPriting event).

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.

Check the 'Status' field in event's argument to see whether the action itself was successful or not.

This event is not cancellable, for the process of printing has already been finished.

If the action was not successful, the document may not be modified and new transactions may not be started.

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

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

Examples

Copy C#
public class Application_DocumentPrinted : IExternalApplication
{
    /// <ExampleMethod>
    /// <summary>
    /// Implement the OnStartup method to register events when Revit starts.
    /// </summary>
    public Result OnStartup(UIControlledApplication application)
    {
        // Register related events
       application.ControlledApplication.DocumentPrinted += new EventHandler<Autodesk.Revit.DB.Events.DocumentPrintedEventArgs>(AppDocumentPrinted);
        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.DocumentPrinted -= new EventHandler<Autodesk.Revit.DB.Events.DocumentPrintedEventArgs>(AppDocumentPrinted);
        return Result.Succeeded;
    }
    /// <TrivialCode>
    /// Code ID: 501
    /// For DocumentPrinted class description
    /// </TrivialCode>


    /// <summary>
    /// Handler method for DocumentPrinted event, it will display some event arguments.
    /// </summary>
    public void AppDocumentPrinted(object sender, Autodesk.Revit.DB.Events.DocumentPrintedEventArgs args)
    {
        StringBuilder info = new StringBuilder();
        Application app = sender as Application;
        info.AppendLine("DocumentPrintedEventArgs Parameters ------>");
        info.AppendLine("    Event Status      : " + args.Status.ToString());
        info.AppendLine("    Event Cancellable : " + args.Cancellable);
        // 
        // PrintedViews
        if (0 == args.GetFailedViewElementIds().Count || 0 == args.GetPrintedViewElementIds().Count)
        {
            info.AppendLine("    Views been printed: <null>");
        }
        else
        {
            info.AppendLine("    Views been printed: ");
            DumpViewsInfo(app, args.GetPrintedViewElementIds(), "      ", ref info);
        }
        // 
        // FailedViews
        if (0 == args.GetFailedViewElementIds().Count)
        {
            info.AppendLine("    Views failed: <null>");
        }
        else
        {
            info.AppendLine("    Views Failed : ");
            DumpViewsInfo(app, args.GetFailedViewElementIds(), "      ", 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 added 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.Name, view.ViewType));
    }
    /// </ExampleMethod>
}
Copy VB.NET
Public Class Application_DocumentPrinted
    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.DocumentPrinted, AddressOf AppDocumentPrinted
        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.DocumentPrinted, AddressOf AppDocumentPrinted
        Return Result.Succeeded
    End Function
    ' <TrivialCode>
    ' Code ID: 501
    ' For DocumentPrinted class description
    ' </TrivialCode>



    ' <summary>
    ' Handler method for DocumentPrinted event, it will display some event arguments.
    ' </summary>
    Public Sub AppDocumentPrinted(sender As Object, args As Autodesk.Revit.DB.Events.DocumentPrintedEventArgs)
        Dim info As New StringBuilder()
        Dim app As Application = TryCast(sender, Application)
        info.AppendLine("DocumentPrintedEventArgs Parameters ------>")
        info.AppendLine("    Event Status      : " & args.Status.ToString())
        info.AppendLine("    Event Cancellable : " & Convert.ToString(args.Cancellable))
        '
        ' PrintedViews
        If 0 = args.GetFailedViewElementIds().Count OrElse 0 = args.GetPrintedViewElementIds().Count Then
            info.AppendLine("    Views been printed: <null>")
        Else
            info.AppendLine("    Views been printed: ")
            DumpViewsInfo(app, args.GetPrintedViewElementIds(), "      ", info)
        End If
        '
        ' FailedViews
        If 0 = args.GetFailedViewElementIds().Count Then
            info.AppendLine("    Views failed: <null>")
        Else
            info.AppendLine("    Views Failed : ")
            DumpViewsInfo(app, args.GetFailedViewElementIds(), "      ", info)
        End If

        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 added 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.Name, view.ViewType))
    End Sub
    ' </ExampleMethod>
End Class

See Also