I'm looking for a way to use JavaFX HTMLEditor's setHtmlText to add a local image. I can add a remote image no problem:
HTMLEditor editor = new HTMLEditor();
editor.setHtmlText("<img src=\"http://someaddress.com\" width=\"32\" height=\"32\" >");
but can't do this for a local image
HTMLEditor editor = new HTMLEditor();
editor.setHtmlText("<img src=\"categoryButton.fw.png\" width=\"32\" height=\"32\" >");
This button is on the same level as the java source. So why won't this work?
Use getResource to get the location of the local image resource.
editor.setHtmlText(
"<img src=\"" +
getClass().getResource("categoryButton.fw.png") +
"\" width=\"32\" height=\"32\" >"
);
It works the same way as loading content into WebView as in:
How to reach css and image files from the html page loaded by javafx.scene.web.WebEngine#loadContent?
Here is an screenshot:
And an executable sample:
import javafx.application.*;
import javafx.scene.Scene;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class HTMLEditorWithImage extends Application {
#Override public void start(Stage stage) {
HTMLEditor editor = new HTMLEditor();
editor.setHtmlText(
"<img src=\"" +
getClass().getResource("Blue-Fish-icon.png") +
"\" width=\"32\" height=\"32\" >"
);
stage.setScene(new Scene(editor));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
I was just curious if this was the only way of getting an image into a some sort of text area?
With JavaFX 2, you can embed images mixed with text into a FlowPane as described in Javafx Text multi-word colorization.
Java 8 adds a TextFlow component which allows you to embed an image into a text region.
Both of the above techniques are for data display only. Neither allow for editing of the multi-styled text with images and other nodes inserted in it. For now, the only controls provided by the JavaFX platform for this functionality are the HTMLEditor or a WebView with contenteditable true or an embedded 3rd party editor written in JavaScript.
There have been some 3rd party efforts to create styled text editors using native JavaFX constructs that don't rely on WebView or HTMLEditor, but as of today I don't think any are ready for widespread use.
Sample Code: Append file:\\ in the image tag to refer a local file.
ScreenCapture x = new ScreenCapture();
String imagePath = x.captureScreen(scCaptureCount+++"", "C:\\work\\temp");
String text = editor.getHtmlText();
editor.setHtmlText(text+"<img src='file:\\\\"+imagePath+"' >" );
This code insert image before the last end paragraph tag or end body tag.
String imgUrl = "http://..../image.png";
StringBuilder sb = new StringBuilder(htmlEditor.getHtmlText());
int pos = sb.lastIndexOf("</p>") > -1 ? sb.lastIndexOf("</p>") : sb.lastIndexOf("</body>");
sb.insert(pos, "<span><img src='" + imgUrl + "'></span>");
htmlEditor.setHtmlText(sb.toString());
Related
I have a custom RadComboBox with an image bound to it. It is not displaying the image, it's displaying the path to the image file in text.
public IDictionary<string, object> Attributes
{
get
{
//Fix for XT-2253
_attributes["DisplayStatusType"] =string.Format(null, "<div class=\"bed_priority_field\"><img src=\"../img/assignment_{0}.png\" /></div>","priority");
_attributes["Tooltip"] = Tooltip;
return _attributes;
}
}
How do I get it to display the image instead of the text?
You should use Templates in this case.
Check this online demo on how to do it:
http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/templates/defaultcs.aspx
The problem is that I am opening workflow designer dynamically from one shell application and I don't have reference to Canvas. I am able to save the WF4 as image but the image is not getting saved properly and contains left & top margins. I followed many articles to get it working but no success. I referred to following article as well.
Saving a canvas to png C# wpf
I am using the below function. I don't have any reference to canvas.
private BitmapFrame CreateWorkflowImage()
{
const double DPI = 96.0;
Visual areaToSave = ((DesignerView)VisualTreeHelper.GetChild(this.wd.View,
0)).RootDesigner;
Rect bounds = VisualTreeHelper.GetDescendantBounds(areaToSave);
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)bounds.Width,
(int)bounds.Height, DPI, DPI, PixelFormats.Default);
bitmap.Render(areaToSave);
return BitmapFrame.Create(bitmap);
}
Please help on this.
I am able to resolve the issue by referring to again the following link
Saving a canvas to png C# wpf
I got the reference to canvas by using following code
Visual canvas= ((DesignerView)VisualTreeHelper.GetChild(this.WorkflowDesigner1.View, 0)).RootDesigner;
This has resolved the border/margin issue.
Please have a look here: http://blogs.msdn.com/b/flow/archive/2011/08/16/how-to-save-wf4-workflow-definition-to-image-using-code.aspx
let’s look at how to generate an image of the workflow definition using the standard mechanism of WPF. After all, workflow designer canvas is a WPF control.
BitmapFrame CreateWorkflowDefinitionImage()
{
const double DPI = 96.0;
// this is the designer area we want to save
Visual areaToSave = ((DesignerView)VisualTreeHelper.GetChild(
this.workflowDesigner.View, 0)).RootDesigner;
// get the size of the targeting area
Rect size = VisualTreeHelper.GetDescendantBounds(areaToSave);
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height,
DPI, DPI, PixelFormats.Pbgra32);
bitmap.Render(areaToSave);
return BitmapFrame.Create(bitmap);
}
The above C# method is very straightforward. Just get the workflow diagram part of the workflow designer and create an in-memory image of it using some WPF API. The next thing is simple: create a file and save the image.
void SaveImageToFile(string fileName, BitmapFrame image)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(fs);
fs.Close();
}
}
At last, let’s try to invoke the above 2 methods in the OnInitialized() method to hook it up and then close the application.
protected override void OnInitialized(EventArgs e)
{
// ...
this.SaveImageToFile("test.jpg", this.CreateWorkflowDefinitionImage());
Application.Current.Shutdown();
}
I use Helvetica font and 14 px size for text. The problem is that if a page does not have any image on it the text is very clear, but in a page with at least 1 image the text is getting a little bold. You can see what I mean in images below:
* Without image on page
* With image on page
The correct font is the one that appear in picture #1. How to make all pages have the same font even if the page contains an image or not?
Thanks.
Sample code:
Document document = new Document(PageSize.LETTER);
document.SetMargins(docMargin, docMargin, docMargin, 25);
writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
document.Open();
Font defaultFont = FontFactory.GetFont("Helvetica", 7.8, Font.NORMAL, new Color(75, 75, 75));
document.Add(new Paragraph("Lorem ipsum lorem ipsum lorem ipsum", defaultFont));
document.Add(Chunk.NEWLINE);
Image img = Image.GetInstance("my png image path");
document.Add(img);
document.Close();
I was finally able to reproduce your problem. The first PNG that I tested with which didn't reproduce your problem I created from Photoshop and used the Save For Web command. The second PNG that I tested and was able to reproduce your problem I created from MSPAINT.EXE. I tried various combinations within Save For Web and none of them have the same problem as Paint.
According to this thread from the official iText mailing list it appears to be something about the color profile of the image.
What are you seeing is the impact of newly placed transparency into a
PDF that had not previously contained it, when consideration isn't
given for the blending colorspace of the final output document.
You have an RGB document that upon adding transparency is forced into
CMYK due to lack of explicit blending space. If you were to specify
RGB as your explicit blending space at the same time you added your
transparency, all would be well.
One thing they recommend is setting the following property on your PdfWriter before adding anything:
writer.RgbTransparencyBlending = true;
When I do it I still see a very minor shift but no where near as pronounced as without it.
This isn't an answer, I just need to be able to post code.
I'm unable to reproduce your results but if I were to guess it has something to do with your PDF renderer. You can confirm this by zooming in on the text, does it look the same when zoomed in? If so, that's your renderer trying to apply visual hints to a print document. If not, can you post a simplified version of your code that does this? Does this do this for all images or just one specific one? How are you creating your text, with Paragraphs, Tables, HTML parsing or something else? What version of iTextSharp are you using?
Below is a full working WinForms C# 2010 targeting iTextSharp 5.1.2.0 that creates a two page PDF. The first page has just text and the second page has text followed by an image loaded from the desktop. On my machine, using Adobe Acrobat Pro 9.1.3 I don't see any difference in fonts when I view it on screen.
using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
string pdfFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
string imgFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.png");
using (FileStream fs = new FileStream(pdfFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (Document doc = new Document(PageSize.LETTER)) {
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font f = new iTextSharp.text.Font(bf, 14);
doc.NewPage();
doc.Add(new Paragraph("This is a test", f));
doc.NewPage();
doc.Add(new Paragraph("This is a test", f));
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imgFile);
img.ScaleAbsolute(100, 100);
doc.Add(img);
doc.Close();
}
}
}
this.Close();
}
}
}
I am using webbrowser control to show local html content, cause the the background-color of the html page is black.
When I using NavigateToString method to navigate to the webbrowser, the webbrowser's background become white immediately, after a white coming the html page, then the background become black.
It's a little disturbing. Consider providing the best UX, I want to implement that the default background of the webbrowser is black.
Thx in advance.
Unfortunately this is a quirk of the WebBrowser control. I discovered the exact same issue when writing PhoneGap applications with WP7. The solution I came up with was to create a UI element that covers the WebBrowser control, wait for the content to be rendered, then fade out and hide the covering element, as described in this blog post.
Apply below code for your browser control.
public static readonly String StyleForBlackbody =
"<style type='text/css'>\n" +
" body {\n" +
" background-color : ??;\n" +
" font-size:$$px; \n" +
" font-family: ##;\n" +
" color: %%; \n" +
" };\n" +
"</style>\n";
public static String GetHtmlHead()
{
String html = StartHead;
html += StyleForBlackbody;
html = html.Replace("??", "auto");
html = html.Replace("%%", "black");
}
call above method.
SVG DOM can be controlled with JavaScript, so it can be AJAX-enabled... I wonder if there are some SVG components for Wicket yet. And if Wicket can have pure xml/svg as the output format.
Quick googling shows only a question at Code Ranch.
I don't know if you need a wicket-svg library anymore. But I have started a project at github to provide wicket components to work with svg.
Follow this link: wicket-svg
I don't know of components built, but Wicket definitely can have xml/svg as output format, and it's quite simple to make a Page that renders svg.
Dumb simple example code:
public class Rectangle extends Page {
#Override
public String getMarkupType() {
return "xml/svg";
}
#Override
protected void onRender(MarkupStream markupStream) {
PrintWriter writer = new PrintWriter(getResponse().getOutputStream());
writer.write(makeRectangleSVG());
writer.flush();
writer.close();
}
private String makeRectangleSVG() {
return "<?xml version=\"1.0\" standalone=\"no\"?>\n" +
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n" +
"\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n" +
"\n" +
"<svg width=\"100%\" height=\"100%\" version=\"1.1\"\n" +
"xmlns=\"http://www.w3.org/2000/svg\">\n" +
"\n" +
"<rect width=\"300\" height=\"100\"\n" +
"style=\"fill:rgb(0,0,255);stroke-width:1;\n" +
"stroke:rgb(0,0,0)\"/>\n" +
"\n" +
"</svg> ";
}
}
If you map this as a bookmarkable page and call it up, it does display a lovely blue rectangle, as per the hard-coded svg (stolen from a w3schools example). And of course you could easily parametrize the page and generate the svg instead of just sending a constant string...
I suspect it also wouldn't be hard to build a component based on the object tag so that svg could be shown as part of an html page rather than being the whole page like this, but I haven't yet tried to do so.