ورود

View Full Version : چاب یک رشته حاوی html بدون واسطه



ali_shmki
سه شنبه 22 دی 1394, 14:30 عصر
سلام.
من یک رشته دارم حاوی کد html
قصد دارم اون کد رو که میتونه شامل هر نوع اطلاعاتی باشه رو مستقیم و بدون استفاده از جاوااسکریپت (window.print) و ActiveX چاپ کنم.

بدین معنی که سرور مستقیم دستور چاپ رو به پرینتر صادر کنه.
راهی که خودم امتحان کردم استفاده از کامپوننت های office و کلاس های WordprocessingDocument و فضای نامMicrosoft.Office.Interop.Word هست که متاسفانه با مشکل چاپ هنگام پابلیش روی IIS مواجه میشم:



"System.Runtime.InteropServices.COMException: 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."
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.AddAlternativeFormatImpo rtPart(AlternativeFormatImportPartType.Html, cid);
using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(te xt)))
{
formatImportPart.FeedData(ms);
AltChunk altChunk = new AltChunk();
altChunk.Id = cid;
document.MainDocumentPart.Document.Body.Append(alt Chunk);
}
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);
}
}