Aspose.Pdf

Tips to Simplify your Code

Since release 1.6 of Aspose.Pdf , some new constructors and methods are added to simplify the use of API so that developer's efforts can be reduced. We know that there are always many ways to write a code for doing the same thing. But, some techniques can reduce the code and efforts of developers so, this is the aim of this topic to provide some tips to developers to write the simplest code for performing some general tasks.

 

Tip 1: Using Add() method

 

Some collection classes including Cells , Rows , Segments and Sections has an Add() method. This method is used to initialize a new instance of the class and add it to the collection.

 

[General Approach]

 

[C#]

 

Row row1 = new Row(table1);

table1.Rows.Add(row1);

 

 

[VB.NET]

 

Dim row1 As Row =  New Row(table1)

table1.Rows.Add(row1)

 

[JAVA]

 

Row row1 = new Row(table1);

table1.getRows().add(row1);

 

 

The same thing can be achieved by just one line of code shown below as a better approach.

 

[Better Approach]

 

[C#]

 

Row row1 = table1.Rows.Add();

 

 

[VB.NET]

 

Dim row1 As Row =  table1.Rows.Add()

 

[JAVA]

 

Row row1 = table1.getRows().add();

 

Tip 2: Using Segment constructor

 

Normally, a Segment(Text) constructor is used to create a Text Segment that takes a Text Paragraph as a parameter and then it is added to the segments collection of the Text Paragraph . After that developers have to set its content.

 

[General Approach]

 

[C#]

 

Segment segment2 = new Segment(text2);

text2.Segments.Add(segment2);

segment2.Content = "cell2";

 

 

[VB.NET]

 

Dim segment2 As Segment =  New Segment(text2)

text2.Segments.Add(segment2)

segment2.Content = "cell2"

 

[JAVA]

 

Segment segment2 = new Segment(text2);

text2.getSegments().add(segment2);

segment2.setContent("cell2");

 

 

The better way is to add the Segment to the segments collection of the Text Paragraph and the content of the Segment should be passed to its constructor directly.

 

[Better Approach]

 

[C#]

 

text2.Segments.Add(new Segment("cell2"));

 

 

[VB.NET]

 

text2.Segments.Add(New Segment("cell2"))

 

[JAVA]

 

  text2.getSegments().add( new Segment(text2, "cell2"));

 

Tip 3: Using Add(string) of Cells class

 

Normally, a Cell object is created then added to Row and then content is added to it. It consumed many steps as shown below in the code snippet.

 

[General Approach]

 

[C#]

 

Cell cell1 = new Cell(row1);

row1.Cells.Add(cell1);

 

 

Text text1 = new Text();

cell1.Paragraphs.Add(text1);

Segment segment1 = new Segment(text1);

text1.Segments.Add(segment1);

segment1.Content = "cell1";

 

 

[VB.NET]

Dim cell1 As Cell =  New Cell(row1)

row1.Cells.Add(cell1)

 

 

Dim text1 As Text =  New Text()

cell1.Paragraphs.Add(text1)

Dim segment1 As Segment =  New Segment(text1)

text1.Segments.Add(segment1)

segment1.Content = "cell1"

 

  [JAVA]

 

Cell cell1 = new Cell(row1);

row1.getCells().add(cell1);

 

Text text1 = new Text(sec1);

cell1.getParagraphs().add(text1);

Segment segment1 = new Segment(text1);

text1.getSegments().add(segment1);

segment1.setContent("cell1");

 

 

The better way is to use Cells.Add(string) method to initialize a new instance of the Cell class that adds a string to the new Cell object and then add the Cell object to the Cells collection of the Row .

 

[Better Approach]

 

[C#]

 

Cell cell1 = row1.Cells.Add("cell1");

 

 

[VB.NET]

 

Dim cell1 As Cell =  row1.Cells.Add("cell1")

 

[JAVA]

 

Cell cell1 = row1.getCells().add("cell1");

 

 

Moreover, 'Cells.Add(string, TextInfo) can also be used that takes cell information alongwith its text format settings.

 

Tip 4: Using Color constructor

 

The Color(byte) constructor is used to initialize a new instance of the Color class with gray colorspace. The Color(byte,byte,byte) is used to initialize a new instance of the Color class with RGB colorspace. The Color(byte,byte,byte,byte) is used to initialize a new instance of the Color class with CMYK colorspace. The Color(string) constructor is used to initialize a new instance of the Color class with RGB colorspace and specified color name.

 

[General Approach]

 

[C#]

 

Line l3 = new Line(graph);

l3.GraphInfo.Color.ColorSpaceType = ColorSpaceType.Cmyk;

l3.GraphInfo.Color.CmykColorSpace = new CmykColorSpace();

l3.GraphInfo.Color.CmykColorSpace.C = 0;

l3.GraphInfo.Color.CmykColorSpace.M = 128;

l3.GraphInfo.Color.CmykColorSpace.Y = 64;

l3.GraphInfo.Color.CmykColorSpace.K = 0;

 

 

[VB.NET]

 

Dim l3 As Line =  New Line(graph)

l3.GraphInfo.Color.ColorSpaceType = ColorSpaceType.Cmyk

l3.GraphInfo.Color.CmykColorSpace = New CmykColorSpace()

l3.GraphInfo.Color.CmykColorSpace.C = 0

l3.GraphInfo.Color.CmykColorSpace.M = 128

l3.GraphInfo.Color.CmykColorSpace.Y = 64

l3.GraphInfo.Color.CmykColorSpace.K = 0

 

[Better Approach]

 

[C#]

 

l3.GraphInfo.Color = new Color(0,128,64,0);

 

 

[VB.NET]

 

l3.GraphInfo.Color = New Color(0,128,64,0)

 

Tip 5: Using BorderInfo constructor

 

The BorderInfo(int) constructor is used to initialize a new instance of the BorderInfo class with specified border sides. For example, BorderInfo((int)(BorderSide.Left | BorderSide.Top)). The BorderInfo(int,float) constructor is used to initialize a new instance of the BorderInfo class with specified border sides and border width. The BorderInfo(int,float,Color) constructor is used to initialize a new instance of the BorderInfo class with specified border sides, border width and border color. The BorderInfo(int,GraphInfo) constructor is used to initialize a new instance of the BorderInfo class with specified border sides and border style. The BorderInfo(int,Color) is used to initialize a new instance of the BorderInfo class with specified border sides and border color.

 

[General Approach]

 

[C#]

 

BorderInfo border2 = new BorderInfo();

GraphInfo graphInfo1 = new GraphInfo();

graphInfo1.Color.ColorSpaceType = ColorSpaceType.Rgb;

graphInfo1.Color.RgbColorSpace = System.Drawing.Color.Red;

border2.Left = border2.Right = border2.Bottom = border2.Top = graphInfo1;

cell2.Border = border2;

 

 

[VB.NET]

 

Dim border2 As BorderInfo =  New BorderInfo()

Dim graphInfo1 As GraphInfo =  New GraphInfo()

graphInfo1.Color.ColorSpaceType = ColorSpaceType.Rgb

graphInfo1.Color.RgbColorSpace = System.Drawing.Color.Red

border2.Left = border2.Right = border2.Bottom = border2.Top = graphInfo1

cell2.Border = border2

 

[JAVA]

 

BorderInfo border2 = new BorderInfo();

GraphInfo graphInfo1 = new GraphInfo();

graphInfo1.setColor(Color.Red);

border2.setLeft(graphInfo1);

border2.setRight(graphInfo1);

border2.setBottom(graphInfo1);

border2.setTop(graphInfo1);

cell1.setBorder(border2);

 

 

[Better Approach]

 

[C#]

 

cell2.Border = new BorderInfo((int)BorderSide.All,new Color("red")); 

 

 

[VB.NET]

 

cell2.Border = New BorderInfo(CType(BorderSide.All,New Color("red"), Integer))

 

[JAVA]

 

cell1.setBorder(new BorderInfo(BorderSide.All, 1, Color.Red));

 

Tip 6: Using Line constructor

 

The Line(float[]) is used to initialize a new instance of the Line class with specified position array. The Line(Graph,float[]) is used to initialize a new instance of the Line class with specified position array and inherit style information from specified Graph object.

 

[General Approach]

 

[C#]

 

Line l1 = new Line(graph);

PositionArray posArr = new PositionArray();

posArr.Length = 4;

posArr[0] = 100;

posArr[1] = 0;

posArr[2] = 300;

posArr[3] = 0;

l1.PositionArray = posArr;

 

 

[VB.NET]

 

Dim l1 As Line =  New Line(graph)

Dim posArr As PositionArray =  New PositionArray()

posArr.Length = 4

posArr(0) = 100

posArr(1) = 0

posArr(2) = 300

posArr(3) = 0

l1.PositionArray = posArr

 

[Better Approach]

 

[C#]

 

float[] posArr = new float[]{100,0,300,0};

Line l1 = new Line(graph1,posArr);

 

 

[VB.NET]

 

Dim posArr() As single =  New single() {100,0,300,0}

Dim l1 As Line =  New Line(graph1,posArr)

 

Tip 7: Using Rectangle constructor

 

The Rectangle(float,float,float,float) constructor is used to initialize a new instance of the Rectangle class with specified position,width and height. The Rectangle(Graph,float,float,float,float) constructor is used to initialize a new instance of the Rectangle class with specified position, width & height and inherit style information from specified Graph object.

 

[General Approach]

 

[C#]

 

Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(graph);

rectangle.Left = 50;

rectangle.Bottom = 10;

rectangle.Width = 100;

rectangle.Height = 50;

 

 

[VB.NET]

 

Dim rectangle As Aspose.Pdf.Rectangle =  New Aspose.Pdf.Rectangle(graph)

rectangle.Left = 50

rectangle.Bottom = 10

rectangle.Width = 100

rectangle.Height = 50

 

[JAVA]

 

com.aspose.pdf.elements.Rectangle rectangle = new com.aspose.pdf.elements.Rectangle();

rectangle.setposX(50);

rectangle.setposY(10);

rectangle.setWidth(100);

rectangle.setHeight(50);

 

[Better Approach]

 

[C#]

 

Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(graph,50,10,100,50);

 

 

[VB.NET]

 

Dim rectangle As Aspose.Pdf.Rectangle =  New Aspose.Pdf.Rectangle(graph,50,10,100,50)

 

[JAVA]

 

com.aspose.pdf.elements.Rectangle rectangle =

new com.aspose.pdf.elements.Rectangle(50,10,100,50);

 

 

Tip 8: Using Arc constructor

 

The Arc(float xPosition,float yPosition,float radius,float alpha,float beta) constructor is used to initialize a new instance of the Arc class with specified arc information. The Arc(Graph graph,float xPosition,float yPosition,float radius,float alpha,float beta) constructor is used to initialize a new instance of the Arc class with specified arc information with inherited style information from specified Graph object.

 

[General Approach]

 

[C#]

 

Arc arc = new Arc(graph);

arc.PositionX = 200;

arc.PositionY = 50;

arc.Radius = 30;

arc.Alpha = 30;

arc.Beta = 90;

 

 

[VB.NET]

 

Dim arc As Arc =  New Arc(graph)

arc.PositionX = 200

arc.PositionY = 50

arc.Radius = 30

arc.Alpha = 30

arc.Beta = 90

 

[Better Approach]

 

[C#]

 

Arc arc = new Arc(graph,200,50,30,30,90);

 

 

[VB.NET]

 

Dim arc As Arc =  New Arc(graph,200,50,30,30,90)

 

Tip 9: Using Circle constructor

 

The Circle(float xPosition,float yPosition,float radius) constructor is used to initialize a new instance of the Circle class with specified circle information. The Circle(Graph graph,float xPosition,float yPosition,float radius) constructor initializes a new instance of the Circle class with specified circle information and inherits style information from specified Graph object.

 

[General Approach]

 

[C#]

 

Circle circle = new Circle(graph);

circle.PositionX = 200;

circle.PositionY = 50;

circle.Radius = 30;

 

 

[VB.NET]

 

Dim circle As Circle =  New Circle(graph)

circle.PositionX = 200

circle.PositionY = 50

circle.Radius = 30

 

[Better Approach]

 

[C#]

 

Circle circle = new Circle(graph,200,50,30);

 

 

[VB.NET]

 

Dim circle As Circle =  New Circle(graph,200,50,30)

 

Tip 10: Using Curve constructor

 

The Curve(float[] positionArr) constructor is used to initialize a new instance of the Curve class with specified curve's position information. The Curve(Graph graph,float[] positionArr) constructor initializes a new instance of the Curve class with specified curve's position information and also inherits style information from specified Graph object.

 

[General Approach]

 

[C#]

 

Curve curve = new Curve(graph);

curve.Position1X = 0;

curve.Position1Y = 0;

curve.Position2X = 200;

curve.Position2Y = 80;

curve.Position3X = 300;

curve.Position3Y = 40;

curve.Position4X = 350;

curve.Position4Y = 90;

 

 

[VB.NET]

 

Dim curve As Curve =  New Curve(graph)

curve.Position1X = 0

curve.Position1Y = 0

curve.Position2X = 200

curve.Position2Y = 80

curve.Position3X = 300

curve.Position3Y = 40

curve.Position4X = 350

curve.Position4Y = 90

 

[Better Approach]

 

[C#]

 

float[] posArr = new float[]{0,0,200,80,300,40,350,90};

Curve curve = new Curve(graph,posArr);

 

 

[VB.NET]

 

Dim posArr() As single =  New single() {0,0,200,80,300,40,350,90}

Dim curve As Curve =  New Curve(graph,posArr)

 

Tip 11: Using Graph constructor

 

The Graph(float width,float height) constructor is used to initialize a new instance of the Curve class with specified curve's position information. The Graph(Section section,float width,float height) constructor initializes a new instance of the Curve class with specified curve's position information and inherit common property values from the specified Section object. The Graph(HeaderFooter hf,float width,float height) constructor initializes a new instance of the Curve class with specified curve's position information and inherits common property values from the specified HeaderFooter object.

 

[General Approach]

 

[C#]

 

Graph graph = new Graph(section);

graph.GraphHeight = 100;

graph.GraphWidth = 400;

 

 

[VB.NET]

 

Dim graph As Graph =  New Graph(section)

graph.GraphHeight = 100

graph.GraphWidth = 400

 

[Better Approach]

 

[C#]

 

Graph graph = new Graph(section,100,400);

 

 

[VB.NET]

 

Dim graph As Graph =  New Graph(section,100,400)

 

Tip 12: Using Text constructor

 

The Text(string content) constructor is used to initialize a new instance of the Text class with specified string as the content of it's first Segment. The Text(string content,TextInfo info) constructor is used to initialize a new instance of the Text class with specified text style and specified string as the content of it's first Segment . The Text(Section section,string content) constructor initializes a new instance of the Text class with specified string as the content of it's first Segment and inherits text style information from the specified Section object. The Text(HeaderFooter hf,string content) initializes a new instance of the Text class with specified string as the content of it's first Segment and inherits text style information from specified HeaderFooter object.

 

[General Approach]

 

[C#]

 

Text text1 = new Text(section);

Segment segment1 = new Segment(text1);

text1.Segments.Add(segment1);

segment1.Content = "this is text content";

 

 

[VB.NET]

 

Dim text1 As Text =  New Text(section)

Dim segment1 As Segment =  New Segment(text1)

text1.Segments.Add(segment1)

segment1.Content = "this is text content"

 

[Better Approach]

 

[C#]

 

Text text1 = new Text(section,"this is text content");

 

 

[VB.NET]

 

Dim text1 As Text =  New Text(section,"this is text content")

 

Tip 13: Using default cell format information

 

Table.DefaultCellBorder property is used to set the border information of all cells in the Table . Table.DefaultCellTextInfo is used to set the text information of all cells in the Table . Table.DefaultCellPadding is used to set the padding information of all cells in the Table . Table.ColumnWidths is used to set the width of all columns in the Table . Row.DefaultCellTextInfo is used to set text information of all cells in the Row . Table.SetColumnTextInfo() is used to set text information of all cells in the column.

 

[General Approach]

 

<Table>

     <Row>

         <Cell FitWidth="70">

             <Border>

                 <All Color="Black"></All>

             </Border>

             <Text MarginTop="1" MarginBottom="1">

                 <Segment>cell1</Segment>

             </Text>

         </Cell>

         <Cell FitWidth="80">

             <Border>

                 <Top Color="Black"></Top>

                     <Right Color="Black"></Right>

                     <Bottom Color="Black"></Bottom>

             </Border>

             <Text MarginTop="1" MarginBottom="1">

                    <Segment>cell2</Segment>

             </Text>

         </Cell>

     </Row>

</Table>

 

[Better Approach]

 

<Table ColumnWidths="70 80">

     <DefaultCellBorder>

         <All Color="Black"></All>

     </DefaultCellBorder>

     <Row>

         <Cell>

             <Text MarginTop="1" MarginBottom="1">

                 <Segment>cell1</Segment>

             </Text>

         </Cell>

         <Cell>

             <Text MarginTop="1" MarginBottom="1">

                 <Segment>cell2</Segment>

             </Text>

         </Cell>

     </Row>

</Table>