Programmatically save open document in MS Word Add-In - c#

I'm trying to create an add-in in C# for MS Word 2010 that will add a new ribbon and a click event-handler. This click event-handler should save the active file in c:\temp, for example. And then I need to load the file content into a byte array.
Probably something like this:
public void ClickEventHandler(Office.IRibbonControl control)
{
string fileLocation = "c:\temp\test.docx";
Word.Document document = this.Document;
document.SaveAs(fileLocation);
byte[] byteArray = File.ReadAllBytes(fileLocation);
}
The point is, this is pseudo-code and I don't know how to load an active document into a byte array. If there is a way without saving the document it would be even better.
And a query if the active file is a docx (and not a doc file) would be nice as well.

Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;
document.SaveAs2(goldenpath + "\\" + name + "." + id + ".docx");
document.Close();

I use this generic function in my program to serialize arbitrary objects to a byte array:
private byte[] MakeByteSize<U>(U obj)
{
if (obj == null) return null;
var bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
var ms = new System.IO.MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
Edit:
After reading your additional content, I'm confident that serializing the Word.Document object won't get you what you need, since the byte array representing that object in the program (which is probably a wrapper around some COM interop) won't be the same as the byte array representing the information stored in the file about the document.
Looking at the MSDN article you referenced, it looks like what we really need is a WordprocessingDoc instance representing the document that we can pass to the HtmlConverter class. So I think the question you really want to ask is "How can I create a DocumentFormat.OpenXml.Packaging.WordprocessingDocument from an open document without saving the file first?"
Unforunately, I'm not sure that's possible since I'm not really spotting any methods on that class that would do that.
On the .doc vs .docx issue, the Open XML SDK for Microsoft Office says that it works with documentat that adhere to the "Office Open XML File Formats Specification" which I believe means it will only work with the .docx file format. You might have to try a different route on this, like exporting to PDF perhaps. Good luck!

Related

Convert ByteArray of Office document to ByteArray of PDF in C#

How can I convert a byte[] of an Office document (.doc, .docx, .xlsx, .pptx) to a byte[] of a PDF document assuming Office is installed and Microsoft.Office.Interop is used?
I fetch the files' byteArray from the database as well as their name.
I would like to first convert each file to a PDF and then combine all of the PDFs to one single PDF using PDFSharp (this part is already implemented).
Code:
foreach (Entity en in res.Entities)
{
byte[] fileByteArray = Convert.FromBase64String(en.GetAttributeValue<string>("documentbody"));
string fileName = en.GetAttributeValue<string>("filename");
string extension = fileName.Split('.')[1];
switch(extension)
{
case "doc":
case "docx":
byteArr.Add(ConvertWordToPdf(fileName, fileByteArray)); break;
case "xlsx":
byteArr.Add(ConvertExcelToPdf(fileName, fileByteArray)); break;
}
}
The problem is I'm not too sure how to implement these two methods.
I tried using the following code:
private byte[] ConvertWordToPdf(string fileName, byte[] fileByteArray)
{
string tmpFile = Path.GetTempFileName();
File.WriteAllBytes(tmpFile, fileByteArray);
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Document doc = app.Documents.Open(tmpFile);
// Save Word doc into a PDF
string pdfPath = fileName.Split('.')[0] + ".pdf";
doc.SaveAs2(pdfPath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF);
doc.Close();
app.Quit();
byte[] pdfFileBytes = File.ReadAllBytes(pdfPath);
File.Delete(tmpFile);
return pdfFileBytes;
}
But it saves the file to disk and that's something I would like to avoid. Is doing the same operation without saving to disk possible?
If you check the documentation for Documents.Open there is no mentioning of opening a document directly from a stream. This is unfortunately an all to common problem in libraries. But there might be other libraries you could use that allow this.
I would not expect saving to a file to be a major performance issue since the conversion will probably be the dominating factor. But it might cause permission issues if your program is running in a very restrictive environment.
If you are keeping the file save method you should add some exception handling to ensure the temporary files are deleted even if an exception occurs. I have also seen issues where external programs release the file locks after some time, so it might be useful to try to delete the file multiple times.

Writing and Reading CustomValues takes too long in PdfSharp

I have a c# method that writes a custom value for given pdf file. In order to write a custom value for a pdf, I am using PdfSharp 1.50.5147
The problem here is PdfReader.Open waits too long for the pdf belove :
https://www.mouser.com.tr/catalog/English/103/dload/pdf/mouser.pdf
public bool WritePropertyToFile(string filePath, string extension, string key, string value)
{
try
{
document = PdfReader.Open(filePath); //Here it lasts 2.5 minutes !!
var properties = document.CustomValues.Elements;
properties.SetString("/" + key, value);
document.Save(filePath);
document = null;
return true;
}
catch (Exception)
{
if (document != null)
document = null;
throw;
}
}
My requirement is to write and read custom values in miliseconds for a given file. Although lots of pdf files' custom values can be written and read in miliseconds, some of the files such as this one may cause problems for me.
Do I need to open whole document for writing or reading a custom value? Is there a different technique for this? Do you have suggestion for this problem?
Currently, there is no method to open, in this case, large pdf's quickly in PdfSharp due to the fact that PdfSharp first loads the entire pdf in memory. The pdf you're trying to open is a whopping 168MB file.
You may extend PdfSharp and try to load the trailer contents first and then read each block of contents according to trailer entries.

How to convert docx to html file using open xml with formatting

I know there are lot of question having same title but I am currently having some issue for them I didn't get the correct way to go.
I am using Open xml sdk 2.5 along with Power tool to convert .docx file to .html file which uses HtmlConverter class for conversion.
I am successfully able to convert the docx file into the Html file but the problem is, html file doesn't retain the original formatting of the document file. eg. Font-size,color,underline,bold etc doesn't reflect into the html file.
Here is my existing code:
public void ConvertDocxToHtml(string fileName)
{
byte[] byteArray = File.ReadAllBytes(fileName);
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
{
HtmlConverterSettings settings = new HtmlConverterSettings()
{
PageTitle = "My Page Title"
};
XElement html = HtmlConverter.ConvertToHtml(doc, settings);
File.WriteAllText(#"E:\Test.html", html.ToStringNewLineOnAttributes());
}
}
}
So I just want to know if is there any way by which I can retain the formatting in converted HTML file.
I know about some third party APIs which does the same thing. But I would prefer if there any way using open xml or any other open source to do this.
PowerTools for Open XML just released a new HtmlConverter module. It now contains an open source, free implementation of a conversion from DOCX to HTML formatted with CSS. The module HtmlConverter.cs supports all paragraph, character, and table styles, fonts and text formatting, numbered and bulleted lists, images, and more. See https://openxmldeveloper.org/
Your end result will not look exactly the way your Word Document turns out, but this link might help.
You might want to find an external tool to help you do this, like Aspose Words
You can use OpenXML Viewer extension for Firefox for Converting with formatting.
http://openxmlviewer.codeplex.com
This works for me. Hope this helps.

How could I make WebBrowser.Navigate analog for bytes array, instead of URL?

I planning to load PDF files into it, but I can't save those to disk. PDFs exist only as byte arrays in my program.
For text data I can use something like this:
webBrowser1.DocumentText = "<html>page content</html>";
But PDF is not text, so I need some other way, but can't find any.
I tried this:
byte[] file_content = File.ReadAllBytes("C:\\Users\\Metafalica\\Documents\\DatabaseSQLLanguageRzheutskaya.pdf");
MemoryStream ms = new MemoryStream(file_content);
ms.Flush();
ms.Position = 0;
webBrowser1.DocumentStream = ms;
But getting this:
It's not possible to load and render a PDF via webBrowser.DocumentStream. What happens behind the scene is that an instance of MSHTML Document Object gets created and initialized with the supplied stream. You could possibly load an image (which MIME type is recognized by MSHTML), but not a PDF. On the other hand, when webBrowser.Navigate is used, an instance of Adobe Acrobat Reader PDF Document gets created, rather than MSHTML.

Creating xlsx file

I am basically creating a xlsx file but I am getting an error while using that file like below.
System.IO.FileFormatException: Archive file cannot be size 0.
The way I tried.
string file = "c:\\DoneDone61.xlsx";
using(File.Create(file))
{
}
Also I cannot open excel file manually because it says the file is corrupted.
Thanks for answers in advance.
An Excel file which you consider to be "blank" is not just a file with no data in it (which is what you are creating). You can see this yourself by creating a document manually in Excel and then opening it in notepad. You'll notice that it actually has data inside of it. That data is used to store information regarding the three empty sheets named "Sheet1", "Sheet2" and "Sheet3". Also, there is some header information so that any program looking at the file knows that it is actually a compressed file (as per the Excel file format). So, as you can see, even a pretty empty excel file still contains SOME data.
If you want to create a blank excel document using C#, you have two good options:
Use a library that allows you to actually work with creating Excel documents that takes care of creating the file correctly. Check out something like the Microsoft OpenXML SDK or ExcelPackage.
Create an empty Excel document, store it somewhere, and when you want to "create" a new empty Excel document, just make a copy of this file.
This one ...
using(File.Create(filePath))
{
}
... creates an empty file. Read: really empty (=> 0 bytes) not an empty XLSX with an XLSX skeleton: ZIP container, file header, style definitions, ....
What exactly did you expect?
EDIT:
If you want to create an empty XLSX file (like "Right Click on Mouse > New > New Microsoft Excel"), you have to use such an template, ... and write it onto the disk.
To achieve that, you have to deploy this template file with your application, and then do a File.Copy(source, dest), or integrate it as a resource and write the resource content to the disk.
What you need to use is Interop.Excel namespace. Here's a guide from msdn
please use this...Its a bit hacky but couldn't get a better way to do this using InterOp
public static void CreateEmptyXLSXFile(string FilePath)
{
FileStream MyStream = new FileStream(FilePath, FileMode.CreateNew, FileAccess.ReadWrite);
MyStream.Write(ExcelDocumentsInterOps.GetEmptyXSLXFileBytes(), 0, ExcelDocumentsInterOps.GetEmptyXSLXFileBytes().Length);
MyStream.Flush();
MyStream.Close();
MyStream.Dispose();
MyStream = null;
}
/// <summary>
/// Returns the bytes for an empty xslx file
/// </summary>
/// <returns></returns>
public static byte[] GetEmptyXSLXFileBytes()
{
string TheSting = "UEsDBBQABgAIAAAAIQBYVsaPYAEAABgFAAATANoBW0NvbnRlbnRfVHlwZXNdLnhtbCCi1gEooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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XQQEAAF8CAAARAAAAAAAAAAAAAAAAAHgWAABkb2NQcm9wcy9jb3JlLnhtbFBLAQItABQABgAIAAAAIQCcPLtYiwEAADQDAAAQAAAAAAAAAAAAAAAAAPAYAABkb2NQcm9wcy9hcHAueG1sUEsFBgAAAAALAAsAygIAALEbAAAAAA==";
return Convert.FromBase64String(TheSting);
}
When dealing with spreadsheet related tasks in .NET, you can use this open source library called SpreadsheetLight to write an excel file (especially, if you want to write content at some point).
If you prefer adding it as package via Nuget, you can say:
Install-Package SpreadsheetLight
After that, going by GenerateReport() exmaple:
// this one creates an empty workbook
using (SLDocument sl = new SLDocument())
{
// sl.SetCellValue("B3", "I love ASP.NET MVC");
sl.SaveAs("c:\\DoneDone61.xlsx");
}
Also see their tutorial for more interesting stuff.

Categories