Использование OpenXml для размещения образа base64 в MSWORD - PullRequest
0 голосов
/ 22 сентября 2019

Я пытаюсь использовать OpenXml для размещения строки base64, которая преобразуется в подпись в документе MS Word с SQL Server.Я не очень разбирался в том, как его использовать, но я решил что-то попробовать, и, поскольку я очень новичок в OpenXML, поэтому решил поставить это здесь.

Я успешно смог использовать контентконтролирует, чтобы определить, где будет отброшена подпись, я потерялся по линии, так как я довольно новичок в этом, я знаю открытие части файла, но тут возникает главная проблема, помещая строку base64 (после ее преобразования в изображение) для управления контентом и сохранения документа Word.

Код выглядит следующим образом:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace ContentControlTestWord
{
    public partial class PlaceimageText : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string constring = @"Data Source=DESKTOP-9CM4N5S\SQLEXPRESS;Initial Catalog=SignatureBox2;User ID=sa;Password=123456;";

            using (SqlConnection con = new SqlConnection(constring))
            {
                con.Open();
                string query = "select * from SignatureBox_DB where StaffID = @StaffID";

                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    cmd.Parameters.AddWithValue("@StaffID", TextBox1.Text);

                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        try
                        {
                            if (rd.Read())
                            {
                                string filePath = "~/ImagesSignature/";
                                string myFile = "sinature.jpg";
                                string fileName = Path.Combine(filePath, myFile);

                                byte[] imageBytes = Convert.FromBase64String(rd["SignatureBase64"].ToString());
                                MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
                                ms.Write(imageBytes, 0, imageBytes.Length);
                                File.WriteAllBytes(Server.MapPath(fileName), imageBytes);

                                string myphysicalPath = System.Web.HttpContext.Current.Server.MapPath(fileName);

                                using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(@"C:\Users\emi\Desktop\Jasmine.docx", true))
                                {
                                    SdtElement s3 = wordprocessingDocument.MainDocumentPart.Document.Descendants<SdtElement>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == "Jasmine").Single();
                                    //Save image in content control  and save document, where Jasmine is the name of the Content Control
                                }
                            }
                        }
                        catch(Exception ex)
                        {
                            Response.Write(ex.ToString());
                        }
                    }
                }
            }
        }
    }
}
...