Due to circumstances beyond my control, I'm replacing all of our Crystal Reports with home-built XML reports, which are working beautifully. For most of the reports that pop up a Crystal Reports viewer, the following code opens them nicely in IE, transforming it to HTML via an XSLT stylesheet.
ProcessStartInfo psi = new ProcessStartInfo(reportFilename)
{
UseShellExecute = true
};
using (Process p = new Process {StartInfo = psi})
{
p.Start();
}
The problem is that some of the reports just print directly to a printer, never showing the report to the user, which works fine in CR. I can't figure out how to do this using the code above.
I'd prefer not to specifically launch an IE process if possible, but I am guaranteed that they're running Windows, so that's not a hard requirement. Also, will printing directly in this manner transform the XML into HTML via the XSL and print that, or just print the actual XML text?
EDIT: I've already tried adding:
Verb = "Print"
to the ProcessStartInfo object, but that winds up with an exception being thrown saying:
"No application is associated with the specified file for this operation"
EDIT AGAIN: Specifying IE as the exe to launch loaded the XML again just fine, but does not offer a "print" action. Adding "window.print()" in a JavaScript block works, but requires manually clicking the print button after allowing the script to run, because IE blocks it.
EDIT THE THIRD: My boss told me not to worry about it, that they can print from IE. I still want to figure this out. I've tried the command line "print.exe", but that only prints the raw XML to the printer. Tried XslCompiledTransform with a PrintDocument, but that's not what I'm looking for, either.
Figured it out, finally. I just created an invisible WebBrowser control that does the IE rendering, and on DocumentCompleted, call its Print() method. Worked like a charm using the default printer settings.
private static void PrintReport(string reportFilename)
{
WebBrowser browser = new WebBrowser();
browser.DocumentCompleted += browser_DocumentCompleted;
browser.Navigate(reportFilename);
}
private static void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
if (null == browser)
{
return;
}
browser.Print();
browser.Dispose();
}
Related
I need help for a little IE Plugin (Browser Helper Object).
What the Plugin should do:
If the user clicks on a link with pdf behind, the plugin should call an exe file installed on the computer. The exe file would check the PDF and open the default pdf application or a special one.
What i have done:
My BHO starts with my IE. I used this code for the beginning:
http://www.codeproject.com/Articles/19971/How-to-attach-to-Browser-Helper-Object-BHO-with-C
I disabled all adobe plugins so the ie download window shows up. Whit this code i can parse the html body add html markup etc. but thats not what i'm trying to do...
My Problem:
I don't know how to grab the pdf. If i call directly a pdf download link the cast of the site object into a InternetExplorer or WebBrowser Object fails.
public int SetSite(object site)
{
if (site != null)
{
ieInstance = site as InternetExplorer;
ieInstance.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
else if (ieInstance != null)
{
ieInstance.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
return 0;
}
Document = '((SHDocVw.InternetExplorer)(ieInstance)).Document' threw an exception of type 'System.Runtime.InteropServices.COMException'
Could someone tell me, how i can grab the pdf befor the download window in the IE appears? I know there is a Event before download, but also this event don't help me.
What are you trying to achieve more broadly?
You can't grab a HTML document instance when the browser is showing a PDF file because there isn't one-- the PDF object is loaded instead of the HTML-rendering object.
Depending on what you're trying to achieve, there are likely better ways to accomplish it (most likely by registering a MIME Filter for the application/pdf MIME type).
I have looked and failed to find a solution to my problem. I have developed an Add-in ribbon for Word 2007 which provides an additional set of load and save functions, to allow users to load and save documents from a bespoke system.
I have most of it working - when a user requests a file to be opened, it is downloaded and saved to the AppData folder, and then opened. However, the problem I am having is that if the user for example opens Word and uses this new 'load' function, the blank Word document remains, and Word opens the new document quite happily, but it does not get the focus.
(I'm on Windows 7 and it creates a second 'W' icon in the task bar for the new document, but it doesn't switch to it in the same way that Word would if I'd used the normal 'Open' route.)
I have tried (as a result of suggestions found elsewhere on here) either setting the 'visible' attribute to true, and calling doc.Activate() but neither does what I need. What am I missing? The code I'm using to open the file is below:
private void OK_Click(object sender, EventArgs e)
{
this.Close();
FES.FESServices wService = new FES.FESServices();
int request_id = wService.SubmitRequestFromAddIn(username, password, "RETR", "", textBox1.Text, "", "");
FES.FileRequestResponse response = wService.GetFileMembersFromAddIn(username, password, request_id);
if (response.ResponseType == "RETR")
{
byte[] data = wService.GetBytesForFilename(response.ResponseValue);
//MessageBox.Show("Loaded data for file...");
//MessageBox.Show(Application.UserAppDataPath);
FileStream fs = new FileStream(Application.UserAppDataPath + "\\" + response.ResponseValue.Substring(6).Split('~')[0], FileMode.Create, FileAccess.Write);
fs.Write(data, 0, (int)data.Length);
fs.Close();
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.Documents.Open(
Application.UserAppDataPath + "\\" + response.ResponseValue.Substring(6).Split('~')[0], Visible:true
);
doc.Activate();
}
}
(I have included this.Close() as the function loading the document is held within a modal dialog box, and without closing it first, Word throws an exception about switching documents with a dialog box open).
Any help gratefully received!
Running this code whilst the modal dialog is showing is interfering with window activation.
I'm not sure exactly what the mechanism is for this interference, but the fix is simple enough. Move the code outside the dialog. Execute this code immediately after the call to ShowDialog returns.
I am making one application where user will print invoices which I am displaying using Crystal Report.
The user showed me his current application made using ForPro. In that application, under Printer Options form, one can see all the printers currently installed and the user could select default printer. When the invoice is made, the user presses the print button, then there is one dialog asking for no. of copies. When it's entered, the invoice gets printed directly, without any Print Dialog Box. If the user wants to change the printer again he/she will change it in the Printer Option form.
I want to know if similar thing is possible in Crystal Report and need guidance on how to approach for it.
Take a look at the ReportDocument.PrintToPrinter SAP Docs or MSDN Docs for how to specify the PrinterName and then Print using the ReportDocument object.
If you can try and get away from how the FoxPro app UI for printer selection. Instead use the standard print dialog box to select the printer.
You should note that if you don't set the PrinterName before sending the report to the printer it will use the default on the crystal file. Not to be confused with the user's OS default printer.
Here's an example of showing the PrintDialog settings some parameters using the SetParameterValue method and then sending the report document to a printer
// Note: untested
var dialog = new PrintDialog();
Nullable<bool> print = dialog.ShowDialog();
if (print.HasValue && print.Value)
{
var rd = new ReportDocument();
rd.Load("ReportFile.rpt");
rd.SetParameter("Parameter1", "abc");
rd.SetParameter("Parameter2", "foo");
rd.PrintOptions.PrinterName = dialog.PrinterSettings.PrinterName;
rd.PrintToPrinter(1, false, 0, 0);
}
The code above no longer works as advertised which has been admitted by SAP
You need to set the report document to an ISCDReportClientDocument and then print it. This is a more robust way of making sure the print job doesn't go to the default printer. The last two lines can be replaced with this code.
CrystalDecisions.ReportAppServer.Controllers.PrintReportOptions printReportOptions = new CrystalDecisions.ReportAppServer.Controllers.PrintReportOptions();
CrystalDecisions.ReportAppServer.Controllers.PrintOutputController printOutputController = new CrystalDecisions.ReportAppServer.Controllers.PrintOutputController();
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rptClientDoc;
rptClientDoc = cryRtp.ReportClientDocument;
printReportOptions.PrinterName = pDialog.PrinterSettings.PrinterName;
rptClientDoc.PrintOutputController.PrintReport(printReportOptions);
Here is another good link
http://mattruma.azurewebsites.net/?p=258
I would like to programmatically open a document from a SharePoint URL.
I have the following code:
Process wordProcess = new Process();
wordProcess.StartInfo.FileName
= "http://sharepoint/blank_site_1/document library 1/word document.docx";
wordProcess.StartInfo.UseShellExecute = true;
wordProcess.Start();
This opens a webbrowser window and downloads the file, which is not what I want. If I append
wordProcess.StartInfo.Verb = "OpenAsReadOnly"
as per (the documentation) I get a Win32 Exception "The parameter is incorrect" at wordProcess.Start(), despite the verb being present in wordProcess.StartInfo.Verbs when examining in the debugger.
I have a POC which does this by extracting the default program from the registry, building a command and starting the program with the filename, but I'd rather not go down that route if this can be easily solved, as all I want to do is open a file (the path of which just happens to look like a URL) with the default program.
Just a guess, try this:
wordProcess.StartInfo.FileName = "winword.exe";
wordProcess.StartInfo.Arguments = "\"http://sharepoint/blank_site_1/document_library_1/word document file.docx\"";
I want to print HTML from a C# web service. The web browser control is overkill, and does not function well in a service environment, nor does it function well on a system with very tight security constraints. Is there any sort of free .NET library that will support the printing of a basic HTML page? Here is the code I have so far, which does not run properly.
public void PrintThing(string document)
{
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
{
Thread thread =
new Thread((ThreadStart) delegate { PrintDocument(document); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
else
{
PrintDocument(document);
}
}
protected void PrintDocument(string document)
{
WebBrowser browser = new WebBrowser();
browser.DocumentText = document;
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
browser.Print();
}
This works fine when called from UI-type threads, but nothing happens when called from a service-type thread. Changing Print() to ShowPrintPreviewDialog() yields the following IE script error:
Error: dialogArguments.___IE_PrintType is null or not an object.
URL: res://ieframe.dll/preview.dlg
And a small empty print preview dialog appears.
You can print from the command line using the following:
rundll32.exe
%WINDIR%\System32\mshtml.dll,PrintHTML
"%1"
Where %1 is the file path of the HTML file to be printed.
If you don't need to print from memory (or can afford to write to the disk in a temp file) you can use:
using (Process printProcess = new Process())
{
string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
printProcess.StartInfo.FileName = systemPath + #"\rundll32.exe";
printProcess.StartInfo.Arguments = systemPath + #"\mshtml.dll,PrintHTML """ + fileToPrint + #"""";
printProcess.Start();
}
N.B. This only works on Windows 2000 and above I think.
I know that Visual Studio itself (at least in 2003 version) references the IE dll directly to render the "Design View".
It may be worth looking into that.
Otherwise, I can't think of anything beyond the Web Browser control.
Easy! Split your problem into two simpler parts:
render the HTML to PDF
print the PDF (SumatraPDF)
-print-to-default $file.pdf prints a PDF file on a default printer
-print-to $printer_name $file.pdf prints a PDF on a given printer
If you've got it in the budget (~$3000), check out PrinceXML.
It will render HTML into a PDF, functions well in a service environment, and supports advanced features such as not breaking a page in the middle of a table cell (which a lot of browsers don't currently support).
I tool that works very well for me is HiQPdf. https://www.hiqpdf.com/
The price is reasonable (starts at $245) and it can render HTML to a PDF and also manage the printing of the PDF files directly.
Maybe this will help. http://www.codeproject.com/KB/printing/printhml.aspx
Also not sure what thread you are trying to access the browser control from, but it needs to be STA
Note - The project referred to in the link does allow you to navigate to a page and perform a print without showing the print dialog.
I don't know the specific tools, but there are some utilities that record / replay clicks. In other words, you could automate the "click" on the print dialog. (I know this is a hack, but when all else fails...)