0
A
回答
0
你可以試試這個。您需要創建一個DocumentResource(Item-> Add new)並關聯一個圖像。
private static Footer BuildFooter(FooterPart hp, string title)
{
ImagePart ip = hp.AddImagePart(ImagePartType.Jpeg);
string imageRelationshipID = hp.GetIdOfPart(ip);
using (Stream imgStream = ip.GetStream())
{
System.Drawing.Bitmap logo = DocumentResources._default;
logo.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
Footer h = new Footer();
DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
Run r = new Run();
Drawing drawing = BuildImage(imageRelationshipID, "_default.jpg", 200, 30);
r.Append(drawing);
p.Append(r);
r = new Run();
RunProperties rPr = new RunProperties();
TabChar tab = new TabChar();
Bold b = new Bold();
Color color = new Color { Val = "000000" };
DocumentFormat.OpenXml.Wordprocessing.FontSize sz = new DocumentFormat.OpenXml.Wordprocessing.FontSize {Val = Convert.ToString("40")};
Text t = new Text { Text = title };
rPr.Append(b);
rPr.Append(color);
rPr.Append(sz);
r.Append(rPr);
r.Append(tab);
r.Append(t);
p.Append(r);
h.Append(p);
return h;
}
,並建立圖像
private static Drawing BuildImage(string imageRelationshipID, string imageName, int pixelWidth, int pixelHeight)
{
int emuWidth = (int)(pixelWidth * EMU_PER_PIXEL);
int emuHeight = (int)(pixelHeight * EMU_PER_PIXEL);
Drawing drawing = new Drawing();
d.Wordprocessing.Inline inline = new d.Wordprocessing.Inline { DistanceFromTop = 0, DistanceFromBottom = 0, DistanceFromLeft = 0, DistanceFromRight = 0 };
d.Wordprocessing.Anchor anchor = new d.Wordprocessing.Anchor();
d.Wordprocessing.SimplePosition simplePos = new d.Wordprocessing.SimplePosition { X = 0, Y = 0 };
d.Wordprocessing.Extent extent = new d.Wordprocessing.Extent { Cx = emuWidth, Cy = emuHeight };
d.Wordprocessing.DocProperties docPr = new d.Wordprocessing.DocProperties { Id = 1, Name = imageName };
d.Graphic graphic = new d.Graphic();
d.GraphicData graphicData = new d.GraphicData { Uri = GRAPHIC_DATA_URI };
d.Pictures.Picture pic = new d.Pictures.Picture();
d.Pictures.NonVisualPictureProperties nvPicPr = new d.Pictures.NonVisualPictureProperties();
d.Pictures.NonVisualDrawingProperties cNvPr = new d.Pictures.NonVisualDrawingProperties { Id = 2, Name = imageName };
d.Pictures.NonVisualPictureDrawingProperties cNvPicPr = new d.Pictures.NonVisualPictureDrawingProperties();
d.Pictures.BlipFill blipFill = new d.Pictures.BlipFill();
d.Blip blip = new d.Blip { Embed = imageRelationshipID };
d.Stretch stretch = new d.Stretch();
d.FillRectangle fillRect = new d.FillRectangle();
d.Pictures.ShapeProperties spPr = new d.Pictures.ShapeProperties();
d.Transform2D xfrm = new d.Transform2D();
d.Offset off = new d.Offset { X = 0, Y = 0 };
d.Extents ext = new d.Extents { Cx = emuWidth, Cy = emuHeight };
d.PresetGeometry prstGeom = new d.PresetGeometry { Preset = d.ShapeTypeValues.Rectangle };
d.AdjustValueList avLst = new d.AdjustValueList();
xfrm.Append(off);
xfrm.Append(ext);
prstGeom.Append(avLst);
stretch.Append(fillRect);
spPr.Append(xfrm);
spPr.Append(prstGeom);
blipFill.Append(blip);
blipFill.Append(stretch);
nvPicPr.Append(cNvPr);
nvPicPr.Append(cNvPicPr);
pic.Append(nvPicPr);
pic.Append(blipFill);
pic.Append(spPr);
graphicData.Append(pic);
graphic.Append(graphicData);
inline.Append(extent);
inline.Append(docPr);
inline.Append(graphic);
drawing.Append(inline);
return drawing;
}
調用這些功能,在您的功能構建文檔
FooterPart hp = mp.AddNewPart<FooterPart>();
string headerRelationshipID = mp.GetIdOfPart(hp);
SectionProperties sectPr = new SectionProperties();
FooterReference footerReference = new FooterReference();
FooterReference.Id = footerRelationshipID;
FooterReference.Type = HeaderFooterValues.Default;
sectPr.Append(footerReference);
b.Append(sectPr);
d.Append(b);
hp.Footer = BuildFooter(hp, "My Test");
相關問題
- 1. 將三個對齊圖像添加到word文檔頁腳
- 2. 如何在Word文檔中以頁腳形式插入圖像
- 3. 將圖像添加到word文檔中
- 4. 如何在MS Word 2010 Addin的頁腳中添加圖像?
- 5. 創建Word文檔並從.NET應用程序添加圖像
- 6. 如何使用python docx功能在word文檔中添加頁眉和頁腳?
- 7. 如何使用POI API在Word文檔中添加頁眉和頁腳?
- 8. 如何將Silverlight UiElement的圖像添加到Word文檔中
- 9. 如何在PDF文檔的Markdown中添加圖像作爲頁眉/頁腳
- 10. 添加頁腳(含圖片)到Word文檔使用POI API
- 11. 如何使用OpenXml將外部圖像添加到word文檔?
- 12. 使用Power Shell向Word文檔添加頁眉和頁腳
- 13. 編程方式添加頁腳Office Word中/ Excel文檔
- 14. Word文檔中的頁腳頁碼
- 15. 在RML文檔中添加頁腳
- 16. 如何在C#(.NET)中加載MS Word文檔的文本?
- 17. 在word文檔中添加gridview
- 18. 在頁腳中添加圖像drupal 7
- 19. 在.NET和Java中將Word文檔的第一頁呈現爲圖像
- 20. c#如何在word文檔的頁腳中獲取表格
- 21. 如何添加文本與CMYK ColorSpace的圖像在.NET 4
- 22. 如何使用Google Apps腳本在文檔中添加頁碼
- 23. 如何將圖像添加到php創建的MS Word文檔中?
- 24. 在C#中MS Word文檔的不同頁面添加形狀
- 25. 如何在DOCX4J中保存Word文檔中的圖像
- 26. 將圖像添加到Word文檔的指定位置
- 27. 將base64編碼圖像添加到Microsoft Word文檔
- 28. 如何在活動文檔中加載圖像? (Photoshop腳本)
- 29. 如何在按鈕中添加圖像?圖像與文字
- 30. 在word中添加多個圖像doc