Free Support Forum - aspose.com

Adding text to multiple paragraphs failing (C# .NET)

I have an issue when I try to write multiple paragraphs in existing Shape. Only the first paragraph is written. I debug the code and I found that the Shape object as all the paragraphs I want. The problem is when I write to file I found only the first one. I share with you the project code.

   class Program
    {
        public static void Run()
        {
         
            string dataDir = ConfigurationManager.AppSettings["directoryToSave"];
            string srcDir = ConfigurationManager.AppSettings["Source"];
            string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string file = Path.Combine(appData, srcDir);
            using (Presentation presentation = new Presentation(srcDir))
            {
                IMasterLayoutSlideCollection layoutSlides = presentation.Masters[0].LayoutSlides;
                ILayoutSlide layoutSlide = null;

                foreach (ILayoutSlide titleAndObjectLayoutSlide in layoutSlides)
                {
                    if (titleAndObjectLayoutSlide.Name == "TITRE_CONTENU")
                    {
                        layoutSlide = titleAndObjectLayoutSlide;
                        break;
                    }
                }            

                var contenu = File.ReadAllText(@"E:\DemosProject\PF_GEN\PF_GEN\Source\contenu.txt", Encoding.UTF8);
                IAutoShape contenuShape = (IAutoShape)layoutSlide.Shapes.SingleOrDefault(r => r.Name.Equals("contenu"));

                ITextFrame txt = ((IAutoShape)contenuShape).TextFrame;
                txt.Paragraphs.Clear();
                string[] lines = contenu.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Where(str => !String.IsNullOrEmpty(str)).ToArray();
                for (int i = 0; i < lines.Length; i++)
                {
                    var portion = new Portion();
                    portion.Text = lines[i];
                    var paragraphe = new Paragraph();
                    paragraphe.Portions.Add(portion);
                    txt.Paragraphs.Add(paragraphe);
                }
                presentation.Slides.InsertEmptySlide(0, layoutSlide);
                presentation.Save(dataDir + "AddLayoutSlides_out.pptx", SaveFormat.Pptx);
            }
        }
       
        static void Main(string[] args)
        {
            try
            {
                var path = ConfigurationManager.AppSettings["sourceAsposeLicensePath"];
                License license = new License();
                license.SetLicense(path);
                Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error" + ex.Message);
            }
            finally
            {
                Console.WriteLine("Terminated");
                Console.ReadKey();
            }
        }
     
    }

You can find the ppt file (source file) in the attachement file. (https://gofile.io/?c=JpBDS8)

Lorem.png (16.6 KB)

Is there any thing that missing in my code?

Thanks

@radouani1984,

I have observed your requirements and suggest you to please try using following sample code on your end. You need to first add slides based on layout and then add text to that shape as per your requirements. The following code willl be helpful to you.

		public static void RunParaText()
		{

			string path = @"C:\Aspose Data\";

			string dataDir = path;
			string srcDir = path + "Master.pptx";
			//string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
			//string file = Path.Combine(appData, srcDir);
		
			using (Presentation presentation = new Presentation(srcDir))
			{
				IMasterLayoutSlideCollection layoutSlides = presentation.Masters[0].LayoutSlides;
				ILayoutSlide layoutSlide = null;

				foreach (ILayoutSlide titleAndObjectLayoutSlide in layoutSlides)
				{
					if (titleAndObjectLayoutSlide.Name == "TITRE_CONTENU")
					{
						layoutSlide = titleAndObjectLayoutSlide;
						break;
					}
				}

				var contenu = File.ReadAllText(dataDir+"contenu.txt", Encoding.UTF8);
				var slide=presentation.Slides.InsertEmptySlide(0, layoutSlide);
				IAutoShape contenuShape = (IAutoShape)slide.Shapes.SingleOrDefault(r => r.Name.Equals("contenu"));

				//IAutoShape contenuShape = (IAutoShape)layoutSlide.Shapes.SingleOrDefault(r => r.Name.Equals("contenu"));

				ITextFrame txt = ((IAutoShape)contenuShape).TextFrame;
				txt.Paragraphs.Clear();
				string[] lines = contenu.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Where(str => !String.IsNullOrEmpty(str)).ToArray();

				for (int i = 0; i < lines.Length; i++)
				{
					var portion = new Portion();
					portion.Text = lines[i];
					var paragraphe = new Paragraph();
					paragraphe.Portions.Add(portion);
					txt.Paragraphs.Add(paragraphe);
				}

				//Change font size w.r.t shape size
				contenuShape.TextFrame.TextFrameFormat.AutofitType = TextAutofitType.Normal;

				presentation.Save(dataDir + "AddLayoutSlides_out.pptx", SaveFormat.Pptx);
			}
		}
1 Like

it works like a charm :slight_smile: Thanks Fayyaz You are a life saver.

@radouani1984,

You are always welcome.

1 Like