PrintDocument is printing filename string instead of document in c# - c#

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

Related

Print number of copies depending on the textbox input and put page numbers C#

Please help i am new to C# and i have a windows form with textbox1, textbox2, and buttom1 i would like to print the value of textbox1 without previewing or let me say straight to the printer without any interactions, textbox value has to be the number of capies i would love to print and also to page number all those copies say if i input 3 it should say page 1/3 2/3 3/3 please help and excuse my english
i have the following code under my button
private void button1_Click(object sender, EventArgs e)
{
PrintDocument printDocument = new PrintDocument();
printDocument.DefaultPageSettings.Landscape = true;
printDocument.DefaultPageSettings.PaperSize = new PaperSize("zed", 400, 850 );
//printDocument.PrinterSettings.Copies = 2;
printDocument.PrintPage += PrintDocumentOnPrintPage;
printDocument.Print();
i did solve the .copies problem but stil cannot solve the page numbering here is my code now under
private void button1_Click(object sender, EventArgs e)
{
short numCopies = 0;
numCopies = Convert.ToInt16(textBox2.Text);
PrintDocument printDocument = new PrintDocument();
printDocument.DefaultPageSettings.Landscape = true;
printDocument.DefaultPageSettings.PaperSize = new PaperSize("zed", 400, 850 );
printDocument.PrinterSettings.Copies = numCopies;
printDocument.PrintPage += PrintDocumentOnPrintPage;
printDocument.Print();
}

Orientation of PDF when viewing with Ghostscript

My application shows PDFs in a picture box by using Ghostscript. The PDFs I use are scanned images with a text layer. created by the OCR function from Acrobat Pro. This OCR function automatically sets the orientation according to the direction of the text. When the page is displayed in the picture box this information is lost. It just displays every page in portrait mode.
Is there a way for Ghostscript to access this property of the PDF and display it in the correct orientation in the picture box?
public Form1()
{
InitializeComponent();
_viewer = new GhostscriptViewer();
_viewer.DisplaySize += new GhostscriptViewerViewEventHandler(_viewer_DisplaySize);
_viewer.DisplayPage += new GhostscriptViewerViewEventHandler(_viewer_DisplayPage);
NumberOfPagesToExtract = 1;
_viewer.Open("NoFile.pdf", _gsVersion, true);
}
void _viewer_DisplaySize(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Image = e.Image;
}
void _viewer_DisplayPage(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Update();
currentPageNumber = _viewer.CurrentPageNumber;
LastPageNumber = _viewer.LastPageNumber;
lblTotalNmbPages.Text = " / " + LastPageNumber.ToString();
txtCurrentPgNmbr.Text = currentPageNumber.ToString();
}
The code to open the file:
private void btnOpenPdfGhostScript_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open PDF file";
ofd.Filter = "PDF, PS, EPS files|*.pdf;*.ps;*.eps";
if (ofd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
btnCLose_Click(this, null);
_viewer.Open(ofd.FileName, _gsVersion, true);
currentFilePath = ofd.FileName;
currentPageNumber = _viewer.CurrentPageNumber;
LastPageNumber = _viewer.LastPageNumber;
lblCurrentFIle.Text = ofd.FileName; //System.IO.Path.GetFileName(ofd.FileName);
if (backgroundWorker1.IsBusy != true) backgroundWorker1.RunWorkerAsync();
}
currentPageNumber = 1;
progressBar1.Value = 0;
}
I'm not sure if I should answer my own questions, but the problem is solved by updating to version 1.1.8. Thanks to HABJAN. Thanks very much.
The first thing to do is to use Ghostscript directly from the command line, rather than in an application. The main reason is that you can then supply a GS command line which other people can use, experiment with and comment on.
I can't see from your code how Ghostscript is invoked.
Ghostscript should, in general, honour the /MediaBox (and optionally /CropBox etc.) as well as the /Rotate attribute of pages.
But without a sample file and command line, I can't give you any suggestions.

Can I print an SSRS Report using PrintDocument?

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.

Printersettings in C#

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;
}

Print images c#.net

I have an image in a PictureBox, and I want to print it. No formatting, no nothing, just print it.
I've been searching on Google but I've got nothing, only people printing forms or text or reports.
private string imgSrc;
public string ImgSrc
{
get { return imgSrc; }
set { imgSrc = value; }
}
public Id_Manager()
{
ImgSrc = "D:\\Foto.jpg";
InitializeComponent();
idPicture.Load(this.ImgSrc);
}
Obviously the image is going to change, but for now I'm just interested in printing that image. I'm saving the url in a property just in case. Any help?
The Code below uses the PrintDocument object which you can place an image on to the printdocument and then print it.
using System.Drawing.Printing;
...
protected void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += PrintPage;
pd.Print();
}
private void PrintPage(object o, PrintPageEventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile("D:\\Foto.jpg");
Point loc = new Point(100, 100);
e.Graphics.DrawImage(img, loc);
}
Using the location, I have this FileInfo extension method that does it:
public static void Print(this FileInfo value)
{
Process p = new Process();
p.StartInfo.FileName = value.FullName;
p.StartInfo.Verb = "Print";
p.Start();
}

Categories

Resources