【问题标题】:Issues regarding image and random row color for a table in openxml?关于 openxml 中表格的图像和随机行颜色的问题?
【发布时间】:2011-01-20 06:04:54
【问题描述】:

我正在使用 HTML 到 OpenXML 转换器,gridview HTML 代码被分配给一个字符串生成器。使用它,我们将该 HTML 转换为 OpenXML,但是当涉及到 word 时,会发现以下问题。

  1. 我必须用 grainsboro 颜色填充表格行的背景,但只填充文本背景,而不是完全填充单元格。

  2. 我想将页眉的图像向右对齐。

这是一个用 C# 完成的 asp.net 应用程序

A***building image code plz tell me how to align the image to right***
d=DocumentFormat.OpenXml.Drawing;
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 = 130, DistanceFromBottom = 430, DistanceFromLeft = 260, DistanceFromRight = 330   };
d.Wordprocessing.Anchor anchor = new d.Wordprocessing.Anchor { DistanceFromTop = 0, DistanceFromBottom = 0, DistanceFromLeft = 0, DistanceFromRight = 0  };
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.Wordprocessing.HorizontalPosition h = new d.Wordprocessing.HorizontalPosition(new d.Wordprocessing.HorizontalAlignment("right"));

 d.Graphic graphic = new d.Graphic();
// We don’t have to hard code a URI anywhere else in the document but if we don’t do it here 
// we end up with a corrupt document.
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);
//anchor.Append(extent);
//anchor.Append(docPr);
//anchor.Append(h);
//anchor.Append(graphic);

drawing.Append(inline);

return drawing;

我认为在 OpenXML 中有一个属性突出显示文本颜色。如何在 C# 代码中获得它?

【问题讨论】:

  • 您可能希望分享您的代码,以便其他人可以指出在哪里修复它,而不是说“请给我 C# 代码”。另外,由于您有 2 个问题,因此您应该将它们分开并打开 2 个单独的问题。

标签: c# html asp.net ms-office openxml


【解决方案1】:

其实你并没有直接设置绘图的对齐方式。例如,您将它放在段落中并设置其属性对齐方式。

这是我正在使用的代码:

public void AddImageParagraph(Drawing element, JustificationValues alignment = JustificationValues.Left)
{

    Paragraph paragraph = new Paragraph();
    ParagraphProperties paragraphProperties = new ParagraphProperties();
    Justification justification = new Justification()
    {
        Val = alignment
    };
    paragraphProperties.Append(justification);
    Run run = new Run(element);
    paragraph.Append(run);
    paragraph.Append(paragraphProperties);
    mBody.Append(paragraph);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-19
    • 2015-03-27
    • 2020-02-12
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多