Silent printing from WPF - c#

I would like to disable this printing dialog:
What should I do to remove this dialog?
var dlg = new PrintDialog();
dlg.PageRangeSelection = PageRangeSelection.AllPages;
dlg.UserPageRangeEnabled = false;
var doc = new FlowDocument();
doc.PageWidth = 275;
doc.FontSize = 18;
doc.TextAlignment = TextAlignment.Center;
doc.Blocks.Add(new Paragraph(new Run("nXXXXXXXXXXXX")));
dlg.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, "Receipt");

You can use PrintDocument class instead of PrintDialog.See the Microsoft's implementation
try
{
streamToPrint = new StreamReader
("C:\\My Documents\\MyFile.txt");
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

Related

Problems previewing documents in Windows Form Application

I'm trying to create a WPF which can preview documents(word,pdf,png,jpg) located on my HDD. I've tried using printPreviewControl but I cant get it working.
Code:
public Form1()
{
InitializeComponent();
initComponents();
}
public void initComponents()
{
try
{
string fname = "C:\\Users\\Gebruiker\\Desktop\\PDF en DOC\\test.pdf";
streamToPrint = new StreamReader(fname, Encoding.UTF8);
try
{
PrintDocument pd = new PrintDocument();
pd = new PrintDocument();
pd.DocumentName = fname;
//Get responsive width and height.
System.Drawing.Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea;
int height = workingRectangle.Height / 100 * 85;
int width = workingRectangle.Width / 100 * 75;
//Settings printPreviewControl
printPreviewControl1.ClientSize = new System.Drawing.Size(width, height);
printPreviewControl1.Location = new System.Drawing.Point(0, 0);
printPreviewControl1.Document = pd;
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
*It says its generating the preview, but stays empty, watch image below:
If you guys have any other solution, I'm open to try.
Thanks

How to print word document using PrintDocument class

My requirement is to print a Word document using c#.
Since Interop Word is not recommended to use at server side, I would like to print Word file using PrintDocument class.
So, how to print Word document using c#?
I tried the below code, but it printed out 2 blank pages:
PrintDocument printDoc = new PrintDocument();
printDoc.DocumentName = "E:\\WordPrint\\Output\\TEST.docx";
printDoc.DefaultPageSettings.PrinterSettings.PrinterName = "Bullzip PDF Printer";
printDoc.DefaultPageSettings.PrinterSettings.Copies = 2;
printDoc.Print();
try
{
streamToPrint = new StreamReader ("C:\\My Documents\\MyFile.txt");
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler (this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
You can find more information on the msdn site:
https://msdn.microsoft.com/de-de/library/system.drawing.printing.printdocument(v=vs.110).aspx

Tesseract Init() method C3

I am using Tesseract 2 with c# .net 4.5
Whenever code reaches to OCR.InIt() method, it comes out of code and program stops execution.
Even Catch block does not hold the code.
Please let me know how to check the problem.
Bitmap image = new Bitmap(ofd_OpenPhoto.FileName);
tessnet2.Tesseract ocr = new tessnet2.Tesseract();
ocr.Init("C:\\tessnet2\\tesseract-ocr\\tessdata", "eng", false);
Please let me know, how to use Init() method, Should I remove null?
private void btn_Browse_Click(object sender, EventArgs e)
{
ofd_OpenPhoto.Multiselect = false;
ofd_OpenPhoto.RestoreDirectory = true;
ofd_OpenPhoto.SupportMultiDottedExtensions = false;
ofd_OpenPhoto.FileName = "";
ofd_OpenPhoto.Title = "Select Photo";
ofd_OpenPhoto.Filter = "Photo Files (*.jpg)|*.jpg";
DialogResult result = ofd_OpenPhoto.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
string file_name;
if (ofd_OpenPhoto.FileName != null)
{
try
{
file_name = ofd_OpenPhoto.FileName.Substring(ofd_OpenPhoto.FileName.LastIndexOf("\\") + 1);
txt_PhotoPath.Text = file_name.Substring(0, file_name.LastIndexOf("."));
Emgu.CV.Image<Bgr, Byte> img_o = new Emgu.CV.Image<Bgr, byte>(ofd_OpenPhoto.FileName);
pb_PhotoViewer_O.Image = img_o.ToBitmap();
if (pb_PhotoViewer_O.Image != null)
{
try
{
Bitmap image = new Bitmap(ofd_OpenPhoto.FileName);
tessnet2.Tesseract ocr = new tessnet2.Tesseract();
ocr.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
ocr.Init(#"C:\\Program Files (x86)\\Tesseract-OCR", "eng", false);
List<tessnet2.Word> result1 = ocr.DoOCR(image, Rectangle.Empty);
txt_ExtractedNumber.Text = result1.ToString();
}
catch (Exception ex)
{
}
}
}
catch (Exception ex)
{
//e.Message();
}
}
}
}
This is my code.
Thanks.
If you write ocr.Init("C:\\tessnet2\\tesseract-ocr\\tessdata", "eng", false);
then at C:\tessnet2\tesseract-ocr\tessdata directory you must have next files:
eng.DangAmbigs
eng.freq-dawg
eng.inttemp
eng.normproto
eng.pffmtable
eng.unicharset
eng.user-words
eng.word-dawg
Also you must check that an Windows system environment variable ( TESSDATA_PREFIX ) is deleted

How do I trigger the PrintPreviewDialog?

using (PrintDialog printDialog1 = new PrintDialog())
{
if (printDialog1.ShowDialog() == DialogResult.OK)
{
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(saveAs.ToString());
info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
info.Verb = "PrintTo";
System.Diagnostics.Process.Start(info);
}
}
The above code works fine. I just don't know how to change the code so that I can preview the Word document first.
Okay, so I worked on this last night after getting home and I believe I figured it out. It's NOT perfect but, it does get you moving in the right direction. BTW, I created a simple WinForms app for this, and you will need to edit the code to suit your needs.
The code:
namespace WindowsFormsApplication1
{
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = doc;
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.PrintPage);
dlg.ShowDialog();
}
private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
string fileName = #"C:\Users\brmoore\Desktop\New Text Document.txt";
StreamReader sr = new StreamReader(fileName);
string thisIsATest = sr.ReadToEnd();
sr.Close();
System.Drawing.Font printFont = new System.Drawing.Font("Arial", 14);
e.Graphics.DrawString(thisIsATest, printFont, Brushes.Black, 100, 100);
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
}
}

How to write a Text file from this Output

I have the following code so that I can search directories to find files. Now I want to add a way for users to Save the output to a text file?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace RecursiveSearchCS
{
public class Form1 : System.Windows.Forms.Form
{
internal System.Windows.Forms.Button btnSearch;
internal System.Windows.Forms.TextBox txtFile;
internal System.Windows.Forms.Label lblFile;
internal System.Windows.Forms.Label lblDirectory;
internal System.Windows.Forms.ListBox lstFilesFound;
internal System.Windows.Forms.ComboBox cboDirectory;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.btnSearch = new System.Windows.Forms.Button();
this.txtFile = new System.Windows.Forms.TextBox();
this.lblFile = new System.Windows.Forms.Label();
this.lblDirectory = new System.Windows.Forms.Label();
this.lstFilesFound = new System.Windows.Forms.ListBox();
this.cboDirectory = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// btnSearch
//
this.btnSearch.Location = new System.Drawing.Point(608, 248);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(75, 23);
this.btnSearch.TabIndex = 0;
this.btnSearch.Text = "Search";
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// txtFile
//
this.txtFile.Location = new System.Drawing.Point(8, 40);
this.txtFile.Name = "txtFile";
this.txtFile.Size = new System.Drawing.Size(120, 20);
this.txtFile.TabIndex = 4;
this.txtFile.Text = "*.*";
//
// lblFile
//
this.lblFile.Location = new System.Drawing.Point(8, 16);
this.lblFile.Name = "lblFile";
this.lblFile.Size = new System.Drawing.Size(144, 16);
this.lblFile.TabIndex = 5;
this.lblFile.Text = "Search for files containing:";
//
// lblDirectory
//
this.lblDirectory.Location = new System.Drawing.Point(8, 96);
this.lblDirectory.Name = "lblDirectory";
this.lblDirectory.Size = new System.Drawing.Size(120, 23);
this.lblDirectory.TabIndex = 3;
this.lblDirectory.Text = "Look In:";
//
// lstFilesFound
//
this.lstFilesFound.Location = new System.Drawing.Point(152, 8);
this.lstFilesFound.Name = "lstFilesFound";
this.lstFilesFound.Size = new System.Drawing.Size(528, 225);
this.lstFilesFound.TabIndex = 1;
//
// cboDirectory
//
this.cboDirectory.DropDownWidth = 112;
this.cboDirectory.Location = new System.Drawing.Point(8, 128);
this.cboDirectory.Name = "cboDirectory";
this.cboDirectory.Size = new System.Drawing.Size(120, 21);
this.cboDirectory.TabIndex = 2;
this.cboDirectory.Text = "ComboBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(688, 277);
this.Controls.Add(this.btnSearch);
this.Controls.Add(this.txtFile);
this.Controls.Add(this.lblFile);
this.Controls.Add(this.lblDirectory);
this.Controls.Add(this.lstFilesFound);
this.Controls.Add(this.cboDirectory);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// The main entry point for the application
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
lstFilesFound.Items.Clear();
txtFile.Enabled = false;
cboDirectory.Enabled = false;
btnSearch.Text = "Searching...";
this.Cursor = Cursors.WaitCursor;
Application.DoEvents();
DirSearch(cboDirectory.Text);
btnSearch.Text = "Search";
this.Cursor = Cursors.Default;
txtFile.Enabled = true;
cboDirectory.Enabled = true;
}
private void Form1_Load(object sender, System.EventArgs e)
{
cboDirectory.Items.Clear();
foreach (string s in Directory.GetLogicalDrives())
{
cboDirectory.Items.Add(s);
}
cboDirectory.Text = "C:\\";
}
void DirSearch(string sDir)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, txtFile.Text))
{
lstFilesFound.Items.Add(f);
}
DirSearch(d);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
}
}
Here is also a screenshot of the Application Open:
Open Application Image
I am going to add a save button which saves to a specific location when clicked, how would I go about doing this.
If I am understanding it correctly, use a simple I/O operation.
using(StreamWriter writer = new StreamWriter("debug.txt", true))
{
foreach (string item in lstFilesFound.Items)
{
writer.WriteLine(item.ToString());
}
}
Few extra pointers:
In DirSearch, create a variable for Directory.GetDirectories(sDir). Your present code is causing this thing to calculate in every loop. Look for some more similar refactoring code in other areas.
var dirs = Directory.GetDirectories(sDir);
foreach (string d in dirs)
{
var files = Directory.GetFiles(d, txtFile.Text);
foreach (string f in files)
{
lstFilesFound.Items.Add(f);
}
DirSearch(d);
}
Hope it helps.
I'm assuming it's the list of found files you're wanting to save to a text file, how about this on your save event (rough code so not extensively tested)?
using (FileStream fs = new FileStream("c:\\files.txt", FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (string item in lstFilesFound.Items)
{
sw.WriteLine(item);
}
}
}

Categories

Resources