Printing from richtextbox and keeping formatting

时间:2019-01-18 18:25:03

标签: c# winforms richtextbox office-interop

I am having problems printing to a printer or a PDF from a richtextbox. It successfully prints all characters, but does not keep it formatted correctly (specifically losing tables).

Example of source:

Example of source

Example of printed output:

Example of printed output

I believe it is due to ".Text" only sending the text (characters) to be printed and not the complete format but I do not know another way to refer to the contents of the richtextbox. ".Rtf" does not seem to help with the formatting either.

The key line of code where I think it is going wrong:

e.Graphics.DrawString(richTextBox1.Text, new Font("Times New Romans", 14, FontStyle.Bold), Brushes.Black, new PointF(100, 100));

Whole code in case something else has been done wrong, the loading seems to work perfectly fine as it looks completely correct when displayed.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;


namespace Print
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

            Microsoft.Office.Interop.Word.Application wordObject = new Microsoft.Office.Interop.Word.Application();
            object File = (@"C:\Users\Jacob\Documents\SomethingHere2.docx"); //this is the path
            object nullobject = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application wordobject = new Microsoft.Office.Interop.Word.Application();
            wordobject.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; Microsoft.Office.Interop.Word._Document docs = wordObject.Documents.Open(ref File, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject); docs.ActiveWindow.Selection.WholeStory();
            docs.ActiveWindow.Selection.Copy();
            this.richTextBox1.Paste();
            docs.Close(ref nullobject, ref nullobject, ref nullobject);
            wordobject.Quit(ref nullobject, ref nullobject, ref nullobject);
        }

        private void button3_Click(object sender, EventArgs e)
        {

            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument1.Print();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawString(richTextBox1.Text, new Font("Times New Romans", 14, FontStyle.Bold), Brushes.Black, new PointF(100, 100));
        }
    }
}

0 个答案:

没有答案