-1

I have been searching for long time to print formatted text and images which are in panel(PanelContain) but I have not get yet to print formatted text and images.Please tell me which text box support early I had used Rich text box but it does not convert in bitmap so I was getting blank text box. Please help me.It is very very important for my project.

This is my code:

private void PrintPanel()
{
    System.Drawing.Printing.PrintDocument doc = new PrintDocument();
    doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
    doc.Print();
}

private void doc_PrintPage(object sender, PrintPageEventArgs e)
{
    try
    {
        RichTxtRptBody.BorderStyle = BorderStyle.None;
        Bitmap bmp = new Bitmap(PanelContain.Width,  
        PanelContain.Height);
        float tgtWidthMM = 210;  //A4 paper size
        float tgtHeightMM = 297;
        float tgtWidthInches = tgtWidthMM / 25.4f;
        float tgtHeightInches = tgtHeightMM / 25.4f;
        float srcWidthPx = bmp.Width;
        float srcHeightPx = bmp.Height;
        float dpiX = srcWidthPx / tgtWidthInches;
        float dpiY = srcHeightPx / tgtHeightInches;
        bmp.SetResolution(dpiX, dpiY);

        PanelContain.DrawToBitmap(bmp, PanelContain.ClientRectangle);
        e.Graphics.InterpolationMode = 
        InterpolationMode.HighQualityBicubic;
        e.Graphics.PageUnit = GraphicsUnit.Millimeter;
        e.Graphics.DrawImage(bmp, 3, 1, tgtWidthMM, tgtHeightMM-24);

    }
    catch (Exception ex)
    {

    }
}

private void toolStripBtnPrint_Click(object sender, EventArgs e)
{
    try
    {
        Img = null;
        PrintDocument doc = new PrintDocument();
        PrintDialog dlgSettings = new PrintDialog();
        dlgSettings.Document = doc;
        if (dlgSettings.ShowDialog() == DialogResult.OK)
        {
            PrintPanel();
        }
    }
    catch (Exception ex)
    {

    }
}

I have also given a snapshot of my panel:

PAPPU KUMAR
  • 7
  • 1
  • 9
  • What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! – TaW Aug 21 '18 at 12:20
  • Did you see [this](https://stackoverflow.com/questions/7399842/c-sharp-printing-richtextbox) ? Or, to include images [this](https://stackoverflow.com/questions/4974276/richtextbox-drawtobitmap-does-not-draw-containing-text)? - In general this is not supported. – TaW Aug 21 '18 at 15:28
  • winform,C# .Do you have any idea about this print,Please help me. – PAPPU KUMAR Aug 22 '18 at 06:17
  • Its is printing a Blank page. – PAPPU KUMAR Aug 22 '18 at 06:24
  • DrawToBitmap doesn't work with RTB. You can get a regular screenshot instead: `using (Graphics gr = Graphics.FromImage(bmp)) gr.CopyFromScreen(PanelContain.PointToScreen(Point.Empty), Point.Empty, PanelContain.Size);` - Of course the quality will still be bad, as it will only have the screen resolution. Both taken from [here](https://stackoverflow.com/questions/4974276/richtextbox-drawtobitmap-does-not-draw-containing-text) – TaW Aug 22 '18 at 06:37
  • If I use CopyFromScreen then it will not print text which is down the scrollbar.Thank you for your valuable time ,please help me more. – PAPPU KUMAR Aug 22 '18 at 06:56

2 Answers2

0

You want to print a rich text box content with formatting

Try this :

Here eintragRichTextBox is a RichTextBox

private void druckenPictureBox_Click(object sender, EventArgs e)
{
    PrintDialog printDialog = new PrintDialog();
    PrintDocument documentToPrint = new PrintDocument();
    printDialog.Document = documentToPrint;

    if (printDialog.ShowDialog() == DialogResult.OK)
    {
        StringReader reader = new StringReader(eintragRichTextBox.Text);
        documentToPrint.PrintPage += new PrintPageEventHandler(DocumentToPrint_PrintPage);
        documentToPrint.Print();
    }
}

private void DocumentToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    StringReader reader = new StringReader(eintragRichTextBox.Text);
    float LinesPerPage = 0;
    float YPosition = 0;
    int Count = 0;
    float LeftMargin = e.MarginBounds.Left;
    float TopMargin = e.MarginBounds.Top;
    string Line = null;
    Font PrintFont = this.eintragRichTextBox.Font;
    SolidBrush PrintBrush = new SolidBrush(Color.Black);

    LinesPerPage = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics);

    while (Count < LinesPerPage && ((Line = reader.ReadLine()) != null))
    {
        YPosition = TopMargin + (Count * PrintFont.GetHeight(e.Graphics));
        e.Graphics.DrawString(Line, PrintFont, PrintBrush, LeftMargin, YPosition, new StringFormat());
        Count++;
    }

    if (Line != null)
    {
        e.HasMorePages = true;
    }
    else
    {
        e.HasMorePages = false;
    }
    PrintBrush.Dispose();
}
saAction
  • 2,035
  • 1
  • 13
  • 18
  • 2
    But your code Does not print formatted text or copy past text.It is just simply printing the text .Thanks for your help. – PAPPU KUMAR Aug 21 '18 at 13:53
0

YOU can use webbrowsercontrol populate dynamically and then you can print easily as you want to print with full formatting option as html code can help you to give layout also.