C# 如何在将word文档嵌入到单个文档中时使用文本创建图像

C# 如何在将word文档嵌入到单个文档中时使用文本创建图像,c#,openxml-sdk,embedding,C#,Openxml Sdk,Embedding,我正在使用System.IO.Packaging和OpenXMLSDK创建一个新的docx。在我的docx中,我想嵌入其他现有的docx文件。结果应该是一样的,我会做它在微软Word手册。对于每个嵌入的docx文件,docx中应该有一个图标。在图标中,我想添加嵌入的docx文件的名称。对于所有要嵌入的文件,我有相同的图像。但我不知道如何在嵌入图像时向图像中添加一些文本 这是我目前的代码: static void Main(string[] args) { const string co

我正在使用System.IO.Packaging和OpenXMLSDK创建一个新的docx。在我的docx中,我想嵌入其他现有的docx文件。结果应该是一样的,我会做它在微软Word手册。对于每个嵌入的docx文件,docx中应该有一个图标。在图标中,我想添加嵌入的docx文件的名称。对于所有要嵌入的文件,我有相同的图像。但我不知道如何在嵌入图像时向图像中添加一些文本

这是我目前的代码:

   static void Main(string[] args)
{
  const string containingDocumentPath = @"ContainingDocument.docx";

  const string embeddedDocumentPath = @"EmbeddedDocument.docx";

  CreatePackage(containingDocumentPath, embeddedDocumentPath);
}

private static void CreatePackage(string containingDocumentPath, string embeddedDocumentPath)
{
  using (WordprocessingDocument package = WordprocessingDocument.Create(containingDocumentPath, WordprocessingDocumentType.Document))
  {
    AddParts(package, embeddedDocumentPath);
  }
}

private static void AddParts(WordprocessingDocument parent,
                             string embeddedDocumentPath)
{
  var mainDocumentPart = parent.AddMainDocumentPart();
  GenerateMainDocumentPart().Save(mainDocumentPart);

  var embeddedPackagePart =
    mainDocumentPart.AddNewPart<EmbeddedPackagePart>(
      "application/vnd.openxmlformats-" +
      "officedocument.wordprocessingml.document",
      "rId1");

  GenerateEmbeddedPackagePart(embeddedPackagePart, embeddedDocumentPath);

  var imagePart = mainDocumentPart.AddNewPart<ImagePart>("image/x-emf", "rId2");
  GenerateImagePart(imagePart);
}

private static Document GenerateMainDocumentPart()
{
  var element =
    new Document(
      new Body(
        new Paragraph(
          new Run(
            new EmbeddedObject(
              new Shape(
                new ImageData()
                  {
                    RelationshipId = "rId2",
                  }
                )
                {
                  Id = "_x0000_i1025",
                  OptionalString = "abc",
                  Style = "width:16pt;height:16pt",
                },
              new OleObject()
                {
                  ProgId = "Word.Document.12",
                  ShapeId = "_x0000_i1025",
                  ObjectId = "_1299573545",
                  Id = "rId1"
                }
              )
              {
                DxaOriginal = "1531UL",
                DyaOriginal = "991UL"
              }
            )
          )
        )
      );

  return element;
}

public static void GenerateEmbeddedPackagePart(OpenXmlPart part, string embeddedDocumentPath)
{
  byte[] embeddedDocumentBytes;

  // The following code will generate an exception if an invalid
  // filename is passed.
  using (FileStream fsEmbeddedDocument = File.OpenRead(embeddedDocumentPath))
  {
    embeddedDocumentBytes = new byte[fsEmbeddedDocument.Length];

    fsEmbeddedDocument.Read(embeddedDocumentBytes, 0, embeddedDocumentBytes.Length);
  }

  using (var writer = new BinaryWriter(part.GetStream()))
  {
    writer.Write(embeddedDocumentBytes);
    writer.Flush();
  }
}

public static void GenerateImagePart(OpenXmlPart part)
{
  using (var writer = new BinaryWriter(part.GetStream()))
  {
    writer.Write(File.ReadAllBytes(@"Icons\MyImage.GIF"));
    writer.Flush();
  }
}
static void Main(字符串[]args)
{
const string containingDocumentPath=@“ContainingDocument.docx”;
常量字符串embeddedDocumentPath=@“EmbeddedDocument.docx”;
CreatePackage(包含DocumentPath、embeddedDocumentPath);
}
私有静态void CreatePackage(字符串containingDocumentPath,字符串embeddedDocumentPath)
{
使用(WordprocessingDocument包=WordprocessingDocument.Create(containingDocumentPath,WordprocessingDocumentType.Document))
{
添加部件(包、嵌入文档路径);
}
}
私有静态void AddParts(WordprocessingDocument父级,
字符串嵌入的文档路径)
{
var mainDocumentPart=parent.AddMainDocumentPart();
GenerateMainDocumentPart().Save(mainDocumentPart);
var嵌入包部件=
mainDocumentPart.AddNewPart(
“应用程序/vnd.openxmlformats——”+
“officedocument.wordprocessingml.document”,
“rId1”);
GenerateEmbeddedPackagePart(embeddedPackagePart,embeddedDocumentPath);
var imagePart=mainDocumentPart.AddNewPart(“image/x-emf”、“rId2”);
生成图像部件(图像部件);
}
私有静态文档生成器IndocumentPart()
{
变量元素=
新文件(
新机构(
新段落(
新运行(
新嵌入对象(
新形状(
新图像数据()
{
RelationshipId=“rId2”,
}
)
{
Id=“\u x0000\u i1025”,
OptionalString=“abc”,
Style=“宽度:16磅;高度:16磅”,
},
新对象()
{
ProgId=“Word.Document.12”,
ShapeId=“\u x0000\u i1025”,
ObjectId=“_1299573545”,
Id=“rId1”
}
)
{
DxaOriginal=“1531UL”,
DyaOriginal=“991UL”
}
)
)
)
);
返回元素;
}
公共静态void GenerateEmbeddedPackagePart(OpenXmlPart部分,字符串嵌入DocumentPath)
{
byte[]embeddedDocumentBytes;
//以下代码将生成一个异常,如果
//文件名已传递。
使用(FileStream fsEmbeddedDocument=File.OpenRead(embeddedDocumentPath))
{
embeddedDocumentBytes=新字节[fsEmbeddedDocument.Length];
读取(embeddedDocumentBytes,0,embeddedDocumentBytes.Length);
}
使用(var writer=newbinarywriter(part.GetStream()))
{
writer.Write(embeddedDocumentBytes);
writer.Flush();
}
}
公共静态void GenerateImagePart(OpenXmlPart)
{
使用(var writer=newbinarywriter(part.GetStream()))
{
writer.Write(File.ReadAllBytes(@“Icons\MyImage.GIF”);
writer.Flush();
}
}

为什么不使用PowerTools for openxml?它有一种将两个或多个docx文件合并为一个文件的简单方法。一般来说,如果答案中包含对代码意图的解释,以及为什么在不引入其他代码的情况下解决问题,那么答案会更有帮助。
    public static void GenerateEmbeddedIconPart(OpenXmlPart part, string filePath)
    {
        Byte[] image = GetIconBytes(filePath);
        using (var writer = new BinaryWriter(part.GetStream()))
        {
            writer.Write(image);
            writer.Flush();
        }
    }

    public static Byte[] GetIconBytes(string filePath)
    {
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(150, 60);
        bitmap.MakeTransparent();
        System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filePath);
        System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 8);
        System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);

        string drawText = Path.GetFileName(filePath);
        System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
        graphics.Clear(System.Drawing.Color.White);
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
        graphics.DrawIcon(icon, 20, 0);
        graphics.DrawString(drawText, drawFont, drawBrush, 0, 48);                      

        Byte[] data;
        using (var memoryStream = new MemoryStream())
        {
            bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
            data = memoryStream.ToArray();
        }

        drawFont.Dispose();
        drawBrush.Dispose();
        graphics.Dispose();
        return data;
    }