使用C#在每张幻灯片中划分五个子弹

时间:2015-01-02 11:37:35

标签: c#

我想计算每张幻灯片中的子弹,如果幻灯片包含超过四个子弹,那么剩余的子弹将移动到新创建的幻灯片中,但如果剩余的子弹大于4,则将四个放入一个幻灯片中,并将扩孔移到另一张幻灯片上。如果幻灯片包含五个以上的项目符号,则此代码会计算每张幻灯片中的项目符号数,然后将项目符号移动到新创建的幻灯片中。但这不正常。有助于解决这个问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;

namespace PPTBullets
{
    class Program
    {
        static void Main(string[] args)
        {
            Application app = new Application();
            app.Visible = MsoTriState.msoTrue;
            Presentation pres = app.Presentations.Open(@"C:\test.pptx");
           Application app = new Application();
            app.Visible = MsoTriState.msoTrue;
            Presentation pres = app.Presentations.Open(fileName);
            int toAdd = 1;
            for (int slideCount = 1; slideCount <= pres.Slides.Count; slideCount++)
            {
                Slide slide = pres.Slides[slideCount];
                foreach (object item in pres.Slides[slideCount].Shapes) //get all objects in all slides
                {
                int bulletCount = 0;
                int paragraphCount=0;

                    var shape = (Microsoft.Office.Interop.PowerPoint.Shape)item;
                    if (shape.HasTextFrame == MsoTriState.msoTrue)
                    {
                        if (shape.HasTextFrame == MsoTriState.msoTrue)
                        {
                            Microsoft.Office.Interop.PowerPoint.TextFrame2 textFrame = shape.TextFrame2;
                            TextRange2 textRange = textFrame.TextRange;
                            TextRange2 ps = textRange.Paragraphs;
                            paragraphCount = ps.Count;
                            for (int i = 1; i <= paragraphCount; i++)
                            {
                                BulletFormat2 bulletFormat2 = ps.Item(i).ParagraphFormat.Bullet;
                                if (bulletFormat2.Type != MsoBulletType.msoBulletNone)
                                {
                                    //this paragraph has has bullet
                                    bulletCount++;
                                }
                            }
                            if (bulletCount > 4)
                            {        

                                //create new slide, cut this paragraph and paste in the new slide
                                Slide tempSlide = pres.Slides.AddSlide(2, pres.Slides[1].CustomLayout);
                                ps.Item(slideCount).Cut();
                               tempSlide.Shapes[2].TextFrame.TextRange.Paste();

                            }

                        }
                    }
                 }
            }
             pres.Save();
              pres.Close();
                app.Quit();
        }

2 个答案:

答案 0 :(得分:1)

您的子弹数量不在正确的位置。

internal class Program
{
    private static void Main(string[] args)
    {
        var app = new Application { Visible = MsoTriState.msoTrue };
        Presentation pres = app.Presentations.Open(@"C:\test.pptx");
        for (int slideCount = 1; slideCount <= pres.Slides.Count; slideCount++)
        { 
            int bulletCount = 0;
            foreach (object item in pres.Slides[slideCount].Shapes) //get all objects in all slides
            {
                var shape = (Microsoft.Office.Interop.PowerPoint.Shape)item;
                if (shape.HasTextFrame == MsoTriState.msoTrue)
                {
                    var paragraphs = shape.TextFrame2.TextRange.Paragraphs;
                    for (int i = 1; i <= paragraphs.Count; i++)
                    {
                        BulletFormat2 bulletFormat2 = paragraphs.Item(i).ParagraphFormat.Bullet;
                        if (bulletFormat2.Type != MsoBulletType.msoBulletNone)
                        {
                            //this paragraph has has bullet
                            bulletCount++;
                        }
                    }

                    if (bulletCount > 4)
                    {
                        //create new slide, cut this paragraph and paste in the new slide
                        Slide tempSlide = pres.Slides.AddSlide(2, pres.Slides[1].CustomLayout);
                        paragraphs.Item(slideCount).Cut();
                        tempSlide.Shapes[2].TextFrame.TextRange.Paste(); 
                    }
                }
            }
        }
        pres.Save();
        pres.Close();
        app.Quit();
    }
}

答案 1 :(得分:0)

public void DivideBullets(Presentation presentation,int slideIndex)         {

        var list = new ArrayList();
        new ArrayList();
        int toadd = 1;
        var newlst = new ArrayList();
        var str = new string[4];
        string strHeader = "";
        ArrayList lst = new ArrayList();
        int shapeCount = 0;
        foreach (object item in presentation.Slides[slideIndex].Shapes) //get all objects in all slides
            {
            int bulletCount = 0;
            int paragraphCount=0;
                var shape = (Microsoft.Office.Interop.PowerPoint.Shape)item;
                if (shape.HasTextFrame == MsoTriState.msoTrue)
                {
                    if (shape.HasTextFrame == MsoTriState.msoTrue)
                    {
                        shapeCount +=1;
                        if (shapeCount == 1)
                        {
                            strHeader = shape.TextFrame.TextRange.Text;
                        }
                        Microsoft.Office.Interop.PowerPoint.TextFrame2 textFrame = shape.TextFrame2;
                        TextRange2 textRange = textFrame.TextRange;
                        TextRange2 ps = textRange.Paragraphs;
                        paragraphCount = ps.Count;

                        for (int bullet = 1; bullet <= paragraphCount; bullet++)
                        {
                            BulletFormat2 bulletFormat2 = ps.Item(bullet).ParagraphFormat.Bullet;
                            if (bulletFormat2.Type != MsoBulletType.msoBulletNone)
                            {
                                //this paragraph has has bullet
                                bulletCount++;
                                //MessageBox.Show( ps.Item(i).Text);
                                if (bulletCount > 4)
                                {
                                    //create new slide, cut this paragraph and paste in the new slide
                                    list.Add(ps.Item(bullet).Text);
                                }
                                newlst.Add(ps.Item(bullet).Text);
                                lst.Add(ps.Item(bullet).Text);
                            }
                        }

                    }

                }

            }
            int slideCounter = 0;
            if (newlst.Count <= 4)
            {
                newlst.Clear();
            }
            else
            {
               // strHeader = newlst[0].ToString();
                newlst.CopyTo(0, str, 0, 4);
                AddNewPptSlide(presentation, slideIndex + toadd, str, strHeader,0,_standard); // adding new slide
                toadd += 1;
                str[0] = "";
                str[1] = "";
                str[2] = "";
                str[3] = "";
                slideCounter = slideCounter + 1;

            }
            if (list.Count > 0)
            {
                int xx = 0;

                for (int x = 0; xx < list.Count; x++)
                {
                    str[x] = list[xx].ToString();
                    if (x == 3)
                    {
                        AddNewPptSlide(presentation, slideIndex + toadd, str, strHeader, 1, _standard); // adding new slide
                        toadd += 1;

                        slideCounter = slideCounter + 1;
                        str[0] = "";
                        str[1] = "";
                        str[2] = "";
                        str[3] = "";
                        x = -1;
                    }
                    xx++;

                } //End for (int x = 0; xx < list.Count; x++)
                if (list.Count % 4 != 0)
                {
                    AddNewPptSlide(presentation, slideIndex + toadd, str, strHeader, 1, _standard); // adding new slide
                    toadd += 1;
                } // if (list.Count%4!= 0)

            } //if(list.Count>0)
            newlst.Clear();
            list.Clear(); //clear ArrayList
            strHeader = "";
            if (lst.Count > 4)
            {
                presentation.Slides[slideIndex].Delete();
            }
        }