How to create XPS file silently? - wpf

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.

Related

Using itextsharp to set ocg state of existing pdf

I have spent several hours researching this and can't seem to locate the answer.
I have downloaded and referenced itextsharp in my wpf .net application. (VB)
What I am doing is needing to turn off a specific layer (ocg object) in an exisiting .pdf that was created in Autocad that is defaulted on.
I have successfully opened and displayed the .pdf but i can't seem to use the setOCGstate control correctly
pdf name is "random.pdf"
layer name that i can see once i open the .pdf is "Option 1"
where im getting stuck is i know the layer names are stored in an array inside the .pdf. i know the name of the layer i am trying to turn off, so how do i reference that layer and turn it off using the setocgstate.
example code
dim doc1 as New PdfReader("random.pdf")
PdfAction.SetOCGstate ("confused", False)
I've created an example that turns off the visibility of a specific layer. See ChangeOCG
The concept is really simple. You already have a PdfReader object and you want to apply a change to a file. As documented, you create a PdfStamper object. As you want to change an OCG layer, you use the getPdfLayers() method and you select the layer you want to change by name. (In my example, the layer I want to turn off is named "Nested layer 1"). You use the setOn() method to change its status, and you're done:
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Map<String, PdfLayer> layers = stamper.getPdfLayers();
PdfLayer layer = layers.get("Nested layer 1");
layer.setOn(false);
stamper.close();
reader.close();
This is Java code. Please read it as if it were pseudo-code and adapt it to your language of choice.

drag and drop Outlook attachment from Outlook in to a WPF datagrid

I saw this code ealier about drag and dropping attachment files (http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C) from Outlook into Windows Form and it works fine in windows Forms, but I can't seem to make it work with WPF.
I tried to simply change System.windows.form.IDataObject to System.Windows.IDataObject but it doesn't work (as I should have guessed).
I also simply tried to get the content of the e.Data FileContents but always get errors (which seems to be the case to everyone when I check on the web).
Did anyone ever did drag and dropping attachment from Outlook to WPF ? I am at a complete loss.
Edit : I am not trying to get file from a Windows Explorer windows ( I do but I know how to). It's really the whole getting attachment from Outlook directly that doesn't work . I am fully aware too that I could simple take the file from outlook into a temp folder and then drop it into my program, but I would like to avoid this unncessary step if possible.
so in the end I was able to find out a link where someone did exactly that :
https://gist.github.com/MattyBoy4444/521547
For those who wonders. Here is what I did exactly.
Create a new project in C# (my code is in VB) and add the code to it
Reference the new project in my main project to be able to use it
In my drop Event, check whether or not I had the "FileGroupDescriptorW" object in the drop data and called the method if I do to retrieve the files.
Here is the complete code
If obj.GetDataPresent("FileGroupDescriptorW") Then 'Outlook
Dim oOutLookObj As New Helpers.OutlookDataObject(e.Data)
Dim StrFiles() As String = oOutLookObj.GetData("FileGroupDescriptorW")
Dim contentStream() As System.IO.MemoryStream = oOutLookObj.GetData("FileContents")
' Do intended work...
End if
The names of the files are in StrFiles and the content are found in the streams. Both have the same array size and are order correctly.

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.

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

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>.

Open Word Document with WPF without open dialog

I have the path to a Word document saved in an SQL Database.
I am able to retrieve the path but I cannot work out the best approach to open the Word document from WPF without using the OpenFileDialog. I've given up any thoughts of embedding Word in WPF as it has too many gotchas.
I just want to be able to click a button or hyperlink and using the retrieved document path, open Word.
Try something like
Process wordProcess = new Process();
wordProcess.StartInfo.FileName = pathToYourDocument;
wordProcess.StartInfo.UseShellExecute = true;
wordProcess.Start();
By setting UseShellExecute to true it will open the document using the default program,
in your case Word.

Resources