私はOpenOfficeXMLが得意ではなく、C#/ASP.Netでプログラムで生成しているdocxファイルに画像を追加しようと苦労しています。履歴書のテキストブロックの上にロゴを配置しようとしています。本文部分(resumeText)のみで作業しているときにプログラムは正常に動作していましたが、画像を追加しようとコードを追加すると、「ファイル'..'。docxはできません」というエラーが表示されます。内容に問題があるため開いた後、詳細に「未宣言のプレフィックス」と表示されます。別の例からxmlを正しくコピーしたと思いましたが、機能しないようです。どこが間違っているのか誰かわかりますか?誰もが提供できる助けをありがとう。関連するコードは次のとおりです。
protected void ibtDownload_Click(object sender, ImageClickEventArgs e)
{
string attachmentDir = "~/tempDwnlds/";
string fileName = m_candidateUser.firstName + "_" + m_candidateUser.lastName + "_" + m_candidateUser.userID.ToString().Substring(0, 6);
string imagesPath = Server.MapPath("Images/Logo1.jpg");
CreateNewWordDocument(Server.MapPath(attachmentDir) + fileName + ".docx", Server.HtmlEncode(m_candidate.resume), imagesPath);
}
public static void CreateNewWordDocument(string document, string resumeText, string imagesPath)
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
{
// Set the content of the document so that Word can open it.
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
ImagePart newImage = wordDoc.MainDocumentPart.AddImagePart("images/jpeg");
using (Stream image = new FileStream(imagesPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
newImage.FeedData(image);
}
SetMainDocumentContent(mainPart, resumeText, imagesPath);
}
}
// Set the content of MainDocumentPart.
public static void SetMainDocumentContent(MainDocumentPart part, string resumeText, string imagesPath)
{
string docXml =
@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:body>
<w:p>
<w:r>
<w:drawing>
<wp:inline>
<wp:extent cx=""3200400"" cy=""704850"" /> <!-- describes the size of the image -->
<wp:docPr id=""2"" name=""Picture 1"" descr=""filename.JPG"" />
<a:graphic>
<a:graphicData uri=""http://schemas.openxmlformats.org/drawingml/2006/picture"">
<pic:pic>
<pic:nvPicPr>
<pic:cNvPr id=""0"" name=""filename.JPG"" />
<pic:cNvPicPr />
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed=""" + imagesPath + @""" /> <!-- this is the ID you need to find -->
<a:stretch>
<a:fillRect />
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:ext cx=""3200400"" cy=""704850"" />
</a:xfrm>
<a:prstGeom prst=""rect"" />
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t xml:space='preserve'>" + WrappableDocXText(resumeText) + @"</w:t>
</w:r>
</w:p>
</w:body>
</w:document>";
using (Stream stream = part.GetStream())
{
byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
stream.Write(buf, 0, buf.Length);
}
}
public static string WrappableDocXText(string source)
{
string nwln = Environment.NewLine;
return source.Replace(nwln, "<w:br/>");
}