I'm creating a program that will generate a HTML page with information pulled from a database, some HTML is loaded into a webbrowser component and this webbrowser gets printed.
So in c#:
- There's a webbrowser component
- With some HTML code added into it
I'm trying to insert c# strings, textpulled from a database on specific places in the HTML that gets displayed in the browser component, but i have no clue how. I know in PHP for example, i am able to do between HTML text for example. Does something similar exist for C#? I'm working in winforms.
I'm looking for something like:
<html>
...
...
<h1>$String1</h1>
...
...
</html>
What I want to archive:
I scanned a transport protocol which has check boxes and text fields on it. I want to be able to display it in an Android app, fill in the needed data digital and make a new PDF (with a transparent background) out the digital added strings and checks (the empty protocol is already pre-printed => I just want to add the data).
What I tried:
I made a fill able PDF in Adobe Acrobat but I'm not able to display it in the app. I tried to embed it into a html file and display it like this (the .html and the .pdf file are both in the "Assets" directory)(the tutorial I followed):
<html>
<head></head>
<body>
<h1>TEST</h1>
<embed src="/TransportReport.pdf" width="500px" height="375px">
</body>
</html>
View view = inflater.Inflate(Resource.Layout.TransportReport, container, false);
view.FindViewById<WebView>(Resource.Id.wv_transportReport_id).LoadUrl("file:///android_asset/TransportReport.html");
but I alwas get this error ("TEST" gets displayed):
11-11 21:48:10.183 E/eglCodecCommon( 4135): **** ERROR unknown type 0x0 (glSizeof,72)
11-11 21:48:10.243 E/eglCodecCommon( 4135): glUtilsParamSize: unknow param 0x00000b44
11-11 21:48:10.243 E/eglCodecCommon( 4135): glUtilsParamSize: unknow param 0x00000bd0
And for the second part: Is it possible to extract the filled in data and "convert" it to a new PDF with a blank background? If not, does someone have a better idea how I can solve this?
Thanks in advance for every suggestion/solution.
I'm working on an email notification project, where in a preexisting Winforms screen the client can edit an email template - adding html, text, etc. A very simplified example input:
<!DOCTYPE html>
<html>
<head>
<title>The Title</title>
</head>
<body bgcolor="#f2f2f2" style="margin: 0; padding: 0;">
<br />
<b>Please do not respond to this e-mail, as it is not monitored.</b>
<br/>
<br/>
“Foo bar baz.
<br/>
<br/>
Baz bar foo.”
<br/>
</body>
</html>
This is saved as a string. On the same screen, the user may then click a button which will raise a ShowDialog call on another Form. This form previews the user's html in a WebBrowser control:
this.webBrowser.DocumentText = theHtmlString;
And the results:
Problem:
I am creating a WPF screen related to the Winforms screens mentioned. It too needs the ability to preview the user's html. To do so I've used an attached behavior modified from this version. Essentially, this dialog also previews the user's html in a WebBrowser control:
webBrowser.NavigateToString(theHtmlString);
However, the results aren't correct, as highlighted below:
If this were my own html input, I'd simply remove the offending characters and replace them with standard quotations. But since this input is from the client, how do I get WPF to render the same as Winforms?
The reason this poses an issue:
In the old Winforms screen, the user creates/edits-existing email templates, previews them, is satisfied with the rendered example, saves changes.
In the new WPF screen, the user exports/imports existing email templates, previews them, is dissatisfied with the rendered example and strange characters, becomes confused when the other screen still renders correctly - calls to report a "bug" in the new screen.
Simple Reproduction Example: - Credit to Eser
var encoded = WebUtility.HtmlEncode(" “ Test ” "); //" “ Test ” "
var buf = Encoding.UTF8.GetBytes(" “ Test ” ");
var str = Encoding.GetEncoding("Windows-1252").GetString(buf); //" “ Test †"
Just add this meta tag to the <head> of your HTML:
<meta charset='utf-8'>
This will display the special characters correctly. I just tested your exact code with this and it works.
I have a page which generates a html code in a text box.
When code generation happens parallel I am inserting the same code in a table.
All this is in between asp controllers.
Under that text box I have button called preview.
All I need is when I click on the button it should fetch the code and preview it like html file in other window.
If saving that code in not a good idea than help to give another alternate.
All I need is preview of that code in html format in other window.
You can easily do this with JavaScript. Open a new window and write the contents of your text box on the new window.
<script type="text/javascript">
function preview()
{
var hwnd=window.open('','preview','[window attributes such as height width etc.]');
hwnd.document.write(document.getElementById('<id of your textbox>').value);
hwnd.document.close();
}
</script>
on you preview button tag add onclick=preview()
Here are the requirements, the users needs to be able to view uploaded PDFs in the browser. They need to be able to add notes to the PDF and save the updated PDF to the server without having to save it to their machine and open it outside the browser.
Any ideas on how to achieve this are welcomed.
by the way I am working with an asp.net website (in C#).
I have no control over what the pdf looks like. It is uploaded client-side then other users need to view and an notes on top of the pdf.
The solution that I was thinking is to render the PDF to a jpeg and use javascript to plot coordinates of where the note should go.
here is a quick example of the html and javascript that create the json of note (using jQuery.)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<style type="text/css">
*
{
margin:0;
padding:0;
}
#PDF
{
position:absolute;
top:0;
bottom:0;
width:600px;
height:800px;
background:url(assets/images/gray.png) repeat;
float:left;
}
#results
{
float:right;
}
.comment
{
position:absolute;
border:none;
background-color:Transparent;
height:300px;
width:100px;
overflow:auto;
float:left;
top:0;
right:0;
font-family: Arial;
font-size:12px;
}
div.comment
{
padding-top:-20px;
}
.comment a.button
{
display:block;
padding-top:-20px;
}
</style>
</head>
<body>
<div>
<div id="PDF"></div>
<div id="results">
</div>
</div>
</body>
</html>
<script type="text/javascript" src="script/jquery.js"></script>
<script type="text/javascript">
var points = [];
$("#PDF").click(function(e) {
if ($("textarea.comment").length == 0) {
var that = this;
var txt = $("<textarea class='comment'></textarea>").css({ top: e.pageY, left: e.pageX }).blur(function() { $(this).remove(); }).keypress(function(e2) {
if (e2.keyCode == 13 && !e.shiftKey) {
var that2 = this;
$("#PDF").append($("<div class='comment'>").html(that2.value.replace(/\r/gi, "<br>")).css({ top: e.pageY, left: e.pageX }));
$(this).remove();
points.push({ "x": e.pageX, "y": e.pageY, "text": that2.value })
$("#results").append('{ "x": ' + e.pageX + ', "y": ' + e.pageY + ', "text": "' + that2.value + '" }<br/>');
}
});
$(this).append(txt);
txt.each(function() { this.focus(); })
}
});
</script>
So now I need to figure out how to:
Render a pdf to jpeg.
Recreate the PDF putting the annotations on top on it.
You can use GhostScript to render a PDF to JPEG.
Command line example:
gswin32c.exe -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -r300 -sOutputFile=output.jpg input.pdf
You need to call GhostScript via the command line version (as above) or use a wrapper.
A Google search turned up this blog post:
A Simple C# Wrapper for Ghostscript
For creating a new PDF you have two main alternatives:
Modify the JPEG and convert the JPEG into PDF (you can use GhsotScript for the conversion)
Use A PDF library that imports your original PDF and add data on top of that
For PDF libraries see this SO question:
Building PDF Files with C#
My company, Atalasoft, provides components that let you view document images, including PDFs and annotate them and save the annotations back into the PDF. In our product suite, you would need dotImage document imaging and the PDF Reader add-on. You would be using dotAnnotate through our AJAX web controls. Here is a link to our online demo - the document displayed is a TIFF, but you could use a PDF too.
I don't think you will be able to have a user load a pdf in their browser, edit it, then save it to the server without them saving it to their machine and then uploading it to the server.
What you can do is setup a webform with a database backend that can represent the pdf, and when they edit it you can regenerate the PDF using itextsharp and loading the information from the database, that way when the user goes back to edit the PDF you can prepopulate the form with what already exists.
itextsharp is extremely easy to use, here is an example:
string sourceFile = "path/to/pdfTemplate.pdf";
PdfReader reader = new PdfReader(sourceFile);
PdfStamper stamper = new PdfStamper(reader, new FileStream("path/to/store/pdf/filename.pdf", FileMode.Create));
AcroFields fields = stamper.AcroFields;
//now assign fields in the form to values from your form
fields.SetField("input1", input1.Text);
fields.SetField("input2", input2.Text);
//close the pdf after filling out fields
stamper.SetFullCompression();
stamper.FormFlattening = true;
stamper.Close();
then if you wanted to show the actual PDF you could easily
Response.Redirect("path/to/store/pdf/filename.pdf");
We do this using lowagie on a Spring/Java platform.
Users are presented with pre-generated sales tax returns and can add certain manual adjustments in a few fields. We then recompute totals fields based on their manual input and save the whole thing back to our DB.
You can use either PDFSharp or itextsharp to create annotations. Haven't tried PDFSharp annotation but iTextSharp does work. You'll have to handle the editing on the server side. probably copy the file to a temp folder edit it and save it back.
You'll find itextsharp at http://itextsharp.sourceforge.net, annotation example: bottom at the page http://itextsharp.sourceforge.net/tutorial/ch03.html
pdfsharp: http://www.pdfsharp.net
If you are able to buy a third party library I'd pretty much recommend TxTextControl. http://www.textcontrol.com/en_US/
With this control you can write an editor, that lets you use your pdf as a template and allows the user make changes and save them. All within the browser, without the need to manually select a tempfile on the computer. Acessing is pretty much like using the TextProperty of a normal TextBox.
You did not specify what technology limitations you have. If you can consider a Silverlight solution, and you have client computers that support Silverlight, you can easily do this.
Take a look at how Microsoft Sketchflow works, it permits the user to annotate documents in the web browser and the annotations are persisted back to the server.
Here is a company with a commercial control to annotate PDF (and other formats).
Microsoft does this in their Sketchflow player. Here is a video. Of course you would not be using sketchflow but rather implimenting something similar that meets your needs.
As an added bonus Silverlight 4 supports the clipboard as well as drag and drop so that end users could paste something onto the PDF immage as well as drag any file onto it ehich you would then be able to upload to your server.