【问题标题】:Print pages NOT in the List打印不在列表中的页面
【发布时间】:2018-10-29 03:51:20
【问题描述】:

我有一个int 列表,其中包含我不想要打印的页面。

我们称之为skipPages。

当我尝试将实际打印部分放入 if(skipPages.IndexOf(currentPage)<0) 语句中时,它会打印出空白页。

public void printPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    List<int> skipPages = new List<int> { 2, 5, 6 };
    if(currentPage<totalPage) e.HasMorePages = true;
    else e.HasMorePages = false;

    if(skipPages.IndexOf(currentPage)<0)
    {
        e.Graphics.DrawString(
            currentPage.ToString(),
            new Font("Times New Roman",12),
            new SolidBrush(Color.Black),
            new Point(10,10));
    }
    currentPage++;

}

当我尝试将e.HasMorePages = true 放入其中时,它只会在第一个跳过页面之后停止所有内容。

public void printPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    List<int> skipPages = new List<int> { 2, 5, 6 };
    if(currentPage<totalPage && skipPages.IndexOf(currentPage)<0) e.HasMorePages = true;
    else e.HasMorePages = false;

    e.Graphics.DrawString(
        currentPage.ToString(),
        new Font("Times New Roman",12),
        new SolidBrush(Color.Black),
        new Point(10,10));
    currentPage++;

}

请教我如何正确设置它!?

非常感谢!!!

【问题讨论】:

  • 嗨 PiggyChu515,欢迎来到 StackOverflow。您能否发布相关代码,因为我们没有太多工作要做。
  • 我认为这个链接可能会有所帮助stackoverflow.com/questions/7761897/…
  • @Francis:我添加了代码,希望对您有所帮助!
  • @Alireza:该示例教我如何打印范围的页面,这不是我想要做的。我尝试跳过页,可能是第 2、5、6 页或第 4、7、12、14、15、19 页。不过还是谢谢!
  • 每个页面都会触发 PrintPage 事件。如果你不画任何东西,那么你会得到一个空白页。因此,您必须增加 currentPage 变量才能到达您想要打印的页面。这使得 e.HasMorePages 设置正确有点棘手,最好的方法是预先进行测试,从 BeginPrint 事件开始。

标签: c# printdocument


【解决方案1】:

每个页面都会触发 PrintPage 事件,因此您只需跳过列表中的 currentPage。

您还需要一种机制来检查最后几页是否在跳过列表中,以避免最后打印空白页。

List<int> skipPages = new List<int> { 2, 5, 6 };

private void printDocument1_BeginPrint(object sender, PrintEventArgs e)
{
    currentPage = 0;
}

public void printPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
    bool f = false;
    int c = currentPage + 1;

    //Mechanism to check for the last few pages.
    while(skipPages.IndexOf(c)>=0) c++;
    if(c>=totalPages) f=false;
    else f=true;

    while(skipPages.IndexOf(currentPage)>=0) currentPage++; //Actual skipping part.
    if(currentPage<totalPage-1) e.HasMorePages = f;
    else e.HasMorePages = false;
    e.Graphics.DrawString(
         currentPage.ToString(),
         new Font("Times New Roman",12),
         new SolidBrush(Color.Black),
         new Point(10,10));
    currentPage++;
}

【讨论】:

  • 您没有正确理解。想象一下当第一页或最后一页在 skipPages 列表中时会发生什么。 e.HasMorePages 分配需要在 while 循环之后完成,这样您就不会得到额外的空白页。而且您确实也需要 BeginPrint 事件,既可以将 currentPage 变量重置为 0,也可以将其推进到任何跳过的页面之外。所以 skipPages 变量不能是局部变量,把它移出方法。
  • 我更新了!请检查我是否做对了!?谢谢!
  • 我认为编辑 sn-p 可能更容易。完成。
  • 谢谢!但是如果把while循环放到BeginPrint里面,还需要放到printPage里面吗!?
猜你喜欢
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
相关资源
最近更新 更多