Create a report from word or pdf documents - c#

My goal is to create report in WPF using dev express. I want to add word or pdf documents into report.
For word documents, I am opening word document into RichEditControl, saving it into memory stream as rtf document and using XRRichText control, I am adding rtf document into report.
public void CreateReport()
{
RichEditControl richEdit = new RichEditControl();
MemoryStream stream = new MemoryStream();
XRRichText rtfDoc = new XRRichText();
richEdit.LoadDocument(#"word document path", DocumentFormat.OpenXml);
richEdit.SaveDocument(stream, DevExpress.XtraRichEdit.DocumentFormat.Rtf);
rtfDoc.LoadFile(stream, XRRichTextStreamType.RtfText);
rtfDoc.WidthF = 550F;
this.Detail.Controls.Add(rtfDoc);
this.RequestParameters = false;
this.CreateDocument();
}
Is this a recommended way to add word document as rtf?
and also, Instead of word document, how can I add pdf document into report?
Thank you!

Check the XtraRichEdit - Document Server (available now in v2011, volume 1) blog post, which may be helpful.

Related

Duplicate Word Document Using OpenXML While Open Original Document

I need to create a same copy of existing word document and open it as another instance while the original first document being opened. The second word document do not save but user may have the option to save it or not.
This need to be done using OpenXML.
I will attached here the current implementation. This implementation is having several issues.
The first document need to close first before use it in WordprocessingDocument using statement.
The second newly created document need to save in local folder.
Code Initiation
var doc = Globals.ThisAddIn.Application.ActiveDocument;
doc.Save();
string fileName = doc.FullName;
doc.Close();
using (WordprocessingDocument document = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
}
Why do you need to use OpenXML ? With Interop you could simply:
Open the existing document
Copy everything within the document range
Create a new document
Paste the other document in the new one
It's done quickly and does the job perfectly

Open word document in a browser not working --asp.net mvc

I Want to open my word and PDF document in a browser window for display. Hwoever, my PDF document is opening fine but the word document fails. Below is my code.
public ActionResult ViewResume(long userid)
{
string FileName = getResumeName(userid);
string path = System.IO.Path.Combine(Server.MapPath("~/Content/files/"), FileName);
var fileStream = new FileStream(path,
FileMode.Open,
FileAccess.Read
);
FileInfo info = new FileInfo(FileName);
string extension = info.Extension;
FileStreamResult fsResult;
if (info.Extension == ".pdf")
fsResult = new FileStreamResult(fileStream, "application/pdf");
else
fsResult = new FileStreamResult(fileStream, "application/vnd.ms-word");
return fsResult;
}
When i try to open word document, It download a .exe file for me instead. I am not sure what the issue is. It opens PDF without an issue. Pls any help would be appreciated.
UPDATE
I am suspecting thE MIME type . I am not sure which is the right one for doc .
For Microsoft 2007 and 2010, here's a list for the Mime types:
OpenXML formats for Microsoft Office 2007/2010 MIME types
Microsoft Word 2007 document (.docx)
application/vnd.openxmlformats-officedocument.wordprocessingml.document
Microsoft PowerPoint 2007 presentation (.pptx)
application/vnd.openxmlformats-officedocument.presentationml.presentation
Microsoft Excel 2007 workbook (.xlsx)
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
And here's a complete list from MS
EDIT:
This solution will download the file , in order to open the word document inline in your browser, there is no native support for that , for many reasons , as you will need to consider the formatting, rendering,parsing ...
You will need to implement an external library for that , here's an example ASP.Net Document Viewer GroupDocs.Viewer

Converting html strings in Excel file to formatted word file with .NET

Input are Excel files - the cells may contain some basic HTML formatting like <b>, <br>, <h2>.
I want to read the strings and insert the text as formatted text into word documents, i.e. <b>Foo</b> would be shown as a bold string in Word.
I don't know which tags are used so I need a "generic solution", a find/replace approach does not work for me.
I found a solution from January 2011 using the WebBrowser component. So the HTML is converted to RTF and the RTF is inserted into Word. I was wondering if there is a better solution today.
Using a commercial component is fine for me.
Update
I came across Matthew Manela's MarkupConverter class. It converts HTML to RTF. Then I use the clipboard to insert the snippet into the word file
// rtf contains the converted html string using MarkupConverter
Clipboard.SetText(rtf, TextDataFormat.Rtf);
// objTable is a table in my word file
objTable.Cell(1, 1).Range.Paste();
This works, but will copy/pasting up to a few thousand strings using the clipboard break anything?
You will need the OpenXML SDK in order to work with OpenXML. It can be quite tricky getting into, but it is very powerful, and a whole lot more stable and reliable than Office Automation or Interop.
The following will open a document, create an AltChunk part, add the HTML to it, and embed it into the document. For a broader overview of AltChunk see Eric White's blog
using (var wordDoc = WordprocessingDocument.Open("DocumentName.docx", true))
{
var altChunkId = "AltChunkId1";
var mainPart = wordDoc.MainDocumentPart;
var chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId);
using (var textStream = new MemoryStream())
{
var html = "<html><body>...</body></html>";
var data = Encoding.UTF8.GetBytes(html);
textStream.Write(data, 0, data.Length);
textStream.Position = 0;
chunk.FeedData(textStream);
}
var altChunk = new AltChunk();
altChunk.Id = altChunkId;
mainPart.Document.Body.InsertAt(altChunk, 0);
mainPart.Document.Save();
}
Obviously for your case, you will want to find (or build) the table you want and insert the AltChunk there instead of at the first position in the body. Note that the HTML that you insert into the word doc must be full HTML documents, with an <html> tag. I'm not sure if <body> is required, but it doesn't hurt. If you just have HTML formatted text, simply wrap the text in these tags and insert into the doc.
It seems that you will need to use Office Automation/Interop to get the table heights. See this answer which says that the OpenXML SDK does not update the heights, only Word does.
Use this code it is working..
Response.AppendHeader("content-disposition", "attachment;filename=FileEName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
//Response.Write("Your HTML Code");
Response.Write("<table border='1 px solid'><tr><th>sfsd</th><th>sfsdfssd</th></tr><tr>
<td>ssfsdf</td><td><table border='1 px solid'><tr><th>sdf</th><th>hhsdf</th></tr><tr>
<td>sdfds</td><td>sdhjhfds</td></tr></table></td></tr></table>");
Response.End();
Why not let WORD do its owns translation since it understands HTML.
Read your Excel cells
Write your values into a HTML textfile as it would be a WORD document.
Open WORD and let it read that HTML file.
Instruct WORD to save the document as a new WORD document (if that is required).

Embed contents of a RTF file into a DOCX file using OpenXML SDK

In our old MSWord-97 based system we use COM to interact with a .doc file, and embed an OLE object, so the embedded document is visible in the parent (not as an icon).
We're replacing this with a system using OpenXML SDK since it requires having Word on our server, which generates .docx files. however we still need to embed the contents of RTF files into the generated DOCX... specifically we replace a bookmark with the contents of the file.
I found a few examples online but they all differ. When I create a simple example in Word and view the XML, there's a lot of stuff to position/display the embedded object's visual representation, while the embedding itself doesn't seem too horrific. What's the easiest way to do this?
You could embed the content of a RTF document into a OpenXML DOCX file
by using the AltChunk anchor for external content. The AltChunk (w:altChunk) element specifies
a location in your OpenXML WordprocessingML document to insert external content such as a RTF document.
The code below uses the AltChunk class in conjunction with the AlternativeFormatImportPart class
to embed the content of a RTF document into a DOCX file after the last paragraph:
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(#"your_docx_file.docx", true))
{
string altChunkId = "AltChunkId5";
MainDocumentPart mainDocPart = wordDocument.MainDocumentPart;
AlternativeFormatImportPart chunk = mainDocPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Rtf, altChunkId);
// Read RTF document content.
string rtfDocumentContent = File.ReadAllText("your_rtf_document.rtf", Encoding.ASCII);
using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(rtfDocumentContent)))
{
chunk.FeedData(ms);
}
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
// Embed AltChunk after the last paragraph.
mainDocPart.Document.Body.InsertAfter(
altChunk, mainDocPart.Document.Body.Elements<Paragraph>().Last());
mainDocPart.Document.Save();
}
If you want to embed an Unicode RTF string into a DOCX file then you have to escape the Unicode characters. For an example please refer to the following stackoverflow answer.
When you encounter the error "the file is corrupt" then ensure that you Dispose() or Close() the WordprocessingDocument. If you do not Close() the document then the releationship for the w:altchunk is not stored in the Document.xml.rels file.
This fella seemed to have figured it out with his own question and answer at How can I embed any file type into Microsoft Word using OpenXml 2.0

iTextSharp - How to generate a RTF document in the ClipBoard instead of a file

I would like to generate a PDF or RTF document using iTextSharp library that can be copied to the clipboard, using the same code I use to generate the document on a file (FileStream).
This way my application would give the user two options: generate to a file or to the clipboard.
Basically every iTextSharp document is attached to a System.IO.Stream.
Document doc = new Document(PageSize.A4);
RtfWriter2.GetInstance(doc, stream);
Usually we save the document to a file, using FileStream. To use the same code to paste the document in the Clipboard, we use a MemoryStream instead.
MemoryStream stream = new MemoryStream();
Document doc = new Document(PageSize.A4);
RtfWriter2.GetInstance(doc, stream);
// (...) document content
doc.Close();
string rtfText = ASCIIEncoding.ASCII.GetString(stream.GetBuffer());
stream.Close();
Clipboard.SetText(rtfText, TextDataFormat.Rtf);
I only had problems with Images: It seems that iTextSharp exports images saving the bytes of the image after the \bin tag. Some libraries put the binary content encoded as hex characters. When I paste (from memory) in Word, the images won't appear, but if I load from a file, everything is OK. Any suggestions?

Categories