| In magazines and newspapers, we mostly see that news are displayed in multiple columns on the single pages instead of the books where text paragraphs are mostly printed on the whole pages from left to right position. Many document processing applications like Microsoft Word and Adobe Acrobat Writer allow users to create multiple columns on a single page and then add data to them. |
Using Aspose.Pdf for .NET, developers can also add multiple columns to the pages of their PDF documents. As we know that we always add Paragraphs to Sections of our PDF documents using Aspose.Pdf for .NET. So, it is necessary to divide a Section into multiple columns and this is achieved by Section.ColumnInfo property, which is used to specify the column related information for each Section. This column information would include the width and spacing of columns etc.
The following figure shows multiple columns and the spacing between them:
|
|---|
| Figure: Demonstration of multiple columns |
Column spacing means the space between the columns and ColumnInfo.ColumnSpacing property of the Section class is used to set this space between columns. Default spacing between the columns is 1.25cm. Moreover, ColumnInfo.ColumnWidths property of Section class is used to set the width of each column. If the column width is not specified by the user then Aspose.Pdf for .NET calculates width for each column automatically according to the page size and column spacing.
An example is given below to demonstrate the creation of three columns in a Section with default spacing.
//Instantiate a Pdf object Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf(); //Add a section to the Pdf Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add(); //Set the number of columns in the section to 3 sec1.ColumnInfo.ColumnCount = 3; ... //Save the Pdf pdf1.Save(...);
'Instantiate a Pdf object Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf 'Add a section to the Pdf Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add() 'Set the number of columns in the section to 3 sec1.ColumnInfo.ColumnCount = 3 ... 'Save the Pdf pdf1.Save(...)
<?xml version="1.0" encoding="utf-8" ?> <Pdf xmlns="Aspose.Pdf"> <Section ColumnCount="3"> ... </Section> </Pdf>
In the above example, we learnt that how to create multiple columns with default spacing between them. But, we can change the width of each column and also the spacing between them. Let's practice it also with the help of an example.
Code Snippet
//Instantiate a Pdf object Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf(); //Add a section to the Pdf Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add(); //Add two columns in the section sec1.ColumnInfo.ColumnCount = 2; //Set the spacing between the columns sec1.ColumnInfo.ColumnSpacing = "15"; //Set the widths of the columns sec1.ColumnInfo.ColumnWidths = "250 150"; ... //Save the Pdf pdf1.Save(...);
'Instantiate a Pdf object Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf 'Add a section to the Pdf Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add() 'Add two columns in the section sec1.ColumnInfo.ColumnCount = 2 'Set the spacing between the columns sec1.ColumnInfo.ColumnSpacing = "15" 'Set the widths of the columns sec1.ColumnInfo.ColumnWidths = "250 150" ... 'Save the Pdf pdf1.Save(...)
<?xml version="1.0" encoding="utf-8" ?> <Pdf xmlns="Aspose.Pdf"> <Section ColumnCount="2" ColumnSpacing="15" ColumnWidths="250 150"> ... </Section> </Pdf>
Custom Positioning
| When you add a Paragraph to the PDF document using Aspose.Pdf for .NET then the page renderer engine of Aspose.Pdf for .NET decides the position of the Paragraph on the page. But sometimes, you may want the Paragraph to be displayed on the page at some position specified by you. In this case, you can use custom positioning. This topic describes how to use custom positioning in Aspose.Pdf for .NET. |
When using custom positioning, you can use the Top and Left property of Paragraph class to specify its position on the page.
There are some positioning types defined by Aspose.Pdf for .NET in PositioningType enumeration. These pre-defined positioning types are listed below with their brief descriptions:
| Positioning Types | Description |
|---|---|
| Auto | Paragraph is positioned automatically by page renderer engine |
| PageRelative | Paragraph is positioned relative to the page |
| ColumnRelative | Paragraph is positioned relative to the column |
| ParagraphRelative | Paragraph is positioned relative to the paragraph |
| The above positioning types are explained in the figures below for your better understanding. |
|
|---|
| Figure: PageRelative positioning |
|
| Figure: ColumnRelative positioning |
|
| Figure: ParagraphRelative positioning |
It is important to know that custom positioning doesn't affect the default automatic positioning. It means that the automatically positioned paragraphs are rendered as if the custom positioning paragraphs don't exist. If a custom positioning paragraph is too large, it will not be rendered into a new page but will simply span the page edge. Moreover, when using ParagraphRelative positioning as shown in above figure, the referenced Paragraph should be a Paragraph "ahead of" the current Paragraph in the document object model. That will make sure the referenced Paragraph to be rendered first.
The example below shows how to use custom positioning for the paragraphs. In this example, a Graph (which is a specialization of the Paragraph class) is added at the top of the page and a note annotation is added besides a Text Paragraph.
//Instantiate the Pdf object Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf(); //Add a section to the Pdf object Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add(); //Create a text paragraph Aspose.Pdf.Generator.Text text1 = new Aspose.Pdf.Generator.Text("This is a text paragraph."); //Set the id of the paragraph to "text1" so that it can referenced uniquely text1.ID = "text1"; //Add the paragraph to the section sec1.Paragraphs.Add(text1); //Create a graph with specified left and top position settings. Set its //poition relative to the page. Add a rectangle to its shapes collection and //then add the graph to the paragraph collection of the section Aspose.Pdf.Generator.Graph graph1 = new Aspose.Pdf.Generator.Graph(200,50); graph1.Left = 200; graph1.Top = 10; graph1.PositioningType = Aspose.Pdf.Generator.PositioningType.PageRelative; graph1.Shapes.Add(new Aspose.Pdf.Generator.Rectangle(0,0,200,50)); sec1.Paragraphs.Add(graph1); //Create an attachment as note annotation and add it to the section as a //pragraph. Set the content and heading for the note. Set its position relative //to the paragraph. Assign a unique id to this note annotation for the //reference purposes and then customize its left and top position Aspose.Pdf.Generator.Attachment noteAttachment = new Aspose.Pdf.Generator.Attachment(); sec1.Paragraphs.Add(noteAttachment); noteAttachment.AttachmentType = Aspose.Pdf.Generator.AttachmentType.Note; noteAttachment.NoteContent = "This is a test for note"; noteAttachment.NoteHeading = "this is a Note"; noteAttachment.PositioningType = Aspose.Pdf.Generator.PositioningType.ParagraphRelative; noteAttachment.ReferenceParagraphID = "text1"; noteAttachment.Left = 200; noteAttachment.Top = 0; //Save the Pdf pdf1.Save("e:/temp/test.pdf");
'Instantiate the Pdf object Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf 'Add a section to the Pdf object Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add() 'Create a text paragraph Dim text1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text("This is a text paragraph.") 'Set the id of the paragraph to "text1" so that it can referenced uniquely text1.ID = "text1" 'Add the paragraph to the section sec1.Paragraphs.Add(text1) 'Create a graph with specified left and top position settings. Set its 'poition relative to the page. Add a rectangle to its shapes collection and 'then add the graph to the paragraph collection of the section Dim graph1 As Aspose.Pdf.Generator.Graph = New Aspose.Pdf.Generator.Graph(200, 50) graph1.Left = 200 graph1.Top = 10 graph1.PositioningType = Aspose.Pdf.Generator.PositioningType.PageRelative graph1.Shapes.Add(New Rectangle(0, 0, 200, 50)) sec1.Paragraphs.Add(graph1) 'Create an attachment as note annotation and add it to the section as a 'pragraph. Set the content and heading for the note. Set its position relative 'to the paragraph. Assign a unique id to this note annotation for the 'reference purposes and then customize its left and top position Dim noteAttachment As Aspose.Pdf.Generator.Attachment = New Aspose.Pdf.Generator.Attachment sec1.Paragraphs.Add(noteAttachment) noteAttachment.AttachmentType = Aspose.Pdf.Generator.AttachmentType.Note noteAttachment.NoteContent = "This is a test for note" noteAttachment.NoteHeading = "this is a Note" noteAttachment.PositioningType = Aspose.Pdf.Generator.PositioningType.ParagraphRelative noteAttachment.ReferenceParagraphID = "text1" noteAttachment.Left = 200 noteAttachment.Top = 0 'Save the Pdf pdf1.Save("e:/temp/test.pdf")
<Pdf xmlns="Aspose.Pdf"> <Section> <Text ID="text1"> <Segment>This is a text paragraph. </Segment> </Text> <Graph Width="200" Height="50" Left="200" Top="10" PositioningType= "PageRelative"> <Rectangle Position="0 0 200 50"></Rectangle> </Graph> <Attachment AttachmentType="Note" NoteHeading="this is a Note" NoteContent="This is a test for note" Left="200" Top="0" PositioningType="ParagraphRelative" ReferenceParagraphID="text1"> </Attachment> </Section> </Pdf>




