打印不在列表中的页面

时间:2018-10-29 03:51:20

标签: c# printdocument

我有一个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++;

}

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

非常感谢!

1 个答案:

答案 0 :(得分: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++;
}