How to redirect the input of ironruby to a textbox (WinForms & Silverlight 4) - ironruby

I'm building an IronRuby Console in silverlight 4 and WinForms (net4). I can redirect the output without problems:
MyRuntime = Ruby.CreateRuntime();
msOutput = new MemoryStream();
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8);
MyEngine = MyRuntime.GetEngine("rb");
MySource = MyEngine.CreateScriptSourceFromString("a='123'\nputs a", SourceCodeKind.Statements);
MySource.Execute();
textBox2.Text = ReadFromStream(msOutput);
Now, I want to redirect the input also, but always getting a 'nil' from the script:
MyRuntime = Ruby.CreateRuntime();
msOutput = new MemoryStream();
msInput = new MemoryStream();
MyRuntime.IO.SetOutput(msOutput, Encoding.UTF8);
MyRuntime.IO.SetInput(msInput, Encoding.UTF8);
MyEngine = MyRuntime.GetEngine("rb");
MySource = MyEngine.CreateScriptSourceFromString("a=gets\nputs a", SourceCodeKind.Statements);
byte[] byteArray = Encoding.UTF8.GetBytes("123");
msInput.Write(byteArray, 0, byteArray.Length);
MySource.Execute();
textBox2.Text = ReadFromStream(msOutput);
I cannot find any samples of redirecting the input, can you please send an example? Thank you.

I don't have any sample code immediately available but instead of using a MemoryStream you need to implement the stream. When reads on the stream occur you need to send the "contents" of the text box to the stream. You'll need some mechanism for determining when you send the contents - e.g. when the user hits return. You'll also probably need to setup a thread for blocking for the reads and probably use an AutoResetEvent to block until the text box signals that the input is complete.

Related

NPOI XWPF how can I place text on a single line that is both left & right justified?

I'm new to using NPOI XWPF and trying to create my first document, so far it's going well. The only issue I have left is trying to place text on the same line that is both left and right justified, I want it to look like:
Area: 1(Left Jstfd) Grade Level/Course: 10th Grade Reading (Right Jstfd)
Below is the code snippet I'm using, it's just pushing all the text together on the left side of the page...blah
XWPFParagraph p2 = doc.CreateParagraph();
p2.Alignment = ParagraphAlignment.LEFT;
XWPFRun r3 = p2.CreateRun();
r3.SetBold(true);
r3.FontFamily = "Times New Roman";
r3.FontSize = 12;
r3.SetText("Area: " + ah.schoolArea);
XWPFRun r4 = p2.CreateRun();
r4.SetBold(true);
r4.FontFamily = "Times New Roman";
r4.FontSize = 12;
r4.SetText("Grade Level/Course: " + ah.filterParm);
Before trying to accomplish a task in (N)POI, it's always good to realize how said task is accomplished in Microsoft Word itself. You can't simply split a paragraph half-way a line, what you do is
Add a tab stop at the end of the line
Set it to right-aligned.
Type text on the left, hit tab, type text on the right
Unfortunately, it doesn't seem XWPFParagraph exposes tabstop functionality at this point. However, XWPFParagraph is a wrapper around the CT_P class, which maps 1:1 onto the underlying Office XML format. Using reflection, we can access this private field and use it to directly add the tabstop.
Sample code:
var paragraph = document.CreateParagraph();
var memberInfo = typeof(XWPFParagraph).GetField("paragraph", BindingFlags.NonPublic | BindingFlags.Instance);
if (memberInfo == null)
{
throw new Exception("Could not retrieve CT_P from XWPFParagraph");
}
var internalParagraph = (CT_P) memberInfo.GetValue(paragraph);
CT_PPr pPr = internalParagraph.AddNewPPr();
CT_Tabs tabs = pPr.AddNewTabs();
CT_TabStop tab = tabs.AddNewTab();
tab.pos = "9000";
tab.val = ST_TabJc.right;
var run = paragraph.CreateRun();
run.SetText("Left aligned");
run.AddTab();
run = paragraph.CreateRun();
run.SetText("Right aligned");
Result:

print pdf silently in c#

I am trying to print pdf silently using adobe reader.
I have taken the example from the following location:
http://www.codeproject.com/Tips/598424/How-to-Silently-Print-PDFs-using-Adobe-Reader-and
I am able to work as desired with the above example code in my localhost.
But when I deploy my application on the server,I am unable to print the PDFs.
In my localhost on button click event,I am creating the PDFs and saving it to one location and printing the same.While printing adobe window opens and prints the PDFs and exits automatically.
The same doesn't work in my server.I am able to create and save PDFs,but adobe is not opening and printing my file.I am not even getting any exception/error.It simply doesn't show up adobe window.
Did anyone face the same issue.
Any help in this regard.
Thanks in advance.
EDIT:
If you are running on a Web Server using ASP.NET or in general IIS the new process executes on the Web server with restricted permissions. I point you out this answer that could explain the cause of your problem.
However the code you are using doesn't print any error message. You probably don't have access to the directory where the AcroRd32.exe is located.
Let's take this function from the article you posted:
public static Boolean PrintPDFs(string pdfFileName)
{
try
{
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.Verb = "print";
//Define location of adobe reader/command line
//switches to launch adobe in "print" mode
proc.StartInfo.FileName =
#"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
proc.StartInfo.Arguments = String.Format(#"/p /h {0}", pdfFileName);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
if (proc.HasExited == false)
{
proc.WaitForExit(10000);
}
proc.EnableRaisingEvents = true;
proc.Close();
KillAdobe("AcroRd32");
return true;
}
catch
{
return false;
}
}
PrintPDFs uses a process, which is called by the .NET framework using the Process class. In the StartInfo option you look carefully two options are set:
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
The first redirect the standard output stream to your application while the second hides the cmd window. The former is handy to use process without showing to the user a command window but the latter hide the console window. The main drawback is, if you're debugging, that you probably won't see error coming through.
One way to debug it would require to add the following to lines:
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
Console.WriteLine(proc.StandardOutput.ReadToEnd());
Another property you can look at is ExitCode. If that is greather than zero means that your process exit with some error.
hope it helps.
A silent printing can be achieved with an Acroread command line parameters or with a PDF JavaScript event handler (of course, if your PDF producer tool has a possibility to define/inject PDF's OpenAction handler).
See http://pd4ml.com/cookbook/pdf_automated_printing.htm
With the JavaScript approach you are not bound to a printer driver, network name or IP address. On the other hand, JavaScript in Acroread can be disabled, for example, by a corporate security policy.
use this with Ghostscript that is GNU:
ProcessStartInfo info = new ProcessStartInfo();
var FileName = #"C:\ResultadoFormulario_CClastMovements.pdf";
var pathPrinter = #"\\Server\namePrinter";
info.CreateNoWindow = true;
var pathGsw = #"path gswin64c here\";
info.WindowStyle = ProcessWindowStyle.Hidden;
string strCmdText = $"{pathGsw}gswin64c.exe -sDEVICE=mswinpr2 -dBATCH -dNOPAUSE -dNOPROMPT -dNoCancel -dPDFFitPage -sOutputFile=\"%printer%{direccionImpresora}\" \"{FileName}\"";
Process.Start("CMD.exe", strCmdText);

how to create a photo unique filename for isolated storage

I'm adding a Astronomy Picture of The Day to my Windows Phone Astronomy app, and I want to allow users to save the displayed photo to their media library. All of the examples I found show how to do this, but all of the filenames are hard coded and overwrite files that have the existing name. So I need a way to create a unique file name. How can I adjust this example to create a unique filename?
// Create a filename for JPEG file in isolated storage.
String tempJPEG = "fl.jpg";
// Create virtual store and file stream. Check for duplicate tempJPEG files.
var store = IsolatedStorageFile.GetUserStoreForApplication();
if (store.FileExists(tempJPEG))
{
store.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream fileStream = store.CreateFile(tempJPEG);
StreamResourceInfo sri = null;
Uri uri = new Uri("fl.jpg", UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
Thanks in advance for any help.
Provided you don't expect multiple saves per second
String tempJPEG = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")+".jpg";
Or some variant of that.
Just one way.

Playing a Media File from Isolated Storage

I am reading a wav file saved as a byte stream from a web service and want to play it back when my record is displayed. Phone 7 app.
My approach has been to save the byte stream to a wav file in isolated storage upon navigating to the record and subsequently set the source of my media player (MediaElement1) to that source when a button is clicked and play it back.
Below is my current code in my "PlayButton". (size matches byte stream but no audio results). If I set the stream to a WAV file stored as a resource it does work so perhaps I just need to know how to set the Uri to the Isolated storage file.
(e.g. following code works)
Mediaelement1.Source = new Uri("SampleData\\MyMedia.wav",UriKind.Relative) Works
Mediaelement1.Position = new TimeSpan(0,0,0,0) ;
Mediaelement1.Play() ;
Here is my code sample... any ideas?
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication() ;
IsolatedStorageFileStream str = new IsolatedStorageFileStream(
"MyMedia.wav", FileMode.Open, isf) ;
long size = str.Length;
mediaelement mediaelement = new MediaElement() ;
mediaelement.SetSource(str) ;
mediaElement1.Source = mediaelement.Source ;
mediaElement1.Position = new TimeSpan(0, 0, 0, 0);
mediaElement1.Play();
You shouldn't have to create 2 media elements. Just call .SetSource on mediaElement1 directly.
I have similar code which sets the MediaElement source to a movie in isolated storage and that works fine:
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isfs = new IsolatedStorageFileStream("trailer.wmv", FileMode.Open, isf))
{
this.movie.SetSource(isfs);
}
}
With the above, movie is a MediaElement I've already created in XAML and set autoPlay to true.
I did have a few issues with the above when first getting it working.
I suggest trying the following to help debug:
Ensure that the file has been written to isolated storage correctly and in it's entirety.
Handle the MediaFailed event to find out why it isn't working.
One thing I noticed is that when the device is tethered to the computer the Audio doesn't work... Spent a couple hours with this one when trying to listen to mp3 files.

append image to EXISTING pdf using itextsharp

The following code is very good at putting a single page into a pdf.
It does not work for subsequent pages.
If the stream is an existing pdf file the image is replaced. How do I get NewPage() to actually create a new page and add the image at the end.
using (Stream ms = GetStream()) {
Document doc = new Document(PageSize.A4);
var writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
if (!doc.NewPage())
throw new InvalidOperationException("NewPage failed.");
PDFImage jpg = PDFImage.GetInstance(image, ImageFormat.Jpeg);
jpg.Alignment = Element.ALIGN_CENTER;
jpg.ScaleToFit(PageSize.A4.Width, PageSize.A4.Height);
doc.Add(jpg);
doc.Close();
}
Calling doc.NewPage() doesn't do anything when there's nothing on the current page. There are at least 3 options:
1) Add something invisible to the current page. An empty paragraph, some white space to the PdfContentByte, whatever.
2) Tell your PDF document "no, its really not empty, take my word": PdfDocument.PageEmpty =false;
3) Don't throw when NewPage returns false. That's perfectly acceptable under the circumstances.
I'd go with #3 personally.

Resources