Use JSP & HTML to read file and change display options? - html

I want to import a text file (not user import), for example:
Coding
There are several languages...
When the file is read, the first line should be in larger font and bolded, and the other lines can be kept in the text file format. Not sure how I can use JSP and link it to HTML

You've left out a lot of important details (Q: How is the JSP getting invoked? Q: Are you passing a filename in the URL? Etc. etc.)
But here's a simple example that might get you started in the right direction:
<%
BufferedReader reader = new BufferedReader(new FileReader("myile.txt"));
boolean firstLine = true;
String line;
while((line = reader.readLine())!= null){
if (firstLine) {
line = "<b>" + line + "</b>";
firstLine = false;
}
out.println(line);
}
reader.close();
%>

Related

Adobe Flash: How to read multiple lines from a .txt file?

I am creating a simple flash animation that displays text in a textField(textBox?).
var fl_TextLoader:URLLoader = new URLLoader();
var fl_TextURLRequest:URLRequest = new URLRequest("./liveStatus.txt");
fl_TextLoader.addEventListener(Event.COMPLETE, fl_CompleteHandler);
function fl_CompleteHandler(event:Event):void
{
var textData:String = new String(fl_TextLoader.data);
trace(textData);
text_feed_1.text_feed_1_text.text = textData;
}
However, when the text file has multiple lines, the text that gets displayed only contains the first line of that file, while the "trace" method displays everything in the console output. So I wonder how to display the additional lines from that text file in the textField.
Also, it would be great if these lines can be parsed into a string array. That would be even better because I can then manipulate the lines of that file.
Thank you very much!
Try to set the "multiline" (and maybe "wordWrap") value from your Textfield to true.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html
An to parse your string in a array you could use the split method from String like so:
var linesArray:Array = fl_TextLoader.data.split("\n");
\n = new line char. ("\r" or "\r\n" might alos do the trick depending on the encoding of your text file)

How to insert html in Microsoft Word Placeholder

I have this situation:
The user has an editor on his page and he enters text(with colors, formating, hyperlinks and he can also add pictures). When he clicks Submit the data from the editor(with the proper formating) must be sent to a specific placeholder in a Microsoft Office Word document.
I am using OpenXml SDK to write in the document and I tried HtmlToOpenXml so I can read the html.
I use HtmlToOpenXml and from the html string(from the user) I det a couple of paragraphs and now I have to insert them in the content control. Do you know how can I find the control and append them in it(if possible)
I managed to fix this and here is the code I used
//name of the file which will be saved
const string filename = "test.docx";
//html string to be inserted and rendered in the word document
string html = #"<b>Test</b>";
//the Html2OpenXML dll supports all the common html tags
//open the template document with the content controls in it(in my case I used Richtext Field Content Control)
byte[] byteArray = File.ReadAllBytes("..."); // template path
using (MemoryStream generatedDocument = new MemoryStream())
{
generatedDocument.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(generatedDocument, true))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
//just in case
if (mainPart == null)
{
mainPart = doc.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
}
HtmlConverter converter = new HtmlConverter(mainPart);
Body body = mainPart.Document.Body;
//sdtElement is the Content Control we need.
//Html is the name of the placeholder we are looking for
SdtElement sdtElement = doc.MainDocumentPart.Document.Descendants<SdtElement>()
.Where(
element =>
element.SdtProperties.GetFirstChild<SdtAlias>() != null &&
element.SdtProperties.GetFirstChild<SdtAlias>().Val == "Html").FirstOrDefault();
//the HtmlConverter returns a set of paragraphs.
//in them we have the data which we want to insert in the document with it's formating
//After that we just need to append all paragraphs to the Content Control and save the document
var paragraphs = converter.Parse(html);
for (int i = 0; i < paragraphs.Count; i++)
{
sdtElement.Append(paragraphs[i]);
}
mainPart.Document.Save();
}
File.WriteAllBytes(filename, generatedDocument.ToArray());
}

Add title to pdf using itext

{
Document document = new Document(PageSize.A3, 32, 32, 32, 32);
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "", "");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from test3");
List arrlist = new ArrayList();
while(rs.next()){
String xa =rs.getString("display");
if(xa.equals("1")){
arrlist.add(rs.getString("question_text"));
}
}
Collections.shuffle(arrlist);
for(int i=0;i<5;i++){
String str = (String) arrlist.get(i);
htmlWorker.parse(new StringReader(str));
htmlWorker.parse(new StringReader("<br>"));
}
document.close();
}
Above is a code snippet which gets data from the database and displays on pdf.How do I add a title, logo,date and page no.to this?Please if anyone could help.I am using itext.
Please check out some of the page event examples. Currently, you're creating an unnamed PdfWriter instance. Change this into a named writer instance and declare a page event before opening the document instance:
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
writer.setPageEvent(new MyPageEvents());
The MyPageEvents class is to be written by you. It needs to implement the PdfPageEvent interface, or, better yet, extend the PdfPageEventHelper class.
Adding a title, logo, date,... in the header is done by implementing the onEndPage() method. Do NOT use the onStartPage() method without reading the documentation!!!
Use these onEndPage() examples for inspiration.
To add an Image, add a member variable img to your page event, and add this to your constructor:
Image img = Image.getInstance(pathToImage);
img.setAbsolutePosition(36, 806);
Then in the onEndPage() method, do this:
writer.getDirectContent().addImage(img);
This will add the image at position x = 36, y = 806 (near the left top of the page). Note that it's important that you don't create a new Image instance for every new page, because that would result in a bloated PDF file. By creating the Image in the constructor of your page event and reusing the object, the image bytes will be present only once in the PDF file.

The image I add to a header doesn't appear

I'm trying to create a header with an image; the header is added, but the image is missing in header. My application is deployed on an Oracle Weblogic server (using Java EE and Hibernate).
I'm trying to create the image like this. getImage(seasonalFilter.getPictureFileId()).getAbsolutePath(). The image path is something like this: /tmp/6461346546165461313_65464.jpg.
Note that I want to add text under the image in the header (for every page).
public File convertHtmlToPdf(String JSONString, ExportQueryTypeDTO queryType, String htmlText, ExportTypeDTO type) throws VedStatException {
try {
File retFile = null;
FilterDTO filter = null;
HashMap<Object, Object> properties = new HashMap<Object, Object>(queryType.getHashMap());
filter = JSONCoder.decodeSeasonalFilterDTO(JSONString);
DateFormat formatter = new SimpleDateFormat("yyyy_MM_dd__HH_mm");
//logger.debug("<<<<<< HTML TEXT: " + htmlText + " >>>>>>>>>>>>>>>>");
StringBuilder tmpFileName = new StringBuilder();
tmpFileName.append(formatter.format(new Date()));
retFile = File.createTempFile(tmpFileName.toString(), type.getSuffix());
OutputStream out = new FileOutputStream(retFile);
com.lowagie.text.Document document = new com.lowagie.text.Document(com.lowagie.text.PageSize.LETTER);
com.lowagie.text.pdf.PdfWriter pdfWriter = com.lowagie.text.pdf.PdfWriter.getInstance(document, out);
document.open();
com.lowagie.text.html.simpleparser.HTMLWorker htmlWorker = new com.lowagie.text.html.simpleparser.HTMLWorker(document);
String str = htmlText.replaceAll("ű", "û").replaceAll("ő", "õ").replaceAll("Ő", "Õ").replaceAll("Ű", "Û");
htmlWorker.parse(new StringReader(str));
if (filter instanceof SeasonalFilterDTO) {
SeasonalFilterDTO seasonalFilter = (SeasonalFilterDTO) filter;
if (seasonalFilter.getPictureFileId() != null) {
logger.debug("Image absolutePath: " + getImage(seasonalFilter.getPictureFileId()).getAbsolutePath());
Image logo = Image.getInstance(getImage(seasonalFilter.getPictureFileId()).getAbsolutePath());
logo.setAlignment(Image.MIDDLE);
logo.setAbsolutePosition(0, 0);
logo.scalePercent(100);
Chunk chunk = new Chunk(logo, 0, 0);
HeaderFooter header = new HeaderFooter(new Phrase(chunk), true);
header.setBorder(Rectangle.NO_BORDER);
document.setHeader(header);
}
}
document.close();
return retFile;
} catch (Exception e) {
throw new VedStatException(e);
}
}
I really dislike your false allegation that "Every tutorial is based on C:\imagelocation\dsadsa.jpg"
I'm the author of two books and many tutorials about iText and I know for a fact that what you say doesn't make any sense. Take a look at my name: "Bruno Lowagie." You are using my name in your code, so please believe me when I say you're doing it completely wrong.
Instead of HTMLWorker, you should use XML Worker. HTMLWorker is no longer supported and will probably be removed from iText in the near future.
I see that you're also using the HeaderFooter class. This class has been removed several years ago. Please take a look at the newer examples: http://www.itextpdf.com/themes/keyword.php?id=221
These examples are written in Java; if you need the C# version, please look for the corresponding C# examples in the SVN repository.
Regarding images, you may want to read chapter 10 of my book.
Finally: please read http://lowagie.com/itext2

WPF RichTextBox with Inline HTML - i18n

I have a RichTextBox which I'm trying to use to display a translateable block of text containing hyperlinks. The problem I'm having is I can't find a way to set the text property without manually coding the s and controls into the content, which isn't translateable. Is there any way of doing this? I tried saving a simple RTF file containing one sentence using Word so I could extract the bits I need, but I end up with 160 lines of difficult to decipher RTF text.
Ideally HTML would be easier but this doesn't seem to be supported
I solved this by using the http://htmlagilitypack.codeplex.com/ to parse out the anchors.
public static IEnumerable<Inline> ParseHtml(string text)
{
var inlines = new List<Inline>();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(text);
if(doc.ParseErrors==null ||!doc.ParseErrors.Any()) {
foreach (var childNode in doc.DocumentNode.ChildNodes) {
switch(childNode.Name.ToLowerInvariant()) {
case "a":
var lnk = new Hyperlink(new Run(childNode.InnerText));
lnk.NavigateUri = new Uri(childNode.Attributes["href"].Value);
inlines.Add(lnk);
break;
default:
inlines.Add(new Run(childNode.InnerText));
break;
}
}
}
return inlines;
}