0

I am using Telerik:RadEditor component for Asp.Net to save openXML and html report, I need to download word file format from this html report using DocumentFormat.OpenXML library from Microsoft, the code i used working successfully but can't see image after download word file.

I tried the below references but image not showing also.

  1. https://learn.microsoft.com/en-us/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document

  2. https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.altchunk?view=openxml-2.8.1

  3. Add HTML String to OpenXML (*.docx) Document

  4. How to insert a non-inline picture into a word document using VB6?

  5. Adding image in word document


My Code is as follows:

protected void btnsavetoword_Click(object sender, EventArgs e)
    {
        try
        {

            clsReportTemplateBO objexist = new clsReportTemplateBO();
            objexist.RefNo = hdnRefNo.Value;
            objexist.CreatedBy = clsUser.UserName;

            List<clsReportTemplateBO> objReport = clsReportTemplateBL.GetSavedReports(objexist);

            if (objReport.Count > 0)
            {

                string ReportText = edrReport.Content;
                ReportText = ReportText.Replace(objReport[0].HeaderHTML, "");
                ReportText = ReportText.Replace(objReport[0].FooterHTML, "");
                objexist.ReportCode = objReport[0].ReportCode;

                foreach (clsReportTemplateBO datarow in objReport)
                {
                    if (datarow.ReportFormat == "Word")
                    {
                        objexist.PatientReportId = datarow.PatientReportId;
                    }
                }



                if (objReport.Count == 2)
                {
                    string PatientReportId = objexist.PatientReportId;
                    ConverttoWord(objexist.ReportCode, PatientReportId, ReportText);
                    rdWMngr.RadAlert("Report saved successfully", 400, 150, "Report", null, clsConstants.SucccessMessage);
                }
                else if (objReport.Count == 1 && objReport[0].ReportFormat == "HTML")
                {
                    objexist.ReportName = objReport[0].ReportName;
                    objexist.HID = objReport[0].HID;
                    objexist.CreatedBy = clsUser.UserName;
                    objexist.ReportFormat = "Word";
                    objexist.RefNo = hdnRefNo.Value;

                    if (clsReportTemplateBL.SavePatientReport(objexist))
                    {
                        string PatientReportId = objexist.PatientReportId;
                        ConverttoWord(objexist.ReportCode, PatientReportId, ReportText);
                        rdWMngr.RadAlert("Report saved successfully", 400, 150, "Report", null, clsConstants.SucccessMessage);
                    }
                }
                else if (objReport.Count == 1 && objReport[0].ReportFormat == "Word")
                {
                    string PatientReportId = objexist.PatientReportId;
                    ConverttoWord(objexist.ReportCode, PatientReportId, ReportText);
                    rdWMngr.RadAlert("Report saved successfully", 400, 150, "Report", null, clsConstants.SucccessMessage);
                }
            }
            else
            {
                clsReportTemplateBO obj = new clsReportTemplateBO();
                string ReportCode = HttpContext.Current.Session["ReportCode"].ToString();

                obj = clsReportTemplateBL.GetReportTemplate(ReportCode);

                obj.ReportName = ReportName;
                obj.ReportCode = ReportCode;
                obj.HID = HID;
                obj.CreatedBy = clsUser.UserName;
                obj.ReportFormat = "Word";
                obj.RefNo = hdnRefNo.Value;

                string ReportText = edrReport.Content;
                ReportText = ReportText.Replace(obj.HeaderHTML, "");
                ReportText = ReportText.Replace(obj.FooterHTML, "");

                if (clsReportTemplateBL.SavePatientReport(obj))
                {
                    string PatientReportId = obj.PatientReportId;
                    ConverttoWord(ReportCode, PatientReportId, ReportText);
                    rdWMngr.RadAlert("Report saved successfully", 400, 150, "Report", null, clsConstants.SucccessMessage);
                }
            }
        }
        catch
        {
            rdWMngr.RadAlert("Report saved failed", 400, 150, "Report", null, clsConstants.ErrorMessage);
        }
    }

ConvertToWord method as follows:

protected void ConverttoWord(string ReportCode, string PatientReportId, string ReportText)
    {
        string domain = HttpContext.Current.Request.Url.Authority;
        string RefNo = hdnRefNo.Value;
        int PatientReportDetailId = clsReportTemplateBL.SavePatientReportDetail(PatientReportId, RefNo, domain, clsUser.UserName);

        string sourceFile = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ReportFormatPath"].ToString() + ReportCode + ".docx");
        string destFile = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["SavedReportPath"].ToString() + RefNo + PatientReportDetailId.ToString() + ".docx");

        System.IO.FileInfo file = new System.IO.FileInfo(sourceFile);
        System.IO.FileInfo dest = new System.IO.FileInfo(destFile);
        if (file.Exists)
        {
            if (dest.Exists)
            {
                rdWMngr.RadAlert("File already converted. Try to download from Saved Report", 400, 150, "Saved Report", null, clsConstants.WarningMessage);
            }
            else
            {
                File.Copy(sourceFile, destFile);
            }

            exporttoword(destFile, ReportText);
        }
        else
        {

            rdWMngr.RadAlert("File not exist on " + sourceFile, 400, 150, "Saved Report", null, clsConstants.WarningMessage);
        }

    }

ExportToWord method as follows:

private void exporttoword(string FilePath, string ReportText)
    {
        using (WordprocessingDocument myDoc = WordprocessingDocument.Open(FilePath, true))
        {


            string altChunkId = "AltChunkId1";
            MainDocumentPart mainPart = myDoc.MainDocumentPart;

            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart("application/xhtml+xml"/*AlternativeFormatImportPartType.Html*/, altChunkId);

            //string stamp = "~/images/Menu/EStamp.png";  //Location of image that I need to insert into word when i save

            using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter streamWriter = new StreamWriter(chunkStream))
                {
                    streamWriter.Write(ReportText);
                }
            }


            AltChunk altChunk = new AltChunk();
            altChunk.Id = altChunkId;

            // this inserts altChunk after the last Paragraph
            mainPart.Document.Body
                .InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());

            mainPart.Document.Save();

        }

        //Download
        System.IO.FileInfo file = new System.IO.FileInfo(FilePath);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }
        else
        {
            rdWMngr.RadAlert("Error", 400, 150, "Saved Report", null, clsConstants.WarningMessage);
        }
    }


How can I insert image to the word file when I need to download it using DocumentFormat.OpenXML library in Asp.Net?

ali
  • 175
  • 1
  • 4
  • 21
  • 1
    What does the HTML look like? Did you inline the image or is it a href to an external source? – Thomas Barnekow Feb 12 '20 at 08:25
  • @ThomasBarnekow The photo inserted into Images website folder, and referenced by src attribute like this `EStamp` – ali Feb 13 '20 at 04:39
  • @ThomasBarnekow I tried to convert photo into **base64**, and it is working correctly, but the problem resolution of the image not the same resolution. – ali Feb 13 '20 at 04:48

0 Answers0