簡體   English   中英

在ASP.Net C#Open Office XML中將ImagePart添加到docx

[英]Adding ImagePart to docx in ASP.Net C# Open Office XML

我不是Open Office XML的佼佼者,並且一直在努力嘗試將圖像添加到我正在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/>");
}

RelationshipId應該在那里,而不是imagesPath @ <a:blip r:embed=""" + imagesPath + @""" /> ,您可以將其作為string relationshipId = mainPart.GetIdOfPart(imagePart)進行獲取string relationshipId = mainPart.GetIdOfPart(imagePart)

您可以找到示例代碼@ msdn鏈接如何將圖片插入文字處理文檔

更新:您需要在兩個地方進行更改1)傳遞圖像部分的ID,如下所示: SetMainDocumentContent(mainPart, resumeText, mainPart.GetIdOfPart(newImage));

2)添加名稱空間,如下所示

公共靜態無效SetMainDocumentContent(MainDocumentPart部分,字符串resumeText,字符串relId){

        string docXml =   @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?><w:document xmlns:a=""http://schemas.openxmlformats.org/drawingml/2006/main"" xmlns:pic=""http://schemas.openxmlformats.org/drawingml/2006/picture"" xmlns:a14=""http://schemas.microsoft.com/office/drawing/2010/main"" xmlns:wpc=""http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"" xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns:m=""http://schemas.openxmlformats.org/officeDocument/2006/math"" xmlns:wp14=""http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"" xmlns:wp=""http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"" xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"" xmlns:w14=""http://schemas.microsoft.com/office/word/2010/wordml"" xmlns:wpg=""http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"" xmlns:wpi=""http://schemas.microsoft.com/office/word/2010/wordprocessingInk"" xmlns:wne=""http://schemas.microsoft.com/office/word/2006/wordml"" xmlns:wps=""http://schemas.microsoft.com/office/word/2010/wordprocessingShape"">    <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=""" + relId + @""" /> <!-- 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>

“;

        using (Stream stream = part.GetStream())
        {
            byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
            stream.Write(buf, 0, buf.Length);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM