首页 文章

在RichTextBox中打印Adorner图层

提问于
浏览
1

我有一个 richTextBox 编辑器,在每个单词的顶部包含图像 adorner 层 .
我想以良好的格式打印文档并打印 adorner 图片层 .
有没有办法可以做到这一点?
我试过 PrintDialog 但它没有打印装饰层 .

2 回答

  • 0

    我设法使用http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae8312df-9ed9-4c4c-951b-42cee5427afa/打印与项目关联的Adorner图层 . 我没有't know if it would work for the RichTextBox, I wasn'能够在RichTextBox中添加Adorner .

    由于Adorner添加了OnRender,我不得不等到所有内容都在屏幕上呈现后再打印(因此打印按钮),如果我在Window_Loaded函数中打印,我没有得到Adorner图层 .

    ContentControl的样式是AdornerLayer打印所必需的 .

    代码如下:

    <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>
    

    窗口代码

    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);
            }
        }
    }
    

    来自Microsoft的装配工SimpleCircleAdorner .

    希望这对你有所帮助 .

  • 0

    我在创建报告时使用的替代方案是,我使用了FlowDocument,其中嵌入了我需要的所有内容 . 你可以看到here的样子 .

    以下是用于生成此报告的丑陋代码,我希望这可能会有所帮助,并指出如果您采用这种方式该怎么做 . 抱歉,如果它太长,我没有时间清理它,开始阅读“// START of FlowDocument” .

    要将UI元素包含到报表中,可以使用FlowDocument链接下所述的UIContainer . 也许可以将它与创建的解决方案here on stackoverflow混合,将Adorners添加到流文档中?

    private FlowDocument HydroPowerReportI(string PlantPath, string PlantName, bool filled)
        {
            //Ignore these first 20 lines, they are there to populate my data
            List<HydroMarket.LocalData.BinaryIO.HydroFileIO.Report1> data = new List<HydroMarket.LocalData.BinaryIO.HydroFileIO.Report1>();
            int nTime = 0;
            List<int> iReportingPoints = new List<int>();
            SimulationsTreeViewModel S = (this.DataContext as SimulationsTreeViewModel);
            List<DateTime> dates = new List<DateTime>();
            if (filled)
            {
                if (HydroFileIO.ObtainReportI(data, out nTime, PlantPath))
                {
                    MarketParameters m = MarketParameters.ReadScenarioParameters(new FileInfo(System.IO.Path.Combine(PlantPath, "MarketParam.bin")));
                    iReportingPoints.Add(0); 
                    iReportingPoints.Add(1); 
                    iReportingPoints.Add(2); 
                    iReportingPoints.Add(3);
                    iReportingPoints.Add(4);
    
                    dates.Add(m.ValueDate);
                    foreach (DateTime x in m.ReportingDates)
                    {
                        DateTime dt = SetBetween(m.ValueDate, x, m.Maturity);
                        dates.Add(dt);
                    }
                    dates.Add(m.Maturity);
                }
                else
                {
                    filled = false;
                }
            }
            //START of FlowDocument 
            Brush HeaderColor = Brushes.RoyalBlue;
            FlowDocument flowDoc = new FlowDocument();
    
            //Original table holder with columns
            Table table2 = new Table();
            flowDoc.Blocks.Add(table2);
    
            table2.Columns.Add(new TableColumn());
            table2.Columns.Add(new TableColumn());
            table2.Columns.Add(new TableColumn());
            table2.Columns.Add(new TableColumn());
            table2.Columns.Add(new TableColumn());
            table2.Columns.Add(new TableColumn());
    
            table2.Columns[0].Width = new GridLength(280);
    
            // Create and add an empty TableRowGroup to hold the table's Rows.
            table2.RowGroups.Add(new TableRowGroup());
            // Add the first (title) row.
            table2.RowGroups[0].Rows.Add(new TableRow());
            // Alias the current working row for easy reference.
            TableRow currentRow2 = new TableRow();
            currentRow2 = table2.RowGroups[0].Rows[0];
    
            // Add cells with content to the second row.
            currentRow2.Cells.Add(new TableCell(new Paragraph(new Run("Valuation Report"))));
            currentRow2.Cells[0].ColumnSpan = 6; //1 less than center for offsetting it
            currentRow2.FontSize = 32;
            currentRow2.Foreground = HeaderColor;
            currentRow2.Cells[0].TextAlignment = TextAlignment.Left;
    
            table2.RowGroups[0].Rows.Add(new TableRow());
            currentRow2 = table2.RowGroups[0].Rows[1];
            if (filled)
                if (S.SimSetup.LastSimulation.HasValue)
                    currentRow2.Cells.Add(new TableCell(new Paragraph(new Run("Valuation: " + S.Name + "\nMarket: " + S.SimSetup.Market.Name + "\nPlant: " + PlantName + "\nLast simulated at: " + S.SimSetup.LastSimulation.Value.ToLongDateString() + " " + S.SimSetup.LastSimulation.Value.ToShortTimeString()))));
                else
                    currentRow2.Cells.Add(new TableCell(new Paragraph(new Run("Valuation: " + S.Name + "\nMarket: " + S.SimSetup.Market.Name + "\nPlant: " + PlantName))));
            else
            {
                //BUGFIX: 091113 
                //When no data is loaded, it is unclear that it has not loaded data
                currentRow2.Cells.Add(new TableCell(new Paragraph(new Run("No simulation data available for valuation " + S.Name))));
            }
            currentRow2.Cells[0].TextAlignment = TextAlignment.Left;
            currentRow2.Cells[0].ColumnSpan = 6; //all for the center
            currentRow2.Cells[0].Padding = new Thickness(10);
    
            //Table Headers
            // Create and add an empty TableRowGroup to hold the table//s Rows.
            table2.RowGroups.Add(new TableRowGroup());
            // Add the first (title) row.
            table2.RowGroups[1].Rows.Add(new TableRow());
            // Alias the current working row for easy reference.
            int iRow = 0;
            TableRow currentRow = new TableRow();
            currentRow = table2.RowGroups[1].Rows[iRow];
            // Add the header row with content,
            if (filled)
            {
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Value date"))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Maturity"))));
    
                table2.RowGroups[1].Rows.Add(new TableRow());
                iRow = 1;
                currentRow = table2.RowGroups[1].Rows[iRow];
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dates[0].ToShortDateString()))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dates[1].ToShortDateString()))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dates[2].ToShortDateString()))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dates[3].ToShortDateString()))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dates[4].ToShortDateString()))));
            }
            else
            {
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Value date"))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Maturity"))));
                table2.RowGroups[1].Rows.Add(new TableRow());
                iRow = 1;
                currentRow = table2.RowGroups[1].Rows[iRow];
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
                currentRow.Cells.Add(new TableCell(new Paragraph(new Run(" "))));
            }
            for (int i = 0; i < 6; i++)
            {
                currentRow.Cells[i].Foreground = HeaderColor;
                currentRow.Cells[i].TextAlignment = TextAlignment.Right;
            }
            iRow++;
            table2.RowGroups[1].Rows.Add(new TableRow());
            currentRow = table2.RowGroups[1].Rows[iRow];
            currentRow.Cells.Add(new TableCell());
            currentRow.Cells[0].ColumnSpan = 6;
            currentRow.Cells[0].BorderThickness = new Thickness(0, 5, 0, 0);
            currentRow.Cells[0].BorderBrush = HeaderColor;
    
            for (int i = 3; i < 23; i++)
            {
                table2.RowGroups[1].Rows.Add(new TableRow());
                currentRow = table2.RowGroups[1].Rows[i];
            }
            table2.RowGroups[1].Rows[3].Cells.Add(new TableCell(new Paragraph(new Run("Expected value"))));
            table2.RowGroups[1].Rows[4].Cells.Add(new TableCell(new Paragraph(new Run("Expected price"))));
            table2.RowGroups[1].Rows[5].Cells.Add(new TableCell(new Paragraph(new Run("Expected income (cumulated)"))));
            table2.RowGroups[1].Rows[6].Cells.Add(new TableCell(new Paragraph(new Run("Expected income"))));
            table2.RowGroups[1].Rows[7].Cells.Add(new TableCell(new Paragraph(new Run("Expected income by generation"))));
            table2.RowGroups[1].Rows[8].Cells.Add(new TableCell(new Paragraph(new Run("Expected costs of pumping"))));
            table2.RowGroups[1].Rows[9].Cells.Add(new TableCell(new Paragraph(new Run("Expected penalties"))));
            table2.RowGroups[1].Rows[10].Cells.Add(new TableCell(new Paragraph(new Run("Expected costs of switching on/off"))));
            table2.RowGroups[1].Rows[11].Cells.Add(new TableCell(new Paragraph(new Run("Standard deviation of value"))));
            table2.RowGroups[1].Rows[12].Cells.Add(new TableCell(new Paragraph(new Run("Value at Risk (95%)"))));
            table2.RowGroups[1].Rows[13].Cells.Add(new TableCell(new Paragraph(new Run("Generated electricity (MWh)"))));
            table2.RowGroups[1].Rows[14].Cells.Add(new TableCell(new Paragraph(new Run("Consumed electricity (MWh)"))));
            table2.RowGroups[1].Rows[15].Cells.Add(new TableCell(new Paragraph(new Run("Days generated"))));
            table2.RowGroups[1].Rows[16].Cells.Add(new TableCell(new Paragraph(new Run("Days pumped"))));
            table2.RowGroups[1].Rows[17].Cells.Add(new TableCell(new Paragraph(new Run("Expected volume (m³)"))));
            table2.RowGroups[1].Rows[18].Cells.Add(new TableCell(new Paragraph(new Run("Expected volume used for generation (m³)"))));
            table2.RowGroups[1].Rows[19].Cells.Add(new TableCell(new Paragraph(new Run("Expected volume used for pumping (m³)"))));
            table2.RowGroups[1].Rows[20].Cells.Add(new TableCell(new Paragraph(new Run("Expected volume natural inflow (m³)"))));
            table2.RowGroups[1].Rows[21].Cells.Add(new TableCell(new Paragraph(new Run("Standard deviation of volume"))));
            table2.RowGroups[1].Rows[22].Cells.Add(new TableCell(new Paragraph(new Run("Volume at risk"))));
    
            if (filled)
            {
                //Writing 
                for (int j = 0; j < 5; j++)
                {
                    table2.RowGroups[1].Rows[3].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].Value.ToString("N0")))));
                    table2.RowGroups[1].Rows[4].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].Price.ToString("N0")))));
                    table2.RowGroups[1].Rows[5].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].IncomeCum.ToString("N0")))));
                    table2.RowGroups[1].Rows[6].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].Income.ToString("N0")))));
                    //table2.RowGroups[1].Rows[6].Cells.Add(new TableCell(new Paragraph(new Run(income[j].ToString("N0")))));
                    table2.RowGroups[1].Rows[7].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].IncomeGen.ToString("N0")))));
                    table2.RowGroups[1].Rows[8].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].CostsPmp.ToString("N0")))));
                    table2.RowGroups[1].Rows[9].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].Penalties.ToString("N0")))));
                    table2.RowGroups[1].Rows[10].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].CostSwitch.ToString("N0")))));
                    table2.RowGroups[1].Rows[11].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].StdValue.ToString("N0")))));
                    table2.RowGroups[1].Rows[12].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].VaRValue.ToString("N0")))));
                    table2.RowGroups[1].Rows[13].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].PowGen.ToString("N0")))));
                    table2.RowGroups[1].Rows[14].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].PowPump.ToString("N0")))));
                    table2.RowGroups[1].Rows[15].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].DaysGen.ToString("N0")))));
                    table2.RowGroups[1].Rows[16].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].DaysPmp.ToString("N0")))));
                    table2.RowGroups[1].Rows[17].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].Volume.ToString("N0")))));
                    table2.RowGroups[1].Rows[18].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].VolGen.ToString("N0")))));
                    table2.RowGroups[1].Rows[19].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].VolPmp.ToString("N0")))));
                    table2.RowGroups[1].Rows[20].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].VolNat.ToString("N0")))));
                    table2.RowGroups[1].Rows[21].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].StdVolume.ToString("N0")))));
                    table2.RowGroups[1].Rows[22].Cells.Add(new TableCell(new Paragraph(new Run(data[iReportingPoints[j]].VaRVolume.ToString("N0")))));
                }
            }
            else
            {
                double d = 0.00;
                for (int j = 0; j < 5; j++)
                {
                    for (int k = 3; k < 23; k++)
                        table2.RowGroups[1].Rows[k].Cells.Add((new TableCell(new Paragraph(new Run(d.ToString("N0"))))));
                }
            }
            for (int r = 3; r < 23; r++)
                for (int j = 1; j < 6; j++)
                    table2.RowGroups[1].Rows[r].Cells[j].TextAlignment = TextAlignment.Right;
    
            for (int j = 0; j < 6; j++)
            {
                table2.RowGroups[1].Rows[10].Cells[j].BorderThickness = new Thickness(0, 1, 0, 0);
                table2.RowGroups[1].Rows[10].Cells[j].BorderBrush = Brushes.Black;
                table2.RowGroups[1].Rows[12].Cells[j].BorderThickness = new Thickness(0, 1, 0, 0);
                table2.RowGroups[1].Rows[12].Cells[j].BorderBrush = Brushes.Black;
                table2.RowGroups[1].Rows[14].Cells[j].BorderThickness = new Thickness(0, 1, 0, 0);
                table2.RowGroups[1].Rows[14].Cells[j].BorderBrush = Brushes.Black;
                table2.RowGroups[1].Rows[20].Cells[j].BorderThickness = new Thickness(0, 1, 0, 0);
                table2.RowGroups[1].Rows[20].Cells[j].BorderBrush = Brushes.Black;
                table2.RowGroups[1].Rows[22].Cells[j].BorderThickness = new Thickness(0, 1, 0, 0);
                table2.RowGroups[1].Rows[22].Cells[j].BorderBrush = Brushes.Black;
            }
    
            return flowDoc;
        }
    

相关问题