How to output HTML in Silverlight 4 to a string (like HtmlTextWriter) - silverlight

I have a requirement to generate an HTML snapshot of an object - basically on my app you can click on the Clipboard button and generate either Text, BB Bode or HTML and it goes through the object and builds a string of text which is copied to the Clipboard. You can then paste this snippet on forums, blogs, websites (ie. a way of sharing).
What is the most efficient way of writing HTML output? In ASP.NET I would use HtmlTextWriter but I can't use the System.Web assembly in Silverlight. I could write the tags manually but I was hoping there was a better way.
Note: This is nothing to do with the current HTML page or displaying HTML in Silverlight or Silverlight in HTML. The requirements are valid ;)

The closest thing to HTmlTextWriter thats actually available in Silverlight is XmlWriter.
StringBuilder sb = new StringBuilder();
XmlWriter writer = new XmlWriter(sb);
// use writer to create html content.
string html = sb.ToString();
Not as slick as using HtmlTextWriter but better than using StringBuilder directly. Just watch out for those elements that need a closing tag such as <div></div>.

Related

Disable active content in WebBrowser control

I have embedded a WebBrowser control in my application and display content that I receive from a server. Specifically: The control is bound to a string which contains the error message from a rest call, that sometimes is HTML.
I wonder if there is a security risk if active content, e.g. JavaScript would be sent as part of the error message. Is there a way to instruct the WebBrowser control to disable all active content?
There are several ways to do:
First way is remove javascript from your string before pass it to browser, from Elian Ebbing's answer:
The quick 'n' dirty method would be a regex like this:
var regex = new Regex(
"(\\<script(.+?)\\</script\\>)|(\\<style(.+?)\\</style\\>)",
RegexOptions.Singleline | RegexOptions.IgnoreCase
);
string ouput = regex.Replace(input, "");
The better* (but possibly slower) option would be to use
HtmlAgilityPack:
HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(htmlInput);
var nodes = doc.DocumentNode.SelectNodes("//script|//style");
foreach (var node in nodes) node.ParentNode.RemoveChild(node);
string htmlOutput = doc.DocumentNode.OuterHtml;
*) For a discussion about why it's better, see this thread.
That way seem better and easier.
Second way is use WinForms webbrowser control, which allow you control lower level of browser, but this involve some invoking to WinAPI.
You can see this link for more info.

Render JSF tags fetched from database

I am trying o create a dynamic component that fetches the content from the a database a writes the content to the page. I am using
ResponseWriter writer = context.getResponseWriter();
writer.write(content)
This renders the html tags fine but when I use JSF tags such as h:input or ui:fragment and so on it does not evaluate them it just writes the raw output to the page. Is there a way around this? I tried the solution in Include dynamic content containing JSF tags/components from stream and https://stackoverflow.com/questions/32570134/jsf2-java-response-writer-to-render-facelets-tags (where #BalusC said it would take more than a couple of paragraphs to explain). Any help would be great.

How to create XPS file silently?

I have UIElement(Grid) with many components on it.
I want to save it as XPS document but without seeing any popup windows like SaveDialog Window.
How to do that?
I have for now something like this:
System.Printing.PrintQueue queue; //as "Microsoft XPS Document Writer"
System.Windows.Xps.XpsDocumentWriter writer =
PrintQueue.CreateXpsDocumentWriter(queue);
writer.Write(myUielement);
You can write any UI element directly to an XPS document without using the "print" functionality. Use the XpsDocument and XpsDocumentWriter classes as dictated here.

iTextSharp to generate PDF from WPF FixedDocument

I have a simple WPF app that displays and prints some
reports with a FixedDocument.
How can generate PDF's from that, with a free and open solution,
such as iTextSharp?
A WPF FixedDocument, also known as an XPS document, is a definite improvement over PDF. It has many capabilities that PDF lacks. In most cases it is better to distribute your document as XPS rather than PDF, but sometimes it is necessary to convert from XPS to PDF, for example if you need to open the document on devices that have only PDF support. Unfortunately most free tools to convert from XPS to PDF, such as CutePDF and BullzipPDF, require installing a printer driver or are not open source.
A good open-source solution is to use the "gxps" tool that is part of GhostPDL. GhostPDL is part of the Ghostscript project and is open-source licensed under GPL2.
Download GhostPDL from http://ghostscript.com/releases/ghostpdl-8.71.tar.bz2 and compile it.
Copy the gxps.exe executable into your project as Content and call it from your code using Process.Start.
Your code might look like this:
string pdfPath = ... // Path to place PDF file
string xpsPath = Path.GetTempPath();
using(XpsDocument doc = new XpsDocument(xpsPath, FileAccess.Write))
XpsDocument.CreateXpsDocumentWriter(doc).Write(... content ...);
Process.Start("gxps.exe",
"-sDEVICE=pdfwrite -sOutputFile=" +
pdfPath +
"-dNOPAUSE " +
xpsPath).WaitForExit();
// Now the PDF file is found at pdfPath
A simple way, which is easy, but probably not the most efficient way is to render the Fixed document to an image and then embed the image in a PDF using iTextSharp.
I have done it this way before successfully. Initially I tried to convert the control primitives (shapes) to PDF equivalents, but this proved too hard.
If you can get it into an image from WPF then you can import it into iTextSharp like they do in this article. You can even avoid the filesystem all together if you write it to a MemoryStream and then use that instead of using a FileStream.
http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
IF you want to do it programatically, your Best bet would be the following path XPS (Fixed Document) -> Print to PS -> Use Ghostscript to read the PS and convert to PDF.
If you dont care about reading the PDF back in the code, you can print to any one of the free PDF printers to which you can pass the destination path. This way your target PDF file will still be searchable if you have any test in your report.

Dynamic Hyperlink in Livecycle Form

I am trying to figure out how to make a hyperlink in a Livecycle Form which points to a URL which will change on different days that the form is rendered. For example on one day I might want the hyperlink to point to:
mywebsite/mypage?option=XXX
and on another day I want it to point to:
mywebsite/mypage?option=YYY
The XXX and YYY can be passed into the form's data pretty easily as XML, but I just don't know how to make it so that the hyperlink is changed to correspond to this.
Any suggestions?
This can be accomplished with JavaScript in LiveCycle Designer. The following script, placed on the Form's docReady event will let you dynamically change the URL of a text object.
form1::docReady - (JavaScript, client)
// If this code is running on the server, you don't want it to run any code
// that might force a relayout, or you could get stuck in an infinite loop
if (xfa.host.name != "XFAPresentationAgent") {
// You would load the URL that you want into this variable, based on
// whatever XML data is being passed into your form
var sURL = "www.stackoverflow.com"; // mywebsite/mypage?option=xxx
// URLs are encoded in XHTML. In order to change the URL, you need
// to create the right XHTML string and push it into the Text object's
// <value> node. This is a super simple XHTML shell for this purpose.
// You could add all sorts of markup to make your hyperlink look pretty
var sRichText = "<body><p>Foo</p></body>";
// Assuming you have a text object called "Text1" on the form, this
// call will push the rich text into the node. Note that this call
// will force a re-layout of the form
this.resolveNode("Text1").value.exData.loadXML(sRichText, false, true);
}
There are a couple of caveats: URLs in Acrobat are only supported in Acrobat 9.0 and later. So if someone using an older version of Acrobat opens your form, the URLs won't work.
Also, as you can see from the "if (xfa.host.name !=...)" line, this code won't run properly if the form is being generated on the server, because forcing a re-layout of a form during docReady can cause problems on certain older versions of the LiveCycle server. If you do need to run this script on the server, you should probably pick a different event then form::docReady.
I a number of complaints from users in WorkSpace that clicking links opened them in the same tab so they lost their WorkSpace form, and there's no option to change that in Designer 11. I think the solution I came up with for that would work for you too.
I made buttons with no border and no background, and in their click event have this line (in Javascript, run at client)
app.launchURL("http:/stackoverflow.com/", true);
It would be easy to add some logic to choose the right URL based on the day and it doesn't cause any form re-rendering.
In some spots where the hyperlink is in line with other text, I leave the text of the link blue and underlined but with no hyperlink, and just place the button (no background, no border, no caption) over it. Does require positioned and not flowed subforms for that to work, so depending on your layout it could get a little clunky.
Wow, just realized I am super late to the party. Well, for anyone using ES4 facing a similar problem . . .
Ended up using a 3rd party component to manipulate the PDF's hyperlinks...wish there was a better solution as this one costs about $1000.

Resources