Hope this is an easy one.
I'm writing this WPF software with VS2017, I need to print the visual to an .xps file at some point and I managed to get this working like this:
private void cmdPrintClick(object sender, RoutedEventArgs e)
{
PrintDialog print = new PrintDialog();
print.PrintQueue = new PrintQueue(new PrintServer(), "XPS"); //XPS is the Microsoft XPS Document Writer
print.PrintVisual(myWindow, "Window"); //myWindow is the visual I want to print
}
This is working just fine.
What I am trying to achieve is to give progmatically the path and the filename, no need to show any dialog.
Thanks everyone for helping out.
Related
I need to disable Microsoft Word start screen programatically using C# in Visual Studio. I don't want to do it manually like this .
Anyone can help ?
I found a good workaround for it. It is simply to open a new blank document on startup
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
if (Application.Documents.Count == 0)
{
Application.Documents.Add();
}
}
I created an app with VS2017 C# WinForms, created a report for a table inside with RDLC reportviewer, the report is working well, I want to print this report directly and send it to the printer without popping up print preview dialog.
Please be informed that I googled this issue and searched this site as well and I couldn't find a solution.
Tried to use the MSDN topic (Walkthrough: Printing a Local Report without Preview) but still no luck.
Here is my button which runs several queries including opening a form (voucher) containing an rdlc reportviewer which loads data to show up in the report:
private void button12_Click(object sender, EventArgs e)
{
SoundPlayer player = new SoundPlayer(Properties.Resources.switch_8);
player.Play();
if (string.IsNullOrWhiteSpace(textBox1.Text) || textBox1.Text == "0")
{
MessageBox.Show("Please select an item!");
}
else
{
MOVE_TO_SOLD();
UPDATE_RECEIPT();
LOAD_DATA();
CLEAR_TEMP();
CHECK_FOR_REC_ID();
GET_NEW_REC_ID();
LOAD_DATA();
txtdiscount.Text = "0";
textBox5.Text = "0";
recowner.Text = recnum.Text;
voucher vr = new voucher();
vr.ShowDialog();
}
}
Thank You!
This Walkthrough will be very helpful for your problem. Please check it out, and if this answer was helpful, mark it as an answer.
In my C# Windows application I want to upload a pdf file but in my toolbox I cannot find a FileUpload control.
How can I go about and uploading a pdf file in a C# windows application.?
regards
After you put a OpenFileDialog control on your form, let's say that you click a button and:
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
//Do whatever you want
//openFileDialog1.FileName .....
}
}
it goes something like this :-)
You can use OpenFileDialog to get the filename of the file you need and then .NET File object to read data from the file. You might need a control being able to display a PDF file. Please read the following:
Viewing PDF in Windows forms using C#
I have written a Label making program for work. It uses an opendialog to pull in the data. Splits it up and put it in tables. No issues there. Setting up the the FixedDocument to print the user control labels as well as the class that stores the Fixed Document Data all work great.
My only issue is I can't stand the restrictions on a WPF document when it comes to text searching and print control. I have gone with a printpreviewdialog but this does not use FixedDocument.
I am needing to know if it can be converted with a simple bit of code or if i Have to rewrite my entire class and go back to the drawing that printpreviewdialog uses.
The code for the call is below.
private void button3_Click(object sender, EventArgs e)
{
var avery = new Avery5160();
DataTable data = (DataTable)dataGridView1.DataSource;
var A5160 = avery.CreateDocument(data);
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = A5160;
ppd.ShowDialog();
}
The error is :
Cannot implicitly convert type 'System.Windows.Documents.FixedDocument' to
'System.Drawing.Printing.PrintDocument'
Thanks for any assistance.
I am not so sure there is a simple conversion. A simple (if not a little time-consuming) method would be to create a preview dialog with a document viewer to simulate what you would be seeing. Then printing using the FixedDocument's DocumentPaginator and a regular PrintDialog.
The answer to this question shows how to do it by creating an XpsDocument that is then used with a custom PrintPreview class.
I'm currently attempting to print a document from WPF. I'm using the web browser because it contains an active x control which has the autodesk plugin which I need to view the document.
I'm aware that WPF doesn't directly support web browser but I've just integrated the Windows Forms library for this. I've managed to get the code running and even printing, however the document that prints is blank.
I'm not sure if it could possibly be a conflict between the Windows Forms library and WPF; I'm navigating to the document and only printing once it's loaded with no errors thrown.
Here's the code I'm using:
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.WebBrowser w = new System.Windows.Forms.WebBrowser();
Uri uri = new Uri("C:\\BOS-BD-4518-000.dwg.dwf");
w.Navigate(uri);
w.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(w_DocumentCompleted);
}
void w_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
System.Windows.Forms.WebBrowser w = (System.Windows.Forms.WebBrowser)sender;
w.Print();
}
One possible hitch could be that the active x control is not being allowed to be load, does anyone know how to force the control to be initialised.
Does anyone have any ideas about how to solve this or another method of printing an autodesk (.dwf) document
Thanks in advance,
SumGuy
Not really an answer of sorts but a solution if anyone out does want to print a .dwf file. Don't, use the new form .dwfx. This is the new file type Autodesk are switching too and its actually a form of XPS which makes things quite easy. You can load it into a web browser without needing active x OR (this is the better way) use the XPS libraries in visual studio because it can be loaded very simply into an XPS viewer.
The code I eventually used to print the dreaded file is below:
private PrintQueue printQueue;
PrintDialog pDialog = new PrintDialog();
pDialog.PageRangeSelection = PageRangeSelection.AllPages;
pDialog.UserPageRangeEnabled = true;
if (pDialog.ShowDialog() == true)
PrintSystemJobInfo xpsPrintJob = printQueue.AddJob(v.FileName, v.FilePath, false);
How easy's that??? There are loads of other ways of doing it using XPS. You can basically just use the dwfx file as an XPS document