Html table (text) to image using C# - c#

Can anyone point me to some sample code in C# for converting an html table to image? I know how to convert text to image but i need to create an image of well formatted text. The whole text is formatted in html table.

You can use the WebBrowser.DrawToBitmap method. Here is an example. So what I would do is create a page dynamically with the table you want, and nothing but the table, then use the DrawToBitmap method to save it to an image file.

Best is to use http://iecapt.sourceforge.net/

Converting HTML to bitmap is difficult task. You will need first a rendering engine capable of handling HTML and optionally javascript and css (in case you want to support them). Using a WebBrowser control could be done but there might be better ways.

Related

Retrieve text from database and format it on the front end of the website

I have some text currently stored in my database table as nvarchar.
I am currently retrieving the text using a stored procedure and binding it to a literal within a gridview on the front end.
What I would like to do is to retrieve the text and then format it , like inserting line spaces and making
certain area bold. Is it possible to do so ? Can anyone give me an idea of how it can be done ?
One idea thats striking me is to use XML while storing the text . But even if I do that how would I make a certain part of the text bold and include line spaces.
So currently, my text is stored in the database table column nvarchar(max) as:
This is the heading this is the content
What I would like to do is to display the above within a gridview like:
**This is a Heading** (heading in bold)
This is the content
The simplest method (one I have used several times) is to store the html in the table like this:
<h1>This is the heading</h1>This is the content
You will have to add special handling for working with html, but it works just fine.
You can also store the header string in one field, and body in another.
Short of that, you would have to have some indicator telling the front which part of the string should be bolded, etc. and that can get very complex
Short answer is that this is possible but takes some work.
You first need to decide in what format are you going to store the data and how can you specify format on the client side, before text is entered into database.
If you have WYSIWYG editor for text – html conversion you can try storing the HTML. This will be the easiest way in terms of storage.
If you decide to use this method note that you’ll need to do a lot of validation on the server to avoid cross site scripting attacks. Shortly put – make sure the HTML you get on the server doesn’t contain any javascript or any tags apart from those you want to support.
Its better to use Editor of AJAX Toolkit, doesnt required any other thing, its a complete editor, you can even color your font as u wanted.

C# simple html editor - how to search and replace content

Today i'm working on simple html editor in visual c#.
My goal is to open pure html file from local drive (Opendialog load into string or load into webbrowser completed) and allow to edit key fragments.
application should find specific divs and return the full content of that div to textbox or better to combobox (compare to combobox item and show it).
if i change the textbox (or pick up another item from combobox) application should present changes on webbrowser control.
Then i need to print this html as is seen on webbrowser control.
Last thing is to save modified hmtl overwrting original and adding comment with changes at bottom of the html file.
I want to know how to perform search&replace in this project? How to "adress" content of a div?
Better is string searching, indexof, string.replace etc. Or drown into DOM-thing (i don't know both at the moment).
How to present changes on html preview on webcontrol component? And finally overwrite a file?
Code's examples appreciated :)
Thanks in advance
You should use HtmlAgilityPack for that, it makes working with DOM a lot more easier, than parsing it yourself.
http://htmlagilitypack.codeplex.com/
For example, here is how to get all divs
var elements = hdoc.DocumentNode.Descendants("div") /.Where(.your conditions..))/;

Invoice Print in vb.net or c#

I'm making a simple one-form Invoice bill printing, below is the screenshot to illustrate it.
I want to generate a report from the existing form after entering the data, simply static without database. Only the combo-box values are fetched from the database.
How can I generate that report after clicking the print button. The resultant report should not have combo-box, it should simply have a label instead of it.
I would always prefer to generate html files as reports. Its very flexible and printer friendly.
Create a html template with place holders and just replace with your required data.
You can do it via html like cloudlight suggest. But I have done it in another way using richtextboxcontrol. This is the link for the class file.
http://support.microsoft.com/kb/812425
I did receipt printing for POS machine before, I found it very easy to create a rtf file as my template. Then using the richtextboxcontrol, i read in the rtf file content, and replace the segment I want to changes.
For example, if name is a variable, in my rtf file, I would put something like <NAME>, after reading, all I need to do is to use string.replace.
Depends on how you customized it, I did it to the extend that I was able to change single variable and multiple lines of item using the same template.

HTML tags inside the Database - YAY or NAY?

I am using ASP.NET with C# and SQL Server 2005. I have a plane simple ASP.NET label on my form.
<asp:Label ID="lblBodyCopy1" runat="server"></asp:Label>
On Page load i populate this label with text that is saved in my database.
<p class="r_box_A">text text</p><p class="r_box_B">text text text</p>
Is this the right thing to do? If not what is then because i need to populate that label with text in such way that will have paragraphes and styles to each paragraph.
Thanks in advance!
Generally, no. You want your DB fields to be independant of the medium they'll be used in (because there may be many: HTML, Plain Text, etc)
What you should do is reformat your fields to whatever context they are being displayed. This implies that if you need the data in a 'richer' format, you typically use a markup scheme (markdown, perhaps) so you can process it and render it in whatever fashion you like.
You may, however, cache this formatting in another field, to speed up the retrieval process.
You can have HTML code in your Label as well as your Literal. A Literal control is much more light weight than a Label.. it's meant to write out text/html directly to the browser. A Label is little bulkier than a Literal, but has all benefits of a WebControl such as styling options etc.
Literal Control : Asp.NET Literal
Who enters these strings to the database? The usual problem with this approach is that:
you need to make sure that the resulting html is well formed
it can be not secure if people include scripts in the html stored in the database, etc.
So in most cases you want to store only real text in the database, and then format it and escape all html chars when adding to the page.
I personally would avoid storing the HTML in the database... but then again it depends on the context.
If you have a table designed for storing input from a rich textbox then you will somehow need to store any formatting that has been applied.
As others have mentioned, prehaps a safer alternative would be to create your own [bbCode] syntax, pretty much similar to the way the textbox on here (Stack OverFlow) works...
eg. any text in bold 'foo'
becomes [b]foo[/b]
There is added work involved because you will need to parse the text and replace any tags with the equivalent [bbCode] tags and then do the reverse when outputting to a web page, but I personally think it is a safer approach with regards to data integrity.
Why not store it as xhtml in the database - you can validate it before you push it in. use the XML field type. This way if your need to query on it you can, and you data is still structured.

displaying an image from a bitmap variable using codebehind

I wish to create a very simple web app that simply displays an Image(s) from a database based upon either a date (from a calendar click event) or from a keyword search, getting the image(s) is no problem but I don't know how to display the resulting image. Preferably I would like to display the image in a table or grid or whatever, adjacent to the Calendar and TextBox on the same page that I used to get the search critera
So how to I stream an image to a page from a codebehind bitmap variable ?
Edit:
The database is a cloud database, so databinding is out.
Take a look at the DynamicImageHandler for ASP.NET up on the CodePlex ASP.NET Futures site:
http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx
If you don't have the physical file on hand, try using:
Response.OutputStream.Write(bitmapbuffer, bitmapbuffer.Length)
I've done this in the past to dynamically serve pdfs and tifs, and it works quite nicely. You'll need to set your img's src attribute to contain the information you need. This web site has an example close to what you are looking for:
http://www.jigar.net/articles/viewhtmlcontent3.aspx
This isn't a good idea, since you don't know when to release the resource.
Rather, create a page (or a page handler, etc, etc) which will take the parameters you need in the querystring to perform the lookup in the database to get the image.
Then, in the handler, you load your image from the database and then stream the bytes back through the page.
In the original page, you set the src attribute of the img tag to the other page, with the appropriate parameters.

Categories

Resources