How to Copy Paragraph & Portion of Text in PPTX Presentation inside .NET Apps

Submitted on: 6/1/2016 8:05:18 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 2089
     This technical tip explains how .NET developers to copy paragraph & portion of text in PPTX presentation using Aspose.Slides inside their .NET applications. In order to format presentation text we need to format that on Paragraph and Portion level. There are some text properties that can be set on Paragraph level and some are set on Portion level. If there is a paragraph or portion in the text that we need to copy to newly added paragraphs or portions, we need to copy all properties of respective paragraph or portion to newly added paragraph or portion.

				This technical tip explains how .NET developers to copy paragraph & portion of text in PPTX presentation using Aspose.Slides inside their .NET applications. In order to format presentation text we need to format that on Paragraph and Portion level. There are some text properties that can be set on Paragraph level and some are set on Portion level. If there is a paragraph or portion in the text that we need to copy to newly added paragraphs or portions, we need to copy all properties of respective paragraph or portion to newly added paragraph or portion. The properties of the Paragraph can be accessed in ParagraphFormat instance of Pargraph class. We need to copy all the properties of source paragraph to target paragraph. In the following example, the CopyParagraph method is shared that takes paragraph to be copied as an argument. It copies all the properties of source paragraph to a temporary paragraph and return the same. The target paragraph gets the copied values.
//Copying a Paragraph
//[C# Code Sample]
//Function Call
IParagraph newPara=CopyParagraph(SourcePara);
//Function Definition//Function Definition
public static IParagraph CopyParagraph(IParagraph par)
Paragraph temp = new Paragraph();
// use CreateParagraphFormatData !!!
IParagraphFormatEffectiveData paraData = par.CreateParagraphFormatEffective();
// use ParagraphFormat to set values
temp.ParagraphFormat.Alignment = paraData.Alignment;
temp.ParagraphFormat.DefaultTabSize = paraData.DefaultTabSize;
temp.ParagraphFormat.MarginLeft = paraData.MarginLeft;
temp.ParagraphFormat.MarginRight = paraData.MarginRight;
temp.ParagraphFormat.FontAlignment = paraData.FontAlignment;
temp.ParagraphFormat.Indent = paraData.Indent;
temp.ParagraphFormat.Depth = paraData.Depth;
temp.ParagraphFormat.SpaceAfter = paraData.SpaceAfter;
temp.ParagraphFormat.SpaceBefore = paraData.SpaceBefore;
temp.ParagraphFormat.SpaceWithin = paraData.SpaceWithin;
temp.ParagraphFormat.Bullet.Type = paraData.Bullet.Type;
temp.ParagraphFormat.Bullet.Char = paraData.Bullet.Char;
temp.ParagraphFormat.Bullet.Color.Color = paraData.Bullet.Color;
temp.ParagraphFormat.Bullet.Height = paraData.Bullet.Height;
temp.ParagraphFormat.Bullet.Font = paraData.Bullet.Font;
temp.ParagraphFormat.Bullet.NumberedBulletStyle = paraData.Bullet.NumberedBulletStyle;
temp.ParagraphFormat.FontAlignment = paraData.FontAlignment;
temp.ParagraphFormat.RightToLeft = paraData.RightToLeft ? NullableBool.True : NullableBool.False;
temp.ParagraphFormat.EastAsianLineBreak = paraData.EastAsianLineBreak ? NullableBool.True : NullableBool.False;
temp.ParagraphFormat.HangingPunctuation = paraData.HangingPunctuation ? NullableBool.True : NullableBool.False;
return temp;
public static Paragraph CopyParagraph(Paragraph par)
//[VB.NET Code Sample]
'Function Call
Dim newPara As IParagraph=CopyParagraph(SourcePara)
'Function Definition
Public Shared Function CopyParagraph(ByVal par As IParagraph) As IParagraph
	Dim temp As New Paragraph()
	' use CreateParagraphFormatData !!!
	Dim paraData As IParagraphFormatEffectiveData = par.CreateParagraphFormatEffective()
	' use ParagraphFormat to set values
	temp.ParagraphFormat.Alignment = paraData.Alignment
	temp.ParagraphFormat.DefaultTabSize = paraData.DefaultTabSize
	temp.ParagraphFormat.MarginLeft = paraData.MarginLeft
	temp.ParagraphFormat.MarginRight = paraData.MarginRight
	temp.ParagraphFormat.FontAlignment = paraData.FontAlignment
	temp.ParagraphFormat.Indent = paraData.Indent
	temp.ParagraphFormat.Depth = paraData.Depth
	temp.ParagraphFormat.SpaceAfter = paraData.SpaceAfter
	temp.ParagraphFormat.SpaceBefore = paraData.SpaceBefore
	temp.ParagraphFormat.SpaceWithin = paraData.SpaceWithin
	temp.ParagraphFormat.Bullet.Type = paraData.Bullet.Type
	temp.ParagraphFormat.Bullet.Char = paraData.Bullet.Char
	temp.ParagraphFormat.Bullet.Color.Color = paraData.Bullet.Color
	temp.ParagraphFormat.Bullet.Height = paraData.Bullet.Height
	temp.ParagraphFormat.Bullet.Font = paraData.Bullet.Font
	temp.ParagraphFormat.Bullet.NumberedBulletStyle = paraData.Bullet.NumberedBulletStyle
	temp.ParagraphFormat.FontAlignment = paraData.FontAlignment
	temp.ParagraphFormat.RightToLeft = If(paraData.RightToLeft, NullableBool.True, NullableBool.False)
	temp.ParagraphFormat.EastAsianLineBreak = If(paraData.EastAsianLineBreak, NullableBool.True, NullableBool.False)
	temp.ParagraphFormat.HangingPunctuation = If(paraData.HangingPunctuation, NullableBool.True, NullableBool.False)
	Return temp
End Function
//Copying a PortionEx
// In the following example, the CopyPortion method is shared that takes portion to be copied as an argument. It copies all the properties of source portion to a temporary portion and return the same. The target portion gets the copied values.
//[C# Code Sample]
//Function Call
 IPortion newPortion=CopyPortion(SourcePortion);
//Function Definition
 public static IPortion CopyPortion(IPortion por)
 Portion temp = new Portion();
 //use CreatePortionFormatData!!!
 IPortionFormatEffectiveData portData = por.CreatePortionFormatEffective();
 // use PortionFormat to set values
 temp.PortionFormat.AlternativeLanguageId = portData.AlternativeLanguageId;
 temp.PortionFormat.BookmarkId = portData.BookmarkId;
 temp.PortionFormat.Escapement = portData.Escapement;
 temp.PortionFormat.FillFormat.FillType = por.PortionFormat.FillFormat.FillType;
 temp.PortionFormat.FillFormat.SolidFillColor.Color = portData.FillFormat.SolidFillColor;
 temp.PortionFormat.FontBold = portData.FontBold ? NullableBool.True : NullableBool.False;
 temp.PortionFormat.FontHeight = portData.FontHeight;
 temp.PortionFormat.FontItalic = portData.FontItalic ? NullableBool.True : NullableBool.False;
 temp.PortionFormat.FontUnderline = portData.FontUnderline;
 temp.PortionFormat.UnderlineFillFormat.FillType = portData.UnderlineFillFormat.FillType;
 temp.PortionFormat.UnderlineFillFormat.SolidFillColor.Color = portData.UnderlineFillFormat.SolidFillColor;
 temp.PortionFormat.IsHardUnderlineFill = portData.IsHardUnderlineFill ? NullableBool.True : NullableBool.False;
 temp.PortionFormat.IsHardUnderlineLine = portData.IsHardUnderlineLine ? NullableBool.True : NullableBool.False;
 temp.PortionFormat.KerningMinimalSize = portData.KerningMinimalSize;
 temp.PortionFormat.Kumimoji = portData.Kumimoji ? NullableBool.True : NullableBool.False;
 temp.PortionFormat.LanguageId = portData.LanguageId;
 temp.PortionFormat.LatinFont = portData.LatinFont;
 temp.PortionFormat.EastAsianFont = portData.EastAsianFont;
 temp.PortionFormat.ComplexScriptFont = portData.ComplexScriptFont;
 temp.PortionFormat.SymbolFont = portData.SymbolFont;
 temp.PortionFormat.TextCapType = portData.TextCapType;
 temp.PortionFormat.Spacing = portData.Spacing;
 temp.PortionFormat.StrikethroughType = portData.StrikethroughType;
 temp.PortionFormat.ProofDisabled = portData.ProofDisabled ? NullableBool.True : NullableBool.False;
 temp.PortionFormat.NormaliseHeight = portData.NormaliseHeight ? NullableBool.True : NullableBool.False;
 temp.PortionFormat.HyperlinkMouseOver = portData.HyperlinkMouseOver;
 temp.PortionFormat.HyperlinkClick = por.PortionFormat.HyperlinkClick;
 temp.PortionFormat.HighlightColor.Color = portData.HighlightColor;
 return temp;
//[VB.NET Code Sample]
'Function Call
 Private newPortion As IPortion=CopyPortion(SourcePortion)
'Function Definition
 Public Shared Function CopyPortion(ByVal por As IPortion) As IPortion
	 Dim temp As New Portion()
	 'use CreatePortionFormatData!!!
	 Dim portData As IPortionFormatEffectiveData = por.CreatePortionFormatEffective()
	 ' use PortionFormat to set values
	 temp.PortionFormat.AlternativeLanguageId = portData.AlternativeLanguageId
	 temp.PortionFormat.BookmarkId = portData.BookmarkId
	 temp.PortionFormat.Escapement = portData.Escapement
	 temp.PortionFormat.FillFormat.FillType = por.PortionFormat.FillFormat.FillType
	 temp.PortionFormat.FillFormat.SolidFillColor.Color = portData.FillFormat.SolidFillColor
	 temp.PortionFormat.FontBold = If(portData.FontBold, NullableBool.True, NullableBool.False)
	 temp.PortionFormat.FontHeight = portData.FontHeight
	 temp.PortionFormat.FontItalic = If(portData.FontItalic, NullableBool.True, NullableBool.False)
	 temp.PortionFormat.FontUnderline = portData.FontUnderline
	 temp.PortionFormat.UnderlineFillFormat.FillType = portData.UnderlineFillFormat.FillType
	 temp.PortionFormat.UnderlineFillFormat.SolidFillColor.Color = portData.UnderlineFillFormat.SolidFillColor
	 temp.PortionFormat.IsHardUnderlineFill = If(portData.IsHardUnderlineFill, NullableBool.True, NullableBool.False)
	 temp.PortionFormat.IsHardUnderlineLine = If(portData.IsHardUnderlineLine, NullableBool.True, NullableBool.False)
	 temp.PortionFormat.KerningMinimalSize = portData.KerningMinimalSize
	 temp.PortionFormat.Kumimoji = If(portData.Kumimoji, NullableBool.True, NullableBool.False)
	 temp.PortionFormat.LanguageId = portData.LanguageId
	 temp.PortionFormat.LatinFont = portData.LatinFont
	 temp.PortionFormat.EastAsianFont = portData.EastAsianFont
	 temp.PortionFormat.ComplexScriptFont = portData.ComplexScriptFont
	 temp.PortionFormat.SymbolFont = portData.SymbolFont
	 temp.PortionFormat.TextCapType = portData.TextCapType
	 temp.PortionFormat.Spacing = portData.Spacing
	 temp.PortionFormat.StrikethroughType = portData.StrikethroughType
	 temp.PortionFormat.ProofDisabled = If(portData.ProofDisabled, NullableBool.True, NullableBool.False)
	 temp.PortionFormat.NormaliseHeight = If(portData.NormaliseHeight, NullableBool.True, NullableBool.False)
	 temp.PortionFormat.HyperlinkMouseOver = portData.HyperlinkMouseOver
	 temp.PortionFormat.HyperlinkClick = por.PortionFormat.HyperlinkClick
	 temp.PortionFormat.HighlightColor.Color = portData.HighlightColor
	 Return temp
 End Function
Overview: Aspose.Slides for .NET
Aspose.Slides is a .NET component to read, write and modify a PowerPoint document without using MS PowerPoint. PowerPoint versions from 97-2007 and all three PowerPoint formats: PPT, POT, PPS are also supported. Now you can create, access, copy, clone, edit and delete slides in your presentations. Other features include PPT/PPTX printing, PPT to XPS format, saving PowerPoint slides into PDF, adding & modifying audio & video frames, using shapes like rectangles or ellipses and saving presentations in SVG format, streams or images.
More about Aspose.Slides for .NET

Other 44 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.