How to print a text file content in .Net using c# - c#

I am having a text file with few lines in them. I just want to send this file to printer of my choice. Why is this so hard? MSDN and google search points me to use : https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument.print?view=dotnet-plat-ext-6.0
But when I use the above code in my VS Code it giving me errors when building because "PagePageEventArgs" is available only in System.Drawing.Common. However, I couldnt install that in my VS Code even through NuGet Package manager.
First of all codes in above lnik looks like it is designed for a windows form and it has too much details. I get so many other errors with all those codes. I am new to programming and these is overwhelming for me and I am stuck with for a week. Can anyone please tell me how to just print a text file (all the lines in text file) using printer of my choice.

If they are text files you could cheat and use Notepad:
var fileToPrint = "C:\\Junk\\Junk.txt";
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "notepad";
p.StartInfo.Arguments = $"/p {fileToPrint}";
p.StartInfo.UseShellExecute = false;
p.Start();
p.Dispose();
Console.WriteLine("FINISHED");
Based on code from: https://kimsereyblog.blogspot.com/2018/01/start-processes-from-c-in-dotnet-core.html
and Notepad command line options from: https://answers.microsoft.com/en-us/windows/forum/all/notepadexe-command-line-options/810760c1-a45a-4013-9544-1c1208e1b389?auth=1

Related

Use wget command to download multiple files at different locations

I want to download multiple files say www.google.com, yahoo.com and gmail.com at 3 different locations using wget. How should i go about it? Please help me out..
I am doing all this through c#:
ProcessStartInfo startInfo = new ProcessStartInfo("CMD.exe");
Process p = new Process();
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
p = Process.Start(startInfo);
p.StandardInput.WriteLine(#"wget --output-document=C:\1.xml xyz.com/a.xml");
p.StandardInput.WriteLine(#"wget --output-document=C:\2.xml xyz.com/b.xml");
p.StandardInput.WriteLine(#"wget --output-document=C:\3.xml xyz.com/c.xml");
p.StandardInput.WriteLine(#"EXIT");
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
p.Close();
This is not working. would like to know if there r any othe ways of downloading multiple files using wget..
If you're just talking about retrieving each file from a different location, but still doing it sequentially, you just change the URI in the wget command to point to a different location.
If you want concurrent downloads rather than sequential, you would have to start three separate processes and have them download one file each. These ptocesses could run side by side but I'd probably only consider this for large files (of which an XML file is probably not).
If you're having troubles getting the commands to run at all, the first thing I would do is ditch cmd.exe and its standard input. There's no reason why you can't have a process run wget directly. Or, if you really only want to start the one process, you could output them to a temporary file and use a single process cmd /c tempfile.cmd to run it.
However, there may be a totally different problem you're having unrelated to what you've shown, because that exact code with three echo statements in place of your wget ones runs fine, generating the correct output, at least in Visual C# Express 2010.
And, in fact, once I got my GnuWin32 wget on to the path, the following worked as well, getting real documents off the net and placing them in my top-level directory:
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
Process p = new Process();
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
p = Process.Start(startInfo);
p.StandardInput.WriteLine(
#"wget --output-document=c:\q1.txt http://www.ibm.com");
p.StandardInput.WriteLine(
#"wget --output-document=c:\q2.txt http://www.microsoft.com");
p.StandardInput.WriteLine(
#"wget --output-document=c:\q3.txt http://www.borland.com");
p.StandardInput.WriteLine(#"exit");
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
p.Close();
}
}
}
Here's the proof, the single window partway through the Microsoft download:
So, bottom line, what you have shown us is not inherently unworkable as evidenced by the image above. My only suggestion is to start looking around at other things such as the version of wget you're using, GnuWin32 or CygWin.
Now, things get interesting with larger files, as you've stated in one of your comments. If I change all three URIs to http://download.microsoft.com/download/5/F/C/5FC4F80C-242D-423B-9A11-9510A013152D/Dolphins.themepack, a file of 12,889,103 bytes, the code above hangs at about 18% of the first download (around the 2.3M mark).
However, if I change the commands so that they have >nul: 2>nul: on the end, the download goes through without issue, so I suspect it's most likely an issue with the way wget writes its output (without newlines). It also works fully if you don't use redirection on the output and error streams, which strengthens that assertion.
Well, first of all, you're on Windows. wget is part of the GNU Operating System. Unless you've installed a "clone" of wget for Windows, this is impossible. You are probably better off downloading the pages yourself, with something like the HTTPClient class.
But if you have a form of wget installed, what is not working? And how do you want it to work? Your question is not very detailed, you just ask how to go about it, and provide a seemingly fine solution.

Using pdflatex.exe to Convert TeX to PDF within a C#/WPF Application

Has anyone ever created a PDF document from a TeX document using pdflatex.exe in their C#/WPF application? I have my TeX document and I want to convert it to PDF and display it within the application, however, I'm unsure how to go about doing this and there's virtually nothing that I can find online about doing something like this. Does anyone know what the best way to do something like this is (convert a TeX document to PDF via pdflatex.exe within a C# application)?
Thanks a lot!
Here's the code I used. It assumes the source is in the same folder as the executable, and includes running BibTeX if you need it (just exclude the second process if needed).
string filename = "<your LaTeX source file>.tex";
Process p1 = new Process();
p1.StartInfo.FileName = "<your path to>\pdflatex.exe";
p1.StartInfo.Arguments = filename;
p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p1.StartInfo.RedirectStandardOutput = true;
p1.StartInfo.UseShellExecute = false;
Process p2 = new Process();
p2.StartInfo.FileName = "<your path to>\bibtex.exe";
p2.StartInfo.Arguments = Path.GetFileNameWithoutExtension(filename);
p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p2.StartInfo.RedirectStandardOutput = true;
p2.StartInfo.UseShellExecute = false;
p1.Start();
var output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();
p2.Start();
output = p2.StandardOutput.ReadToEnd();
p2.WaitForExit();
p1.Start();
output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();
p1.Start();
output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();
Yes, this could be cleaned up a little, and certainly without the call to BibTeX could just be called in a loop (3 times is the recommended number to make sure all the references are correct). Also, there's no exception handling, so you might want to add try / catch blocks around the process calls, etc.
I have never done that, but this is perfectly possible. You'll need to use System.Diagnostics.Process class to run pdflatex.exe
The rest is about choosing the way this should run. You probably have some options here:
If pdflatex supports writing output to "standard output" you can intercept that and do with the content whatever you want.
Another option is using some temporary folder to write the file to and subscribing to notifications about folder changes (asynchronous, you can run multiple instances of pdflatex) or simply waiting for the process to finish (synchronous, you can run only one instance of pdflatex at a time).

Get the output of a console program in my WinForms application (Specifically the output of C# compiler - CSC.EXE)

I decided to do something really simple -
A WinForms application with a text box for C# code and a button. When the button is clicked, I save the content of the text box to a temp .cs file, invoke the csc.exe (Process.Start()) with the file name as parameter so that it is compiled. This is assuming I set the PATH variables and everything.
When the csc.exe outputs syntax errors and stuff, how can I get it back and show it in another text box in my app?
NOTE My aim is not to just "complie C# code from within my app (in whichever way possible)".. it is to get the output of csc.exe
You need to redirect stdout and stderr.
It goes along the lines of:
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = command;
process.StartInfo.Arguments = arguments;
Then reading from process.StandardInput. I can't really remember if this is all you need.
Use csc.exe [parameters] >output.file to stream the console output of csc.exe to a text file, and then read the text file into your text box.
Use a ProcessStartInfo class to redirect the console output to a stream of yours, then display the stream content in your windows control.
You can also take a look a the CodeDom namespace to produce on the fly assemblies, but it depends on your goal and requirement.

Using iTextSharp to save to file the fonts used in a PDF file

This is pretty much a duplicate of this unanswered question, but hopefully someone in the know is watching now and can be helpful.
I'm looking for the ability have some .NET code extract the font embedded in a PDF to a font file. I'm currently using iTextSharp, but I'm open to other .NET libraries (e.g. PDFBox, PDF CLown, etc...). I'm able to iterate the information from BaseFont.GetDocumentFonts(), but I'm not clear on how to stream the font out to a font file.
Thanks, Kenny
#Highmastdon - it is actually really simple to get the font names, at least in iText/iTextSharp (pdfBox as well - but I don't have the code around right now) but in iTextSharp you would do the following:
PdfReader reader = new PdfReader(strFileName);
List<object[]> strFonts = BaseFont.GetDocumentFonts(reader);
And there it is, most libraries have support written in for a simple extraction of fonts (the names in any case).
I contributed a response before, but in the interests of adding solid examples to topics on this site (something I dreadfully needed three months ago) I will iterate through the solution I ended up using.
I downloaded MuPDF and went into the bin folder, retrieving the file mutool.exe. I then call this with a separate process in C#. It runs through pulling all of the fonts embedded in the PDF file and dumps them in the folder containing mutool.exe . Then it was just a matter of moving the fonts from there to the folder I wanted them in.
/// <summary>
/// Extract all fonts from PDF
/// </summary>
/// <param name="strPDFName"></param>
public static void ExtractAll(string strPDFName)
{
if (strMUTOOL != null && strFontFinal != null)
{
Process p = new Process();
p.StartInfo.FileName = strMUTOOL;
p.StartInfo.Arguments = "extract \"" + strPDFName + "\"";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WorkingDirectory = strMUTOOL.Replace("mutool.exe", "").Trim();
p.Start();
p.WaitForExit();
var standardError = p.StandardError.ReadToEnd();
var standardOutput = p.StandardOutput.ReadToEnd();
var exitCode = p.ExitCode;
}
}
As a bit of a heads up, most of these fonts are CFF files and you will need to convert them if you plan on using them. Also, as has been stated, using these fonts may constitute software piracy if these fonts are paid fonts. Finally, these fonts are usually only subsets and do not contain the complete glyph set - just the glyphs used in the PDF.
I didn't get an answer, but I did find several vendor-based solutions. The software from pdf-tools.com, pdfextract.exe works very well. Also the library from quickpdflibrary.com works very well too and is the vender we went with and so far very happy.

how to create command application like run command for vista

how to create application like window run command using C#. When i insert any command (for example: ipconfig) , this return result (for example: 192.168.1.1) on the textbox.
how to get windows command list?
how to get command result?
how to get installed application list on the machine?
(1) The command list will most likely come from whatever executables are found in your %PATH%. You can figure out your list by finding all .exe/.bat/other executable files in every folder specified by %PATH%. You may not even need to know which apps are available, the Process.Start method will find them for you. (see below)
(2) You can run a command-line tool programmatically using:
System.Diagnostics.Process.Start("notepad.exe"); // located using %PATH%
To capture the output, you have to redirect it like this:
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(#"ipconfig");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process myProcess;
myProcess = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = myProcess.StandardOutput; // Capture output
myProcess.WaitForExit(2000);
if (myProcess.HasExited)
{
string output = myOutput.ReadToEnd();
Console.WriteLine(output);
}
(3) Probably the same answer as 1
Create a Windows Forms application using the wizard. Draw a text box and a button. Add a Click handler to the button which takes the contents of the text box and launches a process. Use the Process class. That class also has a StandardOutput property that you can read the output from so you can put it into the text box.
You may find that to use many Command Prompt commands, you need to type CMD /C in front, because they aren't separate programs from the command interpreter.
As for discovering a list of commands, that's not generally possible. A command is just a program (or a feature of the CMD command interpreter). You could search the hard drive for .exe files, but then many of them wouldn't be suitable as "commands".

Categories

Resources