Feedback

C# - Print Txt - Text-Datei drucken

Veröffentlicht von am 8/2/2014
(0 Bewertungen)
Achtung: Es gibt das Problem, das zu lange Zeilen nicht einfach auf der nächsten Seite fortfahren, sondern abgeschnitten werden!!!
Ich versuche es zu beheben!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace DocumentPrint
{
    public class PrintTxt
    {
        public Font font;
        public string path;
        public PrintDocument pd;
        private StreamReader sr;

        public PrintTxt(string p)
        {
            path = p;
            font = new Font("Verdana", 11);
            pd = new PrintDocument();
        }

        public PrintTxt(string p, Font f)
        {
            path = p;
            font = f;
            pd = new PrintDocument();
        }
        
        public void Print()
        {
            try
            {
                sr = new StreamReader(path);
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
                pd.Print();
                if (sr != null)
                    sr.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool ShowDialog()
        {
            try
            {
                PrintDialog pdia = new PrintDialog();
                if (pdia.ShowDialog() == DialogResult.OK)
                {
                    pd.PrinterSettings = pdia.PrinterSettings;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void PrintTextFileHandler(object sender, PrintPageEventArgs ppe)
        {
            try
            {
                Graphics g = ppe.Graphics;
                float linesPerPage = 0;
                float yPos = 0;
                float leftMargin = ppe.MarginBounds.Left;
                float topMargin = ppe.MarginBounds.Top;
                string line = null;
                linesPerPage = ppe.MarginBounds.Height /
                font.GetHeight(g);
                for (int i = 0; i < linesPerPage && ((line = sr.ReadLine()) != null); i++)
                {
                    yPos = topMargin + (i *
                    font.GetHeight(g));
                    g.DrawString(line, font, Brushes.Black, leftMargin, yPos, new StringFormat());
                }
                if (line != null)
                {
                    ppe.HasMorePages = true;
                }
                else
                {
                    ppe.HasMorePages = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

Abgelegt unter print, drucken, text, datei, file, dialog.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!