I want to print a text from file in C#(Visual Studio 2010), but file contains a unformatted text with very long strings; how can i print this file, if long string disappears from for borders and don't print? Give me an example please. Thank you.
private void printDocumentMenu_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
String text = richTextBoxMenuOutput.Text;
OpenFileDialog openAnnotationFile = new OpenFileDialog();
if (openAnnotationFile.ShowDialog() == DialogResult.OK)
{
StreamReader reader = new StreamReader(openAnnotationFile.FileName);
e.Graphics.DrawString(reader.ReadToEnd(), this.Font, Brushes.Black, 0, 0);
}
}
You can use another overload for DrawString(), so that the text gets wrapped in a rectangle:
e.Graphics.DrawString(reader.ReadToEnd(), this.Font, Brushes.Black, e.PageBounds);
Related
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
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.
I am using ExportToXlsx() method in order to export data from GridView to Excel Sheet.
this seams to export and format the data just fine, even my conditional formatting is being exported as is.
The only thing I would like to know is that how do I add a logo and header in that excel file without having to add it manually. Like in a Crystal Report we can add these things at the design time. Is there something that can be done ?
You can do that with PrintableComponentLink and write a CreateReportHeaderArea event handler:
private void button_Export_Click(object sender, EventArgs e)
{
var y = new DevExpress.XtraPrinting.PrintableComponentLink(new PrintingSystem());
y.Component = gridControl1;
y.CreateReportHeaderArea += y_CreateReportHeaderArea;
if (saveFileDialog_Report.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog_Report.FileName.ToLower().EndsWith("xlsx"))
{
y.ExportToXlsx(saveFileDialog_Report.FileName);
}
}
}
void y_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
{
DevExpress.XtraPrinting.TextBrick brick;
brick = e.Graph.DrawString("My Report Title Here", Color.Navy, new RectangleF(0, 0, 500, 40), DevExpress.XtraPrinting.BorderSide.None);
brick.Font = new Font("Arial", 16);
brick.StringFormat = new DevExpress.XtraPrinting.BrickStringFormat(StringAlignment.Center);
}
In addition to Milen's answer that added the header, I was also able to add an image to the report like below
void y_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
{
Image newimage = Image.FromFile("path");
DevExpress.XtraPrinting.ImageBrick iBrick;
iBrick = e.Graph.DrawImage(newimage, new RectangleF(0,0,40,40));
DevExpress.XtraPrinting.TextBrick brick;
brick = e.Graph.DrawString("My Report Title Here", Color.Navy, new RectangleF(40, 0, 500, 40), DevExpress.XtraPrinting.BorderSide.None);
brick.Font = new Font("Arial", 16);
brick.StringFormat = new DevExpress.XtraPrinting.BrickStringFormat(StringAlignment.Center);
}
just make sure that you keep the coordinates and size correct so the image and text dont overlap.
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 have this code for PrintPreview and print.
private void button2_Click_1(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(Logo.Image, 800, 100);
e.Graphics.DrawString(label20.Text, label20.Font, Brushes.Black, 134, 100);
e.Graphics.DrawString(label22.Text, label22.Font, Brushes.Black, 462, 100);
e.Graphics.DrawString(textBox101.Text, textBox101.Font, Brushes.Black, 134, 230);
e.Graphics.DrawString(textBox104.Text, textBox104.Font, Brushes.Black, 134, 270);
Now how can I save the same output as the printPreview as a PDF file with another buttonClick action or in the print Preview window.
If you already using the printing features of WinForms it would be the simplest solution install a PDF printer program, e.g. PDFCreator. After installing it can be used like a real printer, but saves a PDF file.
If you want to build in the feature into your application, you should check out this question.
If you are intresteed in creating your own,You can use this .
To add a button in PrintPreviewDialougue ;
class CustomPrintPreviewDialog : System.Windows.Forms.PrintPreviewDialog
{
public CustomPrintPreviewDialog()
: base()
{
if(this.Controls.ContainsKey("toolstrip1"))
{
ToolStrip tStrip1 = (ToolStrip)this.Controls["toolstrip1"];
ToolStripButton button1 = new ToolStripButton();
button1.Text = "Save";
button1.Click += new EventHandler(SaveDocument);
button1.Visible = true;
tStrip1.Items.Add(button1);
}
}
protected void SaveDocument(object sender, EventArgs e)
{
// code for save the document
MessageBox.Show("OK");
}
}
From :Codeproject