Search code examples
c#wpfrichtextboxadorneradornerlayer

Printing Adorner Layers in a RichTextBox


I have a richTextBox Editor that contains image adorner layers on top of each word.
I want to print the document in a good format and print the adorner picture layers too.
Is there a way I can accomplish this?
I tried PrintDialog but it didnt print the adorner layers.


Solution

  • I managed to print an Adorner layer associated with a item using http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae8312df-9ed9-4c4c-951b-42cee5427afa/ . I don't know if it would work for the RichTextBox, I wasn't able to add the Adorner inside the RichTextBox.

    Since the Adorner is added OnRender, I had to wait until all was rendered on screen before printing it (hence the Print button), if I printed in the Window_Loaded function, I did not get the Adorner layer.

    The Style to the ContentControl is necessary for the AdornerLayer to print.

    Code follows:

    <Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <Window.Resources>
        <Style TargetType="{x:Type ContentControl}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ContentControl}">
            <AdornerDecorator>
              <ContentPresenter
                        Content="{TemplateBinding Content}"
                        ContentTemplate="{TemplateBinding ContentTemplate}" />
            </AdornerDecorator>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    </Window.Resources>
    <ContentControl Name="MyContent">
        <StackPanel>
    <Grid Name="MyGrid" Margin="10">
        <RichTextBox Name="MyDoc" Margin="2">
            <FlowDocument Name="MyFlow">
                <Paragraph Name="MyParagraph">
                    I am a flow document. Would you like to edit me?
                        <Bold>Go ahead.</Bold>
                </Paragraph>
    
                <Paragraph Foreground="Blue">
                    I am blue I am blue I am blue.
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
    </Grid>
            <Button Click="Button_Click">Print</Button>
            </StackPanel>
    </ContentControl>
    

    Window code

     public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var myAdornerLayer = AdornerLayer.GetAdornerLayer(MyDoc);
            myAdornerLayer.Add(new SimpleCircleAdorner(MyDoc));
    
    
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LocalPrintServer ps = new LocalPrintServer();
            PrintQueue pq = ps.DefaultPrintQueue;
            XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(pq);
            PrintTicket pt = pq.UserPrintTicket;
            if (xpsdw != null)
            {
                pt.PageOrientation = PageOrientation.Portrait;
                PageMediaSize pageMediaSize = new PageMediaSize(this.ActualWidth, this.ActualHeight);
                pt.PageMediaSize = pageMediaSize;
    
                xpsdw.Write(MyContent);
            }
        }
    }
    

    Adorner from Microsoft SimpleCircleAdorner.

    Hope this helps you.