DevExpress GridView Exported Excel Sheet Adding Custom Heading and Logo - c#

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.

Related

Save webbrowser control as image

I'm trying to get webbrowser control as image. But when my code gives me a full screen image. I want to get only webbrowser image. How should I fix my code to get the right image?
Bitmap memoryImage;
private void button2_Click(object sender, EventArgs e)
{
Graphics myGraphics = webBrowser1.CreateGraphics();
Size s = webBrowser1.Size;
memoryImage = new Bitmap(webBrowser1.Width,webBrowser1.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(webBrowser1.Location.X, webBrowser1.Location.Y, 0, 0, s);
memoryImage.Save("C:\\Users\\Koo\\Desktop\\NaverMap.png");
Use the following code - Which supported and does work, but not always works fine.
In some circumstances you will get a blank image screenshot(when there is a more complex html it loads, the more possible it fails)
using (var browser = new System.Windows.Forms.WebBrowser())
{
browser.DocumentCompleted += delegate
{
using (var pic = new Bitmap(browser.Width, browser.Height))
{
browser.DrawToBitmap(pic, new Rectangle(0, 0, pic.Width, pic.Height));
pic.Save(imagePath);
}
};
browser.Navigate(Server.MapPath("~") + htmlPath); //a file or a url
browser.ScrollBarsEnabled = false;
while (browser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}

PrintDocument is printing filename string instead of document in 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

Simple document using FastReport.Net does not show text

I tried to make a simple document using FastReport. So I started with placing a button on the form and writing the following code in order to run it when the button is clicked:
private void button1_Click(object sender, EventArgs e)
{
Report report = new Report();
ReportPage page1 = new ReportPage();
page1.Name = "Page1";
report.Pages.Add(page1);
page1.ReportTitle = new ReportTitleBand();
page1.ReportTitle.Name = "ReportTitle1";
TextObject text1 = new TextObject();
text1.Name = "Text1";
text1.Text = "REPORT TITLE TEXT";
text1.HorzAlign = HorzAlign.Center;
text1.Font = new Font("Tahoma", 14, FontStyle.Bold);
page1.ReportTitle.Objects.Add(text1);
report.Show();
}
Unfortunately, when I ran the application and I pressed the button, a blank page was shown, without any text. What is wrong with the code? Does it lack some element?
You should set width and height for text object:
text1.AutoWidth = true;
text1.Height = 100;

Save as PDF in PrintPreview

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

Document Printing in C#/VS 2010

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

Categories

Resources