2011-08-01 12 views
1

richTextBox画像が含まれており、各単語の上に画像adornerのレイヤーが含まれています。
文書を適切な形式で印刷し、adorner画像のレイヤーも印刷したいとします。
これを達成する方法はありますか?
私はPrintDialogを試しましたが、それはアドナー層を印刷しませんでした。リッチテキストボックス内のAdornerレイヤーの印刷

+0

ない直接的な答えすることができますが、このリンクヘルプあなたのhttp:?//social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae8312df-9ed9-4c4c-951b-42cee5427afa/ コンテンツコントロール全体をXPSとして印刷することができます。希望が役立ちます。 – NetSquirrel

答えて

0

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae8312df-9ed9-4c4c-951b-42cee5427afa/を使用してアイテムに関連付けられたAdornerレイヤーを印刷することができました。私はそれがRichTextBoxのために働くかどうかわからない、私はRichTextBox内でAdornerを追加することができませんでした。

AdornerがOnRenderに追加されているので、印刷する前にすべてがスクリーンに表示されるまで待たなければならなかったため、Print_Loaded関数で印刷するとAdornerレイヤーが表示されませんでした。

AdornerLayerが印刷するには、ContentControlのスタイルが必要です。

コードは次のとおりです。

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

AdornerマイクロソフトSimpleCircleAdornerから。

希望すると、これが役に立ちます。

+0

これは素晴らしいですが、文書全体の内容ではなく、画面に表示される文書の内容のみを印刷します:( – raym0nd

+0

アドオンが「レンダリング中」に追加されているため、おそらくこれらの「オフセセンス」を印刷する 印刷のためにすべてを準備し、ドキュメントに埋め込まれた実際のイメージとしてアドナーを追加する、実際のXPSドキュメントを作成することが最善の方法かもしれません。 、幸運 – NetSquirrel

+0

私はそれをどうやって行うことができますか?私はすでにたくさんのことを試みましたが、何も私に良い結果を与えていません。 – raym0nd

0

私がレポートを作成しなければならなかったときの代わりに、FlowDocumentを使用して、必要なものすべてを埋め込んでいました。それはhereのように見えます。

このレポートを生成するために使用される醜いコードは次のとおりです。この方法が役立つ場合は、これが役立つと思います。あまりにも長い場合は、私はそれをきれいにする時間がありません、 "//FlowDocumentの開始"で読書を開始する必要があります。

レポートにUI要素を含めるには、FlowDocumentリンクの説明に従ってUIContainersを使用できます。そして、フロー文書にAdornersを追加するために、それをhere on stackoverflowというソリューションと混ぜてもよいでしょうか?

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; 
    } 
本当に