繁体   English   中英

在C#的演示文稿(Open XML SDK)上添加日期

[英]Add date on Presentations (Open XML SDK) on C#

我的参考是演示文稿(Open XML SDK)

通过下面的代码,我在现有的PowerPoint上添加了日期。

该代码有效,并且在第一张幻灯片中添加了日期,但是可以在页面上自定义该日期的插入点吗? 字体和大小?

这是当前输出:

在此处输入图片说明

预先感谢您的帮助。

string fileName = @"C:\\inetpub\\wwwroot\\aspnet\\Template\\01_FOCUS.pptx";

using (PresentationDocument oPDoc = PresentationDocument.Open(fileName, true))
{
            PresentationPart oPPart = oPDoc.PresentationPart;
            SlideIdList slideIdList = oPPart.Presentation.SlideIdList;
            SlidePart sp = slideIdList.ChildElements
           .Cast<SlideId>()
           .Select(x => oPPart.GetPartById(x.RelationshipId))
           .Cast<SlidePart>().First();
            AddDateToSlidePart(sp);
}

public static void AddDateToSlidePart(SlidePart slidePart1)
{
    Slide slide1 = slidePart1.Slide;
    CommonSlideData commonSlideData1 = slide1.GetFirstChild<CommonSlideData>();
    ShapeTree shapeTree1 = commonSlideData1.GetFirstChild<ShapeTree>();

    DocumentFormat.OpenXml.Presentation.Shape shape1 =
        new DocumentFormat.OpenXml.Presentation.Shape();

    DocumentFormat.OpenXml.Presentation.NonVisualShapeProperties nonVisualShapeProperties1 =
        new DocumentFormat.OpenXml.Presentation.NonVisualShapeProperties();

    DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties nonVisualDrawingProperties1 =
        new DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Date Placeholder 3" };

    DocumentFormat.OpenXml.Presentation.NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties1 =
        new DocumentFormat.OpenXml.Presentation.NonVisualShapeDrawingProperties();

    DocumentFormat.OpenXml.Drawing.ShapeLocks shapeLocks1 =
        new DocumentFormat.OpenXml.Drawing.ShapeLocks() { NoGrouping = true };

    nonVisualShapeDrawingProperties1.Append(shapeLocks1);

    ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 =
        new ApplicationNonVisualDrawingProperties();

    PlaceholderShape placeholderShape1 =
        new PlaceholderShape() { Type = PlaceholderValues.DateAndTime, Size = PlaceholderSizeValues.Half, Index = (UInt32Value)10U };

    applicationNonVisualDrawingProperties1.Append(placeholderShape1);
    nonVisualShapeProperties1.Append(nonVisualDrawingProperties1);
    nonVisualShapeProperties1.Append(nonVisualShapeDrawingProperties1);
    nonVisualShapeProperties1.Append(applicationNonVisualDrawingProperties1);

    DocumentFormat.OpenXml.Presentation.ShapeProperties shapeProperties1 =
        new DocumentFormat.OpenXml.Presentation.ShapeProperties();

    DocumentFormat.OpenXml.Presentation.TextBody textBody1 =
        new DocumentFormat.OpenXml.Presentation.TextBody();

    DocumentFormat.OpenXml.Drawing.BodyProperties bodyProperties1 =
        new DocumentFormat.OpenXml.Drawing.BodyProperties();

    DocumentFormat.OpenXml.Drawing.ListStyle listStyle1 =
        new DocumentFormat.OpenXml.Drawing.ListStyle();

    DocumentFormat.OpenXml.Drawing.Paragraph paragraph1 =
        new DocumentFormat.OpenXml.Drawing.Paragraph();

    DocumentFormat.OpenXml.Drawing.Field field1 =
        new DocumentFormat.OpenXml.Drawing.Field() { Id = "{528B97E8-8E4B-4D32-BA17-4F287283DFD6}", Type = "datetime1" };

    DocumentFormat.OpenXml.Drawing.RunProperties runProperties1 =
        new DocumentFormat.OpenXml.Drawing.RunProperties() { Language = "it-IT" };

    DocumentFormat.OpenXml.Drawing.Text text1 =
        new DocumentFormat.OpenXml.Drawing.Text();

    text1.Text = DateTime.Now.ToString("dd/mm/yyyy");

    field1.Append(runProperties1);
    field1.Append(text1);

    DocumentFormat.OpenXml.Drawing.EndParagraphRunProperties endParagraphRunProperties1 =
        new DocumentFormat.OpenXml.Drawing.EndParagraphRunProperties() { Language = "it-IT" };

    paragraph1.Append(field1);
    paragraph1.Append(endParagraphRunProperties1);
    textBody1.Append(bodyProperties1);
    textBody1.Append(listStyle1);
    textBody1.Append(paragraph1);
    shape1.Append(nonVisualShapeProperties1);
    shape1.Append(shapeProperties1);
    shape1.Append(textBody1);
    shapeTree1.Append(shape1);
}

OpenXML SDK生产力工具(可从Microsoft站点下载)是您的朋友。 当我需要做这样的事情时,我:

  1. 创建我想要的文档
  2. 在适当的工具(在这种情况下为PowerPoint)中将其打开。
  3. 进行微小的更改(以“弄脏”文档,并使PowerPoint认为需要更改)。
  4. 保存结果
  5. 对我要查看的文档进行更改,并以新名称保存结果
  6. 打开OpenXML生产力工具,然后单击“比较文件”工具。

我强制保存(在步骤3/4中)的原因是PowerPoint可以将其所有PowerPoint样式添加到我创建的文档中。 第二次保存(第5步)将必然具有所有这些内容,因此我希望两个文档尽可能地接近-只有“我要查看的文档的更改”是两个文档之间的区别。

此时,您应该看到解决问题的途径。 使用OOXML时,该工具必不可少。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM