[英]How to insert image into header of OpenXML Word document?
我的OpenXML Word文檔生成項目需要文本,表格和圖像。 但首先, 我需要一個帶有徽標(圖像)的文檔標題。
我已經使用Microsoft示例通過使用Open XML SDK 2.0 for Microsoft Office在Word 2007中使用頁眉和頁腳生成文檔創建頁眉和頁腳,文本標題工作正常,但圖像顯示在標題中損壞的圖像圖標, 正確大小的邊框 ,以及消息“此圖像當前無法顯示”。 此外,我可以將選定的圖像加載到文檔正文中。 以下是我創建ImagePart的方法:
// Create AG logo part.
_agLogoPart = mainDocumentPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(_agLogoFilename, FileMode.Open))
{
_agLogoPart.FeedData(stream);
}
_agLogoRel = mainDocumentPart.GetIdOfPart(_agLogoPart);
圖像使用從Microsoft示例派生的LoadImage方法加載,但添加寬度和高度參數,並返回Drawing對象:
private static Drawing LoadImage(string relationshipId,
string filename,
string picturename,
double inWidth,
double inHeight)
{
double emuWidth = Konsts.EmusPerInch * inWidth;
double emuHeight = Konsts.EmusPerInch * inHeight;
var element = new Drawing(
new DW.Inline(
new DW.Extent { Cx = (Int64Value)emuWidth, Cy = (Int64Value)emuHeight },
new DW.EffectExtent { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
new DW.DocProperties { Id = (UInt32Value)1U, Name = picturename },
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties { Id = (UInt32Value)0U, Name = filename },
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }))
{
Embed = relationshipId,
CompressionState = A.BlipCompressionValues.Print },
new A.Stretch(
new A.FillRectangle())),
new PIC.ShapeProperties(
new A.Transform2D(
new A.Offset { X = 0L, Y = 0L },
new A.Extents { Cx = (Int64Value)emuWidth, Cy = (Int64Value)emuHeight }),
new A.PresetGeometry(
new A.AdjustValueList()) { Preset = A.ShapeTypeValues.Rectangle })))
{
Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"
}))
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U,
EditId = "50D07946"
});
return element;
}
使用此代碼,以下代碼可以正常工作,將圖像加載到我想要的正文中:
Paragraph paraImage = new Paragraph(new Run(LoadImage(_genomeImageRel, _genomeImageFilename, "name" + _genomeImageRel, 7.5, 2.925)));
body.AppendChild(paraImage);
並且以下代碼無法在標頭中放置徽標圖像:
private static Header GeneratePageHeaderPart(string headerText)
{
Header hdr = new Header(new Paragraph(new Run(LoadImage(_agLogoRel, _agLogoFilename, "name" + _agLogoRel, 2.57, 0.73))));
return hdr;
}
我懷疑我做的事情非常微妙,因為我可以將圖像加載到標題中的任何位置。 任何人都可以建議嗎?
我懷疑我做的事情非常微妙,因為我可以將圖像加載到標題中的任何位置。
如果您能夠將圖像插入主文檔,您還可以使用此代碼在頁眉或頁腳中插入圖像( private static Drawing LoadImage
)。
唯一的區別是您添加ImagePart
:
要在文檔正文中插入圖像,請將ImagePart
添加到MainDocumentPart
:
_agLogoPart = mainDocumentPart.AddImagePart(ImagePartType.Jpeg); ... _agLogoRel = mainDocumentPart.GetIdOfPart(_agLogoPart);
要在標題中插入圖像,您需要將ImagePart
添加到用於創建標題的HeaderPart
_agLogoPart = headerPart.AddImagePart(ImagePartType.Jpeg); ... _agLogoRel = headerPart.GetIdOfPart(_agLogoPart);
要在頁腳中插入圖像,您需要將ImagePart
添加到用於創建頁腳的FooterPart
:
_agLogoPart = footerPart.AddImagePart(ImagePartType.Jpeg); ... _agLogoRel = footerPart.GetIdOfPart(_agLogoPart);
有關:
請嘗試以下代碼:
Bitmap image = new Bitmap(imagePath);
SdtElement controlBlock = doc.MainDocumentPart.HeaderParts.First().Header.Descendants<SdtElement>().Where
(r => r.SdtProperties.GetFirstChild<Tag>().Val == tagName).SingleOrDefault();
//find the Blip element of the content control
Blip blip = controlBlock.Descendants<Blip>().FirstOrDefault();
//add image and change embeded id
ImagePart imagePart = doc.MainDocumentPart.AddImagePart(ImagePartType.Jpeg);
using (MemoryStream stream = new MemoryStream())
{
image.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;
imagePart.FeedData(stream);
}
blip.Embed = doc.MainDocumentPart.GetIdOfPart(imagePart);
private static MainDocumentPart AddHeader(MainDocumentPart mainDocPart)
{
string LogoFilename = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Images/TemplateLogo.png");
mainDocPart.DeleteParts(mainDocPart.HeaderParts);
var FirstHeaderPart = mainDocPart.AddNewPart<HeaderPart>();
var DefaultHeaderPart = mainDocPart.AddNewPart<HeaderPart>();
var imgPartFirst = FirstHeaderPart.AddImagePart(ImagePartType.Jpeg);
var imagePartFirstID = FirstHeaderPart.GetIdOfPart(imgPartFirst);
var imgPartDefault = FirstHeaderPart.AddImagePart(ImagePartType.Jpeg);
var imagePartDefaultID = FirstHeaderPart.GetIdOfPart(imgPartDefault);
using (FileStream fs = new FileStream(LogoFilename, FileMode.Open))
{
imgPartFirst.FeedData(fs);
}
using (FileStream fsD = new FileStream(LogoFilename, FileMode.Open))
{
imgPartDefault.FeedData(fsD);
}
var rIdFirst = mainDocPart.GetIdOfPart(FirstHeaderPart);
var FirstHeaderRef = new HeaderReference { Id = rIdFirst, Type = HeaderFooterValues.First };
var rIdDefault = mainDocPart.GetIdOfPart(DefaultHeaderPart);
var DefaultHeaderRef = new HeaderReference { Id = rIdDefault };
var sectionProps = mainDocPart.Document.Body.Elements<SectionProperties>().LastOrDefault();
if (sectionProps == null)
{
sectionProps = new SectionProperties();
mainDocPart.Document.Body.Append(sectionProps);
}
sectionProps.RemoveAllChildren<HeaderReference>();
FirstHeaderPart.Header = GenerateFirstPicHeader(imagePartFirstID);
DefaultHeaderPart.Header = GeneratePicHeader(imagePartDefaultID);
FirstHeaderPart.Header.Save();
DefaultHeaderPart.Header.Save();
sectionProps.PrependChild(new TitlePage());
sectionProps.AppendChild(DefaultHeaderRef);
sectionProps.AppendChild(FirstHeaderRef);
return mainDocPart;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.