Printing various pdf files with print dialog visible from asp.net - c#

I am creating pdf files in an asp.net project that is being stored on the server in a folder. When a user wants to print this file, I need the user to specify which printer among all the label printers on the network since it may contain numeral laser printers as well. I have tried creating a print process but this send the pdf file straight to the default printer. is there any way to display the print dialog so that users can select the required printer?
printjob.StartInfo.FileName = pdfFileName;<br/>
printjob.StartInfo.Verb = "Print";<br/>
printjob.StartInfo.CreateNoWindow = false;<br/>
printjob.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
PrinterSettings setting = new PrinterSettings();<br/>
printjob.Start();

This can be achieved by Spire.pdf.dll reference.
To install this Open package manager console and type Install-Package Spire.pdf. this will install spire.pdf. Now the following code will help you print the pdf files.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("D:\\sample.pdf");
//Use the default printer to print all the pages
//doc.PrintDocument.Print();
//Set the printer and select the pages you want to print
PrintDialog dialogPrint = new PrintDialog();
dialogPrint.AllowPrintToFile = true;
dialogPrint.AllowSomePages = true;
dialogPrint.PrinterSettings.MinimumPage = 1;
dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
dialogPrint.PrinterSettings.FromPage = 1;
dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
if (dialogPrint.ShowDialog() == DialogResult.OK)
{
doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;
PrintDocument printDoc = doc.PrintDocument;
dialogPrint.Document = printDoc;
printDoc.Print();
}

Related

Setting PrinterSettings.PrinterName leads to printing of junk value on print of crystal report

I am trying to print a crystal report in WPF C# by sending it directly to printer without a viewer .The user can select different printers based on a drop down .However when i try to set PrinterSettings.PrinterName="PrinterName" ,the printer is printing junk values
My code is
ReportDocument ObjDoc = new ReportDocument();
System.Drawing.Printing.PrintDocument pDoc = new System.Drawing.Printing.PrintDocument();
CrystalDecisions.Shared.PrintLayoutSettings PrintLayout = new CrystalDecisions.Shared.PrintLayoutSettings();
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
System.Drawing.Printing.PageSettings pSettings = new System.Drawing.Printing.PageSettings(printerSettings);
System.Drawing.Printing.PageSettings pageSettings = new System.Drawing.Printing.PageSettings(printerSettings);
//Fetch Values in dataset cdtUTDocEng
ObjDoc.Load("//ServerName//Crystal_Reports//VHRSSALEFDE002.rpt");
if (cdtUTDocEng.Rows.Count > 0)
ObjDoc.SetDataSource(cdtUTDocEng);
else
{
lsPrintMessage = "Printing Failed -Failed to fetch Undertaking English details";
break;
}
if (cEnvironment.Production == psEnvironment)
{
ObjDoc.PrintOptions.PrinterDuplex = PrinterDuplex.Default;
lsPrinterName = cmbPrinter.SelectedValue.ToString().Trim();
if (CheckifPrinterInstalled(cmbPrinter.SelectedValue.ToString().Trim()) == true)
{
System.Drawing.Printing.PrinterSettings.PrinterName =cmbPrinter.SelectedValue.ToString().Trim();
ObjDoc.PrintToPrinter(printerSettings, pSettings, false, PrintLayout);
ObjDoc.Dispose();
lsPrintMessage = lsPrintMessage + "Full documentation English " + pdtPrintDetails.Rows[lirow]["COPY_TYPE"].ToString().Trim() + " has been sent to printer " + cmbPrinter.SelectedValue.ToString().Trim() + Environment.NewLine;
}
else
lsPrintMessage = "This printer " + cmbPrinter.SelectedValue.ToString().Trim() + " is not installed on server,cannot print";
}
This line causes the issues
System.Drawing.Printing.PrinterSettings.PrinterName =cmbPrinter.SelectedValue.ToString().Trim();
If i remove this line,the printer prints to my default printer and print comes correctly
I tried various combinations of setting printer name with the below values,all print junk values
1)printerSettings.PrinterName= "Dyna_Offshore";
2)pSettings.PrinterSettings.PrinterName= "Dyna_Offshore";
3)System.Drawing.Printing.PrinterSettings.PrinterName = "\\\\My PC IP\\Dyna_Offshore";
I tried setting printer name on report document like this ,but the code ignores this setting and takes PrinterName from printerSettings
1)ObjDoc.PrintOptions.PrinterName= "Dyna_Offshore";
it was printer driver issue.In WPF VS2017 ,when we are printing what ever driver name is printed on the physical printer .The printer needs to be installed with the exact driver name .If there is even a slight mismatch in the driver name ,it was causing junk values to be printed when printing programmatically through VS2017.Normal printing will work if there is mismatch in drivers.Also this issue is not there in VS2010 with Webforms .I have tested this in 2 printers in different countries and i was able to print successfully.

Launching a RemoteApp programmatically without a system call to "mstsc"

I've an RDP file that successfully start a RemoteApp.
remoteapplicationmode:i:1
remoteapplicationprogram:s:||application
remoteapplicationname:s:application.exe
remoteapplicationcmdline:s:
authentication level:i:2
gatewayusagemethod:i:2
gatewayprofileusagemethod:i:1
gatewaycredentialssource:i:0
full address:s:aaa.bbb.ccc.com
I tried to copy its settings into my C# objects:
AxMsRdpClient7NotSafeForScripting rc = new AxMsRdpClient7NotSafeForScripting();
rc.OnConnected += (_1, _2) => { rc.RemoteProgram2.ServerStartProgram("application.exe", "", "%HOMEDRIVE%" + "%HOMEPATH%", true, "", true); };
rc.RemoteProgram2.RemoteProgramMode = true;
rc.RemoteProgram2.RemoteApplicationProgram = "||application";
rc.RemoteProgram2.RemoteApplicationName = "application.exe";
rc.TransportSettings.GatewayUsageMethod = 1;
rc.TransportSettings.GatewayProfileUsageMethod = 1;
rc.TransportSettings.GatewayCredsSource = 0;
rc.Server = "aaa.bbb.ccc.com";
rc.UserName = "DOMAIN\\user";
rc.AdvancedSettings7.PublicMode = false;
rc.AdvancedSettings7.ClearTextPassword = "pass";
rc.AdvancedSettings7.AuthenticationLevel = 2;
rc.DesktopWidth = SystemInformation.VirtualScreen.Width;
rc.DesktopHeight = SystemInformation.VirtualScreen.Height;
rc.AdvancedSettings7.SmartSizing = true;
rc.Connect();
I've been searching everywhere but I wasn't able to find any example of how to launch a RemoteApp programmatically.
I've red this page, but it was not very helpfull. The client (a COM control) is connecting successfully, but it just displays a blue screen and no RemoteApp is launched.
Furthermore, I'm not sure that the right method to launch rc.RemoteProgram2.ServerStartProgram, because it takes paths as arguments, while in my RDP file no path is present!
Can anyone help me? I'm using the right objects to do what I want?
The server runs Windows Server 2008R2
If all you want to do is pragmatically launch a RemoteApp you already have an rdp file for, then just start it as a process:
System.Diagnostics.Process.Start(#"C:\Path_To_Rdp_File.rdp");

Printing a pdf file on client side printer in asp.net C#?

I have a problem in my project that i open a dynamically generated PDF file in popup window which working correctly. But now i want to print that pdf directly when popup is open at client side printer, how can i solved it ??
I need help of yours. Please suggest me some code for this.
You need to open the popup with javascript and the fire the print() function on it.
var opts = 'width=700,height=500,toolbar=0,menubar=0,location=1,status=1,scrollbars=1,resizable=1,left=0,top=0';
var newWindow = window.open(yourUrl,'name',opts);
newWindow.print();
Note that the url you open must be in the same domain as your current page for this to work.
Try This Code It will Work For You.
Process printjob = new Process();
printjob.StartInfo.FileName = #"D:\R&D\Changes to be made.pdf" //path of your file;
printjob.StartInfo.Verb = "Print";
printjob.StartInfo.CreateNoWindow = true;
printjob.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
PrinterSettings setting = new PrinterSettings();
setting.DefaultPageSettings.Landscape = true;
printjob.Start();

LocalReport Receipt Printing

I am printing a 3 inch receipt using a LocalReport with code from "Walkthrough: Printing a Local Report without Preview"
Some printers require the DeviceInfo PageWidth to be 8.5in to work correctly and some require 3.0in. Its appears that the report is being stretched to fill a wrong sized page. I have tried to adjust both the Report Paper Size and the Printer Paper size but can't seem to get the right combination.
Has anybody experienced this?
I figured this out. You need to account for the printer dpi.
Get the Printer Default Page Settings:
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = printerName;
this.defaultPageSettings = ps.DefaultPageSettings;
The Build the DeviceInfo Xml with that info:
private string BuildDeviceInfo()
{
StringBuilder returnValue;
System.Xml.XmlWriter writer;
returnValue = new StringBuilder(1024);
writer = System.Xml.XmlWriter.Create(returnValue);
writer.WriteStartElement("DeviceInfo");
writer.WriteElementString("OutputFormat", "EMF");
if (defaultPageSettings != null)
{
// DPI will keep the output from scaling in weird ways
writer.WriteElementString("PrintDpiX", defaultPageSettings.PrinterResolution.X.ToString());
writer.WriteElementString("PrintDpiY", defaultPageSettings.PrinterResolution.Y.ToString());
writer.WriteElementString("PageWidth", (defaultPageSettings.PaperSize.Width / 100m).ToString("F2") + "in");
writer.WriteElementString("PageHeight", (defaultPageSettings.PaperSize.Height / 100m).ToString("F2") + "in");
}
writer.Close();
return returnValue.ToString();
}
This worked for me. I'm printing from a WPF app to a STAR TSP100 receipt printer. When I set the margins & page size myself it came out huge, but when I calculated the minimum margins and page size as well as set the dpi from default printer settings the receipts printed correctly.
I used this to figure out the minimum margins: (H/T http://www.dreamincode.net/forums/topic/135864-printing-with-minimum-margins-specified-by-the-printer/)
Dim minimumMarginLeft, minimumMarginRight, minimumMarginTop,
minimumMarginBottom As Single
minimumMarginLeft = PrintDialog1.PrinterSettings.DefaultPageSettings.PrintableArea.Left
minimumMarginRight = PrintDialog1.PrinterSettings.DefaultPageSettings.PaperSize.Width - _
PrintDialog1.PrinterSettings.DefaultPageSettings.PrintableArea.Right
minimumMarginTop = PrintDialog1.PrinterSettings.DefaultPageSettings.PrintableArea.Top
minimumMarginBottom = PrintDialog1.PrinterSettings.DefaultPageSettings.PaperSize.Height - _
PrintDialog1.PrinterSettings.DefaultPageSettings.PrintableArea.Bottom
-I figured this out. You need to account for the printer dpi.
-Get the Printer Default Page Settings:
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = printerName;
this.defaultPageSettings = ps.DefaultPageSettings; The Build the DeviceInfo Xml with that info:
private string BuildDeviceInfo()
{
StringBuilder returnValue;
System.Xml.XmlWriter writer;
returnValue = new StringBuilder(1024);
writer = System.Xml.XmlWriter.Create(returnValue);
writer.WriteStartElement("DeviceInfo");
writer.WriteElementString("OutputFormat", "EMF");
if (defaultPageSettings != null)
{
// DPI will keep the output from scaling in weird ways
writer.WriteElementString("PrintDpiX", defaultPageSettings.PrinterResolution.X.ToString());
writer.WriteElementString("PrintDpiY", defaultPageSettings.PrinterResolution.Y.ToString());
writer.WriteElementString("PageWidth", (defaultPageSettings.PaperSize.Width / 100m).ToString("F2") + "in");
writer.WriteElementString("PageHeight", (defaultPageSettings.PaperSize.Height / 100m).ToString("F2") + "in");
}
writer.Close();
return returnValue.ToString();
}

PowerPoint Printing C# - Nothing gets printed

I am trying to print PowerPoint docs through my windows application in C#. I am using Microsoft.Office.Interop.PowerPoint for this functionality. Following is the code which I have used. It sends the request to printer but nothing gets printed.
string filename = "C:\\test.ppt";
int copies = 1;
Microsoft.Office.Interop.PowerPoint.Presentation work = null;
Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
Microsoft.Office.Interop.PowerPoint.Presentations presprint = app.Presentations;
work = presprint.Open(filename, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
//app.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
work.PrintOptions.PrintInBackground = Microsoft.Office.Core.MsoTriState.msoFalse;
//work.PrintOptions.PrintInBackground = Microsoft.Office.Core.MsoTriState.msoTrue;
//work.PrintOptions.ActivePrinter = "HP LaserJet 5000 Series PCL6";
work.PrintOptions.ActivePrinter = app.ActivePrinter;
work.PrintOut(1, work.Slides.Count, app.ActivePrinter, copies, Microsoft.Office.Core.MsoTriState.msoFalse);
work.Close();
app.Quit();`
When debugging, try halting after work.PrintOut and check your printer to see if the jobs arrives. I'm guessing you are closing PowerPoint too fast.

Categories