簡體   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