PDF and Silverlight, can they work together? - c#

I have a project where I create sample documents. Code here :
private void btnExcel_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (AutomationFactory.IsAvailable)
{
dynamic excel = AutomationFactory.CreateObject("Excel.Application");
excel.Visible = true;
dynamic workbook = excel.workbooks;
workbook.Add();
dynamic sheet = excel.ActiveSheet;
dynamic cell = null;
int i;
int z = 20;
for (i = 1; i <= 20; i++)
{
cell = sheet.Cells[i, 1];
cell.Value = i.ToString();
cell = sheet.Cells[i, 2];
cell.Value = z.ToString();
z--;
}
}
}
As you can see, this is for Excel documents. Is there any way I can something like this and export in to PDF files? Thanks for the insights.

Yes you can.
You can call a WCF service operation passing it all the data that it needs. Then do all what is required to generate the PDF file. After you've done that, you have two options:
You can send back the bytes as the result of the operation. In this case, you can show the user a SaveDialog to save the file to his local disk.
You can send the generated file to another aspx page and display the PDF file to the user who then has the choice to save it to his local disk or simply view it.
I hope that was what you're looking for.

Sure, you can. But why?! As I understand with AutomationFactory.CreateObject you can do it when on client machine Excel or PDF are installed.
But dealing with web application (especially when it's not an enterprise applications) you want to see pdf/xls/xlsx/... anywhere and you can relay on what is installed on client machine or not.
I have similar situation for one of my projects. We are using XPS format to show in silverlight because it has native support. And other formats are converted to xps.
There nuances in conversion, e.g. for excel documents I think best way is to convert each sheet separately, and implement special kind viewer that differs from viewer, for example, for word documents or pdf's.
There are 3rd party viewers that allow to view not only xps on silverlight but pdf too. But without them you can only let user to download PDF file not to view that, because these 3rd part viewer most likely convert pdf to xps under the hood.
Any way, especially without 3rd parties, it will take huge efforts to implement PDF viewer on silverlight. So I suggest you to use xps for viewing. But when you need to have pdf files for downloads than use XPS for viewing and PDF on download. In such case your converters will produce 2 formats for each sitation.
For example, look at
http://firstfloorsoftware.com/blog/announcement-document-toolkit-for-silverlight/
http://silverpdf.codeplex.com/
http://www.componentone.com/SuperProducts/PdfViewerSilverlight/

Related

Open file explorer to export my crystal report in ASP.NET MVC

I have created a MVC project to view my crystal report. What I want to do now is export my crystal report to some specific file formats (PDF, Word, CSV etc). Before exporting the file I want to open my file explorer and give a name to the file, select export type, select specific folder and export it.
How can I do this?
Not sure if this works the same while using ASP.NET.
private string SelectLocation(string fileName)
{
SaveFileDialog brwsr = new SaveFileDialog();
brwsr.FileName = fileName;
brwsr.Filter = "Pdf|*.pdf";
//Check to see if the user clicked the cancel button
if (brwsr.ShowDialog() == DialogResult.Cancel)
return "";
else
{
string newDirectoryPath = brwsr.FileName;
return newDirectoryPath;
}
}
This is a piece of code to find out which path they want to use to save the file.
If you use a Crystal Report Viewer to display the report on-screen, there are controls within the viewer that would allow a user to export the report and choose the export format and the folder and filename where it is saved. Some of your users may need some training to make effective use of some of the export parameters though. For example, when exporting to Excel there are a number of additional export parameters that can be used.
The nice thing about this method of exporting reports is the export feature is already fully implemented and all you have to code for is the creation and display of the report.

Convert a Datatable to .xls,.xlsx,.csv by the given delimeter in input

I want a method to write the datatable data to .xls,.xlsx or.csv based on the input provided along with the delimiter as input
public class DataTableExtensions
{
/*Input Params : Datatable input
fileFormat(.xls,.csv,.xlsx)
delimeter('\t' (tabSpace) or ,(comma) or | (pipe Symbol)
filepath - Any local folder*/
public void WriteToCsvFile(DataTable dataTable,string fileFormat,string delimeter, string filePath)
{
//Code to convert file based on the input
//Code to create file
System.IO.File.WriteAllText(filePath, fileContent.ToString());
}
}
You said it is only 1000 rows every 2 hours in the comments. That is a acceptable amount of data for a C# programm. I would say the big question left is wich output format you use.
.CSV is the simplest one. This format can be done with a File.WriteLine() and some string concaction. There is no build in CSV parser or writer code I am aware off in C#, but there is plenty of 3rd party code.
.XLS requires the (t)rusty Office COM Interop. That requires office to be installed and does not work from a non-interactive session (like a Windows Service). On top of all the normal issues for using COM interop.
There is the odd "export to XLS" function on existing classses, but those are rare, far inbetween and about everything you get. Unfortunately as we always had COM Interop as fallback, we never quite developed a standalone library for working with .XLS. Ironically working with this old format is harder from C#/.NET then it would be from Java.
.XLSX however is easier. It can be written using the OpenXML SDK. Or the XML writer and ZipArchive class: At their core all the ???x formats are a bunch of .XML files in a renamed .ZIP container. There should even be 3rd party code out there to make using the SDK easier.
.CSV is the lowest common denominator and propably the easiest to create. However if a user is supposed to open this document, the lack for formating might become an issue.
.XSLX would be my choice if you need a user to open it.
.XSL I would avoid like a swarm of angry bees.
I have written this Program to convert Xls,XLSx using console application with
Datatable as input and for text file I have written a simple stream writer logic.This works good. Initially I have installed package manage console and below code
using expertXLs package.I am not sure wheather I can share the key of that
or not.Please search the key and give in config before running it
Package Manage Console - Install-Package ExpertXls.ExcelLibrary -Version 5.0.0
Code :
--------
private static void GenerateTxtFileFromDataTable(DataTable sampleDataTable,string delimiter)
{
var _expertxlsLK = ConfigurationManager.AppSettings["ExpertxlsLK"];
//GetKey Value from config
// Create the workbook in which the data from the DataTable will be loaded 0 for 2003 Excel(xls),1 for 2007 Excel(xlsx)
ExcelWorkbookFormat workbookFormat = ExcelWorkbookFormat.0;
// create the workbook in the desired format with a single worksheet
ExcelWorkbook workbook = new ExcelWorkbook(workbookFormat);
workbook.EnableFormulaCalculations();
workbook.LicenseKey = _expertxlsLK;
// get the first worksheet in the workbook
ExcelWorksheet worksheet = workbook.Worksheets[0];
// set the default worksheet name
worksheet.Name = "ClaimInformation";
// load data from DataTable into the worksheet
worksheet.LoadDataTable(sampleDataTable, 1, 1, true);
worksheet.Workbook.EnableFormulaCalculations();
workbook.Save(#"M:\Rupesh\test.xlsx");
workbook.Close();
}

Onenote OCR capabilities in a desktop software

Is there an API to use Onenote OCR capabilities to recognise text in images automatically?
If you have OneNote client on the same machine as your program will execute you can create a page in OneNote and insert the image through the COM API. Then you can read the page in XML format which will include the OCR'ed text.
You want to use
Application.CreateNewPage to create a page
Application.UpdatePageContent to insert the image
Application.GetPageContent to read the page content and look for OCRData and OCRText elements in the XML.
OneNote COM API is documented here: http://msdn.microsoft.com/en-us/library/office/jj680120(v=office.15).aspx
When you put an image on a page in OneNote through the API, any images will automatically be OCR'd. The user will then be able to search any text in the images in OneNote. However, you cannot pull the image back and read the OCR'd text at this point.
If this is a feature that interests you, I invite you to go to our UserVoice site and submit this idea: http://onenote.uservoice.com/forums/245490-onenote-developers
update: vote on the idea: https://onenote.uservoice.com/forums/245490-onenote-developer-apis/suggestions/10671321-make-ocr-available-in-the-c-api
-- James
There is a really good sample of how to do this here:
http://www.journeyofcode.com/free-easy-ocr-c-using-onenote/
The main bit of code is:
private string RecognizeIntern(Image image)
{
this._page.Reload();
this._page.Clear();
this._page.AddImage(image);
this._page.Save();
int total = 0;
do
{
Thread.Sleep(PollInterval);
this._page.Reload();
string result = this._page.ReadOcrText();
if (result != null)
return result;
} while (total++ < PollAttempts);
return null;
}
As I will be deleting my blog (which was mentioned in another post), I thought I should add the content here for future reference:
Usage
Let's start by taking a look on how to use the component: The class OnenoteOcrEngine implements the core functionality and implements the interface IOcrEngine which provides a single method:
public interface IOcrEngine
{
string Recognize(Image image);
}
Excluding any error handling, it can be used in a way similar to the following one:
using (var ocrEngine = new OnenoteOcrEngine())
using (var image = Image.FromFile(imagePath))
{
var text = ocrEngine.Recognize(image);
if (text == null)
Console.WriteLine("nothing recognized");
else
Console.WriteLine("Recognized: " + text);
}
Implementation
The implementation is far less straight-forward. Prior to Office 2010, Microsoft Office Document Imaging (MODI) was available for OCR. Unfortunately, this no longer is the case. Further research confirmed that OneNote's OCR functionality is not directly exposed in form of an API, but the suggestions were made to manually parse OneNote documents for the text (see Is it possible to do OCR on a Tiff image using the OneNote interop API? or need a document to extract text from image using onenote Interop?. And that's exactly what I did:
Connect to OneNote using COM interop
Create a temporary page containing the image to process
Show the temporary page (important because OneNote won't perform the OCR otherwise)
Poll for an OCRData tag containing an OCRText tag in the XML code of the page.
Delete the temporary page
Challenges included the parsing of the XML code for which I decided to use LINQ to XML. For example, inserting the image was done using the following code:
private XElement CreateImageTag(Image image)
{
var img = new XElement(XName.Get("Image", OneNoteNamespace));
var data = new XElement(XName.Get("Data", OneNoteNamespace));
data.Value = this.ToBase64(image);
img.Add(data);
return img;
}
private string ToBase64(Image image)
{
using (var memoryStream = new MemoryStream())
{
image.Save(memoryStream, ImageFormat.Png);
var binary = memoryStream.ToArray();
return Convert.ToBase64String(binary);
}
}
Note the usage of XName.Get("Image", OneNoteNamespace) (where OneNoteNamespace is the constant "http://schemas.microsoft.com/office/onenote/2013/onenote" ) for creating the element with the correct namespace and the method ToBase64 which serializes an GDI-image from memory into the Base64 format. Unfortunately, polling (See What is wrong with polling? for a discussion of the topic) in combination with a timeout is necessary to determine whether the detection process has completed successfully:
int total = 0;
do
{
Thread.Sleep(PollInterval);
this._page.Reload();
string result = this._page.ReadOcrText();
if (result != null)
return result;
} while (total++ < PollAttempts);
Results
The results are not perfect. Considering the quality of the images, however, they are more than satisfactory in my opinion. I could successfully use the component in my project. One issue remains which is very annoying: Sometimes, OneNote crashes during the process. Most of the times, a simple restart will fix this issue, but trying to recognise text from some images reproducibly crashes OneNote.
Code / Download
Check out the code at GitHub
not sure about OCR, but the documentation site for onenote API is this
http://msdn.microsoft.com/en-us/library/office/dn575425.aspx#sectionSection1

c# easy way to print formatted data

I'm looking for a simple way to print DIN-A4 paper with data from a database on it. The data should be filled into multiple tables with borders. Some of the data should have a different text format p.e. bold or underlined. I should also be able to print multiple images onto that sheet.
The program should be a Windows Forms Application or a console application written in C#.
Which is the easiest / most common way to format data and print it like that?
Any suggestions apreciated :)
EDIT:
this is my current code with almost no success. It actually prints but what I get is simply the xml file printed.
private void printButton_Click(object sender, EventArgs e)
{
string printPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
fileToPrint = new System.IO.StreamReader(printPath + #"\test.xml");
printFont = new System.Drawing.Font("Arial", 10);
PrintDocument printDocument1 = new PrintDocument();
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.Print();
fileToPrint.Close();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float yPos = 0f;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
while (count < linesPerPage)
{
line = fileToPrint.ReadLine();
if (line == null)
{
break;
}
yPos = topMargin + count * printFont.GetHeight(e.Graphics);
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
count++;
}
if (line != null)
{
e.HasMorePages = true;
}
}
Printing in .NET is quite hard without additional tools.
Visual Studio 2008 and Report Wizard
Its my favorite tool. It's based on Microsoft Reporting Services which is available in Visual Studio 2008. It works similar to MS Access reports functionality.
Unfortunatelly i was not able to run it and design reports in other Visual Studio versions. Reporting Services are available in .NET Framework and you can use them with any Visual Studio, but there is no Report Designer/Wizard tool in versions other than 2008).
Crystal Reports (expensive but really good).
If you have some time you may fight with pixels like this:
Simplified .NET printing in C# Dave Brighton at codeproject.com
WebBrowser control, as #colosso wrote in another answer, but this depends on Internet Explorer version and i personally don't like this method.
I found two possibilities:
1) In my opinion this is the worse one. I found a third-party software called "Antena house formatter". You can find it on www.antennahouse.com but it's unfortunately neither open source nor free software. This software allows you to convert xml, xsl or xsl-fo data into pdf and other formats. From there you could print it with standard c#.
I didn't chose this way for some reasons: To be hooked on a third-party software is no good solution in my opinion, although this Antennahouse formatter is a quite nice, reliable and fast software.
2) This is the solution I have chosen. You can create a simple WebBrowser control and fill it either with a saved html file or you can just fill it with a dynamically created string. I now generate a string witch contains the whole html document an load it into the Webbrowser control:
webBrowser1.Document.OpenNew(true);
string strHtml = "<html><head></head><body></body></html>";
webBrowser1.Document.Write(strHtml);
When loading the form I open a new "tab" with:
webBrowser1.Navigate("about:blank");
You can either show the Webbrowser control to have a "preview" of the site that is getting printed or just hide it. Finally, when you loaded you html file into the control you can print it with:
webBrowser1.Print();
It will ow print the document with you default printer. I know, using a html file to print a site like that feels some sort of "hacky" but it's the easiest way I found to do something like that. Especially if you net to print very complex Sites with many different things on it.
Nice to know:
I had to print DIN-A4 sites. I set my content with to 670px and that fits nice.
You are printing with a Webbrowser control. He will take the printer settings from your local installed Internet Explorer (or the registry if we want to be exactly). If you want to change it go into your IE > Printing > Page settings... and set you desired options or just change it in your registry.
Hope this helps someone :)

Generate PDF from ASP.NET from raw HTML/CSS content?

I'm sending emails that have invoices attached as PDFs. I'm already - elsewhere in the application - creating the invoices in an .aspx page. I'd like to use Server.Execute to return the output HTML and generate a PDF from that. Otherwise, I'd have to use a reporting tool to "draw" the invoice on a PDF. That blows for lots of reasons, not the least of which is that I'd have to update both the .aspx page and the report for every minor change. What to do...
There is no way to generate a PDF from an HTML string directly within .NET, but there are number of third party controls that work well.
I've had success with this one: http://www.html-to-pdf.net
and this: http://www.htmltopdfasp.net
The important questions to ask are:
Does it render correctly as compared to the 3 major browsers: IE, FF and Safari/Chrome?
Does it handle CSS fine?
Does the control have it's own rendering engine? If so, bounce it. You don't want to trust a home grown rendering engine - the browsers have a hard enough problem getting everything pixel perfect.
What dependencies does the third party control require? The fewer, the better.
There are a few others but they deal with ActiveX displays and such.
We use a product called ABCPDF for this and it works fantastic.
http://www.websupergoo.com/abcpdf-1.htm
This sounds like a job for Prince. It can take HTML and CSS and generate a PDF, which you can then present to your users. It supports CSS3 better than most web browsers (staff include HÃ¥kon Wium Lie, the inventor of CSS).
See the samples, especially the ones for Wikipedia pages, for the beautiful output it can generate. There's also an interesting Google Tech Talk with the authors.
Edit: There is a .NET wrapper available.
wkhtmltopdf is a free and cool exe to generate pdf from html. Its written in c++. But nReco htmltopdf is a wrapper dotnet library for this awesome tool. I implemented using this dotnet library and it was just so good it does everything by its own you just need to give html as a data source.
/// <summary>
/// Converts html into PDF using nReco dll and wkhtmltopdf.exe.
/// </summary>
private byte[] ConvertHtmlToPDF()
{
HtmlToPdfConverter nRecohtmltoPdfObj = new HtmlToPdfConverter();
nRecohtmltoPdfObj.Orientation = PageOrientation.Portrait;
nRecohtmltoPdfObj.PageFooterHtml = CreatePDFFooter();
nRecohtmltoPdfObj.CustomWkHtmlArgs = "--margin-top 35 --header-spacing 0 --margin-left 0 --margin-right 0";
return nRecohtmltoPdfObj.GeneratePdf(CreatePDFScript() + ShowHtml() + "</body></html>");
}
The above function is an excerpt from the below link post which explains it in detail.
HTML to PDF in ASP.Net
The initial question is about converting another aspx page containing an invoice to a PDF document. The invoice is probably using some session data and the user suggests to use Server.Execute() to obtain the invoice page HTML code and then to convert that code to PDF. Converting the invoice page URL directly is not possible because a new session would be created during conversion and the session data would be lost.
This is actually a good technique to preserve session data during conversion which is applied in Convert a HTML Page to PDF in Same Session ASP.NET Demo of the EvoPdf library. The complete C# code to get the HTML string rendered by the invoice page and to convert that string to PDF is:
// Execute the invoice page and get the HTML string rendered by this page
TextWriter outTextWriter = new StringWriter();
Server.Execute("Invoice.aspx", outTextWriter);
string htmlStringToConvert = outTextWriter.ToString();
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Use the current page URL as base URL
string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;
// Convert the page HTML string to a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlStringToConvert, baseUrl);
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Page_in_Same_Session.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
As long as you can make sure to use proper XHTML, you could also use a product like Alt-Soft's Xml2PDF to convert XML (XHTML) into PDF by means of XSLT/XSL-FO.
It takes a bit of a learning curve to master, but it works very well once you've "got" it!
Marc
Since you are producing the answer, you can use a tool like Report.NET:
http://sourceforge.net/projects/report/
I disagree with the answers that say you cannot convert directly from output to PDF, however, as you can "re-call" the page and get the HTML as a stream and convert it. I am not sure what tool you would want to use to do this, however. In other words, it is possible, but I am not sure it is worth it. The PDF creation libs, like Report.NET, even though they force reusing some logic and no automagic converrsion, it is easier.
I have not tried this component, but I have heard good things about it from those who have. The model is more like HTML, but I am not sure you can simply send a rendered ASPX to it to create PDF:
http://www.websupergoo.com/abcpdf-8.htm
If you try to find some html to pdf software via GOOGLE you'll get a pile of this stuff.
There are about 10 leaders but most of them use IE dlls in background mode.
Just couple of them use their own parsing engine.
Please try PDF Duo .NET component in your ASP.NET project if you wish to create a PDF programaticaly.
It is light component for a cool generating of PDF invoces, reports e.g.
I'd go a different route. Assuming you are using SQL Server, use SSRS and generate the PDF that way.
A possible minimal solution to use Server.Execute() to obtain the HTML of the invoice page and convert that code to a PDF using winnovative html to pdf api for .net is:
TextWriter outTextWriter = new StringWriter();
Server.Execute("Invoice.aspx", outTextWriter);
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
byte[] pdfBytes = htmlToPdfConverter.ConvertHtml(outTextWriter.ToString(),
httpContext.Current.Request.Url.AbsoluteUri);
You can use PDFSharp or iTextSharp to convert html to pdf. PDFSharp is not free.

Categories