2013-02-21 110 views
2

我有一个打印按钮,必须打印两个文档: - 此人写的一封信 其次,在第二个打印中,我必须处理可能的多页信函。我怎样才能做到这一点?在很多页面上打印信件

这封信是一个文本框里面,我的网页上。

这里是我的代码:

void btPrint_Click(object sender, RoutedEventArgs e) 
     { 

      if (itmCandidatSelect.LettreMotiv != null || itmCandidatSelect.LettreMotiv != "") 
      { 
       _lineIndex = 0; 
       _documentBodyLines = new List<string>(); 
       string[] lines = tbLettreMotiv.Text.Split(new char[] { '\r' }, StringSplitOptions.None); 
       _documentBodyLines.AddRange(lines); 

       PrintDocument maLettreMotiv = new PrintDocument(); 
       //maLettreMotiv.BeginPrint += new EventHandler<BeginPrintEventArgs>(maLettreMotiv_BeginPrint); 
       //maLettreMotiv.EndPrint += new EventHandler<EndPrintEventArgs>(maLettreMotiv_EndPrint); 
       maLettreMotiv.PrintPage += new EventHandler<PrintPageEventArgs>(maLettreMotiv_PrintPage); 

       maLettreMotiv.Print("LettreMotivation_" + itmCandidatSelect.NomCandidat + "_" + itmCandidatSelect.PrenomCandidat); 

      } 

}

int _lineIndex; 
     List<string> _documentBodyLines; 

void maLettreMotiv_PrintPage(object sender, PrintPageEventArgs e) 
     { 
      PrintLettreMotivTemplate page = new PrintLettreMotivTemplate(); 
      page.SetHeaderAndFooterText("Lettre de motivation", ""); 
      int numberOfLinesAdded = 0; 
      while (_lineIndex < _documentBodyLines.Count) 
      { 
       page.AddLine(_documentBodyLines[_lineIndex]); 
       page.Measure(new Size(e.PrintableArea.Width, double.PositiveInfinity)); 
       if (page.DesiredSize.Height > e.PrintableArea.Height && numberOfLinesAdded > 1) 
       { 
        page.RemoveLastLine(); 
        e.HasMorePages = true; 
        break; 
       } 
       _lineIndex++; 
       numberOfLinesAdded++; 

      } 
      e.PageVisual = page; 
     } 

我的模板:

<UserControl x:Class="erecrutement_procclass.Views.PrintLettreMotivTemplate" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      Width="815" 
      Height="1024" 
      mc:Ignorable="d"  
      d:DesignHeight="300" 
      d:DesignWidth="400"> 
<Grid x:Name="documentRoot"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="25"/> 
     <RowDefinition /> 
     <RowDefinition Height="25"/> 
    </Grid.RowDefinitions> 
    <TextBlock x:Name="headerTextBlock" HorizontalAlignment="Center" /> 
    <TextBlock x:Name="bodyTextBlock" Grid.Row="1" TextWrapping="Wrap" Margin="5,5,5,5"/> 
    <TextBlock x:Name="footerTextBlock" HorizontalAlignment="Center" Grid.Row="2"/> 
</Grid> 


public partial class PrintLettreMotivTemplate : UserControl 
    { 
     public PrintLettreMotivTemplate() 
     { 
      InitializeComponent(); 
     } 

     public void SetHeaderAndFooterText(string header, string footer) 
     { 
      headerTextBlock.Text = header; 
      footerTextBlock.Text = footer; 
     } 


     public void AddLine(string line) 
     { 
      bodyTextBlock.Inlines.Add(line); 
      bodyTextBlock.Inlines.Add(new LineBreak()); 
     } 



     public void RemoveLastLine() 
     { 
      for (int index = 0; index < 2; index++) 
      { 
       bodyTextBlock.Inlines.RemoveAt(bodyTextBlock.Inlines.Count - 1); 
      } 
     } 
    } 

但是,当它应该在一个页面,它停了下来,只有我有一页,并且一个字母丢失。

我该如何解决这个问题?

谢谢。

+0

对于多页书是指http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument(v=vs.71).aspx,monDocument.Print( “Fiche_” );和maLettreMotiv.Print(“LettreMotivation_”);应该在设置模板之后。 – Mullaly 2013-02-21 09:29:51

+0

我的视觉工作室无法找到组装System.Drawing。但是我使用.NET 4,为什么我找不到它? – 2013-02-21 10:02:51

回答

3

原因是这样的。在PrintLettreMotivTemplate.xaml

<UserControl x:Class="DelmePrint.PrintLettreMotivTemplate" 
      Width="815" 
      Height="1024" 
      > 
    <Grid x:Name="documentRoot"> 
     ... 
    </Grid> 
</UserControl> 

你设置了可打印区域的高度,以1024当我通过代码加强,

/* DesiredSize always (793, 1024) 
    PrintableArea always (793, 1122) */ 
if (page.DesiredSize.Height > e.PrintableArea.Height) 

因此,代码保持添加几行的中间部分页。

为了解决这个问题,就需要在PrintLettreMotivTemplate改变高度来了minHeight。这样,当行数太多时,字母会溢出。

<UserControl x:Class="DelmePrint.PrintLettreMotivTemplate" 
      Width="815" 
      MinHeight="1024" 
      > 
    <Grid x:Name="documentRoot"> 
     ... 
    </Grid> 
</UserControl> 
+0

非常感谢你。只为失踪闵之前de高度! – 2013-03-04 08:02:57

2

下面财产尝试;

maLettreMotiv.PrintPage += (s, args) => 
      { 
       Candidat monCandidat = this.itmCandidatSelect; 

       PrintLettreMotivTemplate monTemplatePrint = PrintableAreaLettreMotiv.printTemplateLettreMotiv(monCandidat); 
       monTemplatePrint.Width = args.PrintableArea.Width; 
       monTemplatePrint.Height = args.PrintableArea.Height; 
       args.HasMorePages = true; 
       args.PageVisual = monTemplatePrint; 
      }; 
+0

似乎开始打印,但一个巨大的事情:当我生成PDF格式的PDF创作者,3页的文档工作变成了200页以上(并且非常沉重的文档)。这怎么可能? – 2013-02-21 12:58:35

+0

他还在每个页面中重复使用我的标题的Textbloc。有3或4页工作,我有一个65页的pdf创建者。不明白为什么。我的xaml写错了吗? – 2013-02-21 13:09:03

+0

这是Silverlight向打印机发送大量数据的可怕行为。 ın你的情况下,你可能需要专门计算措施。这[视频](http://msdn.microsoft.com/en-us/silverlight4trainingcourse_sl4lob_06_02_multipageprinting.aspx)可能会有帮助 – scetiner 2013-02-21 13:26:49