Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to direct print a html page from server

$
0
0

Hi

I want to print a string variable that contains some Html tags. 

How can I print content of this variable without javascript or ActiveX methods (like window.print)

I use "Interop" office tool. it's OK but when I published my project on IIS, I get a error like this:

Word was unable to read this document.  It may be corrupt.
Try one or more of the following:
* Open and Repair the file.
* Open the file with the Text Recovery converter.

but, when I open the file from Windows Explorer, it is't corrupt and open without any errors.

this is my print method: 

public void DoPrint(string text)        {            var path = System.Web.HttpContext.Current.Server.MapPath("~/") + DateTime.Now.Ticks + ".docx";            using (WordprocessingDocument doc = WordprocessingDocument.Create(path, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))            {                MainDocumentPart mainPart = doc.AddMainDocumentPart();                mainPart.Document = new Document();                Body body = mainPart.Document.AppendChild(new Body());                Paragraph para = body.AppendChild(new Paragraph());                Run run = para.AppendChild(new Run());                run.AppendChild(new Text(""));            }            String cid = "Chunkid";            using (WordprocessingDocument document = WordprocessingDocument.Open(path, true))            {                AlternativeFormatImportPart formatImportPart = document.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, cid);                using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(text)))                {                    formatImportPart.FeedData(ms);                    AltChunk altChunk = new AltChunk();                    altChunk.Id = cid;                    document.MainDocumentPart.Document.Body.Append(altChunk);                }                document.MainDocumentPart.Document.Save();                document.Close();            }            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();            wordApp.Visible = false;            Microsoft.Office.Interop.Word.Document doc1 = wordApp.Documents.Add(path);            wordApp.ActiveDocument.PrintOut();            doc1.Close(SaveChanges: false);            doc1 = null;            wordApp.Quit();            if (File.Exists(path))            {                File.Delete(path);            }        }

 


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>