I have an SSRS report that I need to print from my windows form application using VS 2008 (C#). I want to print this report using PrintDocument. Is there anyway to do this?
Here is what I have:
private void Printing(string pname)
{
PrintDocument printDoc = new PrintDocument();
if (pname.Length > 0)
printDoc.PrinterSettings.PrinterName = pname;
PageSettings ps = new PageSettings();
PaperSize pz = new PaperSize();
pz.Height = 650;
pz.Width = 400;
ps.PaperSize = pz;
printDoc.DefaultPageSettings = ps;
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
printDoc.Print();
}
private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
{
}
I just don't know how to attach the report name to the PrintDocument.
You will need to use the ReportExecution2005.ReportExecutionService and call the Render method with the format set to "IMAGE" and device info data set similar to:
<DeviceInfo>
<OutputFormat>TIFF</OutputFormat>
</DeviceInfo>
The device info can be used for images to control dpi specific parameters used in print controls.
Related
I am currently developing a WPF application using C# language and .Net Framework 4.8
In the MainWindow I have the Print menu button to generate a Fixed Document like this
private void clPrintMenu_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
/* Margins is a User-defined structure to set Top, Right, Bottom and Left values
in Cm, Inches and Pixels */
Margins margins = new Margins(21, 29.7);
margins.Unit = Units.Pixel;
Size sz = new Size(margins.Left.Value, margins.Top.Value);
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = sz;
/* CashJournal is a UserControl designed as an A4 sheet the list below contains several
Cashjournal which represent multiple pages */
List<CashJournal> journals = CashJournal.PrintJournals;
foreach (CashJournal jrl in journals)
{
FixedPage page = new FixedPage();
page.Width = margins.Left.Value;
page.Height = margins.Top.Value;
FixedPage.SetLeft(jrl, 0);
FixedPage.SetTop(jrl, -20);
page.Children.Add(jrl);
PageContent content = new PageContent();
((IAddChild)content).AddChild(page);
document.Pages.Add(content);
}
/* The document is then passed to a window for preview */
CashPrintPreview dialog = new CashPrintPreview(selectedTab, document);
dialog.ShowDialog();
}
In the CashPrintPreview, the document is displayed in a DocumentViewer which has a Print button. I modifed the Print() CommandBinding to bind the function to my custom function PrintPreview
private void PrintView(object sender, ExecutedRoutedEventArgs e)
{
PrintDialog dialog = new PrintDialog();
bool? rslt = dialog.ShowDialog();
if (rslt != true)
return;
/* This block is my problem */
PrintQueue queue = dialog.PrintQueue;
PrintCapabilities capabilities = queue.GetPrintCapabilities();
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
document.DocumentPaginator.PageSize = sz;
XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(queue);
writer.Write(document);
}
When I choose the XPS printer from the PrintDialog, the created file render perfectly as it appears in the preview. But when I choose the PDF printer from Adobe the document is not well scaled like too much margins on top and not enough margins on left.
How can I resolve this issue. Thanks.
PS. Please be explicit.
I am finally able to print a FixedDocument to PDF using the Acrobat printer. I just needed to pass my document to the PrintDocument function of the chosen printer:
private void PrintView(object sender, ExecutedRoutedEventArgs e)
{
PrintDialog dialog = new PrintDialog();
bool? rslt = dialog.ShowDialog();
if (rslt != true)
return;
dialog.PrintDocument(((IDocumentPaginatorSource)document).DocumentPaginator, "");
}
I am implementing PrintDocument in my asp.net project wherein i need to print specific pages of my document but instead of document being printed, its the string of "fileName" which is getting printed. Below is my code
string fileName = "C:\\DocToPrint\\Sample2018.pdf";
protected void Page_Load(object sender, EventArgs e)
{
using (PrintDocument pd = new PrintDocument())
{
pd.PrinterSettings.FromPage = 1;
pd.PrinterSettings.ToPage = 1;
pd.PrinterSettings.PrintRange = PrintRange.SomePages;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
public void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
ev.Graphics.DrawString(fileName, new Font("Arial", 10), Brushes.Black,
ev.MarginBounds.Left, 0, new StringFormat());
}
}
What is being missed here? Please suggest..Thanks
There's a nuget package called Spire.Pdf that's very simple to use. The free version has a limit of 10 pages although, however, in my case it was the best solution once I don't want to depend on Adobe Reader and I don't want to install any other components.
https://www.nuget.org/packages/Spire.PDF/
PdfDocument pdfdocument = new PdfDocument();
pdfdocument.LoadFromFile(pdfPathAndFileName);
pdfdocument.PrinterName = "My Printer";
pdfdocument.PrintDocument.PrinterSettings.Copies = 1;
pdfdocument.PrintFromPage = index;
pdfdocument.PrintToPage = index;
pdfdocument.PrintDocument.Print();
pdfdocument.Dispose();
Sample for printing some page.
https://www.e-iceblue.com/forum/print-one-page-from-pdf-t6586.html
Using
using System.Drawing.Printing;
using System.Windows.Forms;
private void Main()
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
PrintDialog pdi = new PrintDialog();
pdi.Document = pd;
// -> preventing PrintDialog from changing the default printer
// -> BUT changing the PrintDocuments-Printer to the selected??
if (pdi.ShowDialog() == DialogResult.OK)
{
pd.Print();
// -> OR reset default Printer?
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
//- do the printing
ev.HasMorePages = false;
}
always changes the default Windows Printer if the user selects a printer with PrintDialog:
How can I prevent the change by PrintDialog? And how to set the selected Printer for PrintDocument
or
Do I need to reset the default printer after printing? How?
or
Am I'm doing something wrong (I need to develop with Net Framework 2.0)
Regards Thomas
I need to send all the images in a folder to printer at once. This is possible from windows explorer where we select all the image files, right click and select print to send all the selected images to print dialog from where we can select printer settings and proceed to print. How do I do this from within c# windows form Application?
Edit: I came up with this but it prints only the last page. How should I modify this?
private void printAllCardSheetBtn_Click(object sender, EventArgs e)
{
PrintDocument pdoc = new PrintDocument();
pdoc.DocumentName = "cardsheets";
PrintDialog pd = new PrintDialog();
if(pd.ShowDialog() == DialogResult.OK)
{
PrinterSettings ps = pd.PrinterSettings;
pdoc.PrinterSettings = ps;
pdoc.PrintPage += pdoc_PrintPage;
pdoc.Print();
}
}
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
string[] sheetpaths = Directory.GetFiles(_sheetDirectory);
Point point = new Point(0, 0);
foreach (string s in sheetpaths)
{
g.DrawImage(new Bitmap(s), point);
}
}
You can use PrintDocument.
Just get all images from folder, load them to a Bitmap and through a For Loop use PrintDocument to print one by one.
BTW, use PrintPage event and with PrintPageEventArgs you can draw the image in the document to print with Graphics.
Cheers
EDIT: Check this example -> Example
I am creating a PDF File in my application, and then I print it (works fine.)
When I print this pdf on another computer/Printer it doesn't look the same! I want it so that it always looks the same, on whichever printer I print on.
Maybe I have to set the borders? Like this:
PrinterSettings ps = new PrinterSettings();
ps.DefaultPageSettings.HardMarginX = 0;
ps.DefaultPageSettings.HardMarginY = 0;
But HardMargin is not writable. Have you guys got some ideas?
Try to set up this way:
PrintDocument printDocument1 = new PrintDocument();
var printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = "Printer name";// optional
//printerSettings.PrinterName = "HP Officejet J6400 series";
printDocument1.PrinterSettings = printerSettings;
printDocument1.PrintPage += printDocument1_PrintPage;
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = printDocument1;
// in the dialog, you can set up the paper size, etc.
printDialog1.UseEXDialog = true;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
Handler function:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//This print form a rich textbox, but you can render pdf here.
//e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);
//e.Graphics.PageUnit = GraphicsUnit.Inch;
}