Retrieve, Get, Copy and Insert a Page
Retrieving Page Information
In Microsoft Visio, pages are either foreground or background pages. To get page information, for example page ID and page name, first establish whether a page is a background or foreground page.
The Page
object represents the drawing area of a foreground page or a background page. The Pages property exposed by the Diagram
class supports a collection of Page objects. This property can be used to retrieve page information.
Use the Page.Background
property to determine whether a page is a foreground or background page .
Retrieve Page Information Programming Sample
The following piece of code retrieves the pages information from a diagram.
import jpype | |
import asposediagram | |
jpype.startJVM() | |
from asposediagram.api import * | |
lic = License() | |
lic.setLicense("Aspose.Total.Product.Family.lic") | |
# Call the diagram constructor to load diagram | |
diagram = Diagram("RetrievePageInfo.vdx") | |
for page in diagram.getPages(): | |
# Checks if current page is a background page | |
if page.getBackground() == BOOL.TRUE: | |
# Display information about the background page | |
print("Background Page ID : " + str(page.getID())) | |
print("Background Page Name : " + str(page.getName())) | |
else: | |
# Display information about the foreground page | |
print("\nPage ID : " + str(page.getID())) | |
print("Universal Name : " + str(page.getNameU())) | |
print("ID of the Background Page : " + str(page.getBackPage())) | |
jpype.shutdownJVM() |
Get the Visio Page from a Diagram
Sometimes, developers need to get a Visio drawing’s page details. Aspose.Diagram for Python via Java has features that helps them do this.
Aspose.Diagram for Python via Java offers the Diagram
class that represents a Visio drawing. The Pages property exposed by the Diagram class supports a collection of Page
objects. The PageCollection class exposes getPage
method that can be called to get Page object.
Getting a Visio Page Object by ID
This example work as follows:
- Create an object of the Diagram class.
- Call the Diagram.Pages class' getPage method.
The following example shows how to get a page object by id from Visio drawing.
Get Page Object by ID Programming Sample
import jpype | |
import asposediagram | |
jpype.startJVM() | |
from asposediagram.api import * | |
lic = License() | |
lic.setLicense("Aspose.Total.Product.Family.lic") | |
# Call the diagram constructor to load diagram from a VDX file | |
diagram = Diagram("DrawingFlowCharts.vsdx") | |
# Set page id | |
page_id = 2 | |
# Get page object by id | |
page2 = diagram.getPages().getPage(page_id) | |
jpype.shutdownJVM() |
Getting a Visio Page Object by Name
This example work as follows:
- Create an object of the Diagram class.
- Call the Diagram.Pages class' GetPage method.
Get Page Object by Name Programming Sample
The following example shows how to get a page object by name from Visio drawing.
import jpype | |
import asposediagram | |
jpype.startJVM() | |
from asposediagram.api import * | |
lic = License() | |
lic.setLicense("Aspose.Total.Product.Family.lic") | |
# Call the diagram constructor to load diagram from a VSDX file | |
diagram = Diagram("DrawingFlowCharts.vsdx") | |
# Set page name | |
pageName = "Flow 2" | |
# Get page object by name | |
page2 = diagram.getPages().getPage(pageName) | |
jpype.shutdownJVM() |
Copy a Visio Page into Another Diagram
Aspose.Diagram for Python via Java API allows developers to copy and add its content from the one Visio diagram to another. This help topic explains how to accomplish this task.
Aspose.Diagram for Python via Java API has the Diagram
class that represents a Visio drawing. The Pages property exposed by the Diagram class supports a collection of Page
objects. The PageCollection class exposes add
method that can be called to add another Page object.
This example work as follows:
- Create a new object of the Diagram class.
- Load an existing Visio diagram into the Diagram class object.
- Add all masters from the loaded Visio diagram
- Get page object from the loaded diagram (which need to be copied).
- Set page object name and id.
- Remove empty page of the new diagram (optional).
- Call add method of the PageCollection class.
- Save the new diagram in the computer storage.
Copy a Visio Page Programming Sample
The code example below shows how to copy a Visio page object into another Visio drawing.
import jpype | |
import asposediagram | |
jpype.startJVM() | |
from asposediagram.api import * | |
lic = License() | |
lic.setLicense("Aspose.Total.Product.Family.lic") | |
# Call the diagram constructor to load diagram from a VSD file | |
originalDiagram = Diagram("Drawing1.vsdx") | |
# initialize the new visio diagram | |
newDiagram = Diagram() | |
# add all masters from the source Visio diagram | |
originalMasters = originalDiagram.getMasters() | |
for master in originalMasters: | |
newDiagram.addMaster(originalDiagram, master.getName()) | |
# get the page object from the original diagram | |
SrcPage = originalDiagram.getPages().getPage("Page-1") | |
# set page name | |
SrcPage.setName("new page") | |
# it calculates max page id | |
max_page_id = 0 | |
if newDiagram.getPages().getCount() != 0: | |
max_page_id = newDiagram.getPages().get(0).getID() | |
for i in range(0, newDiagram.getPages().getCount() - 1): | |
if max_page_id < newDiagram.getPages().get(i).getID(): | |
max_page_id = newDiagram.getPages().get(i).getID() | |
MaxPageId = max_page_id | |
# set page id | |
SrcPage.setID(MaxPageId) | |
# add reference of the original diagram page | |
newDiagram.getPages().add(SrcPage) | |
# remove first empty page | |
newDiagram.getPages().remove(newDiagram.getPages().get(0)) | |
# save diagram in VDX format | |
newDiagram.save("CopyVisioPage_Out.vsdx", SaveFileFormat.VSDX) | |
jpype.shutdownJVM() |
Copy Visio Page to another Page instance
The copy
method of the Page
class takes a page instance to clone.
# import diagram
diagram = Diagram(dataDir + "Drawing1.vsdx")
newPage = Page()
# copy page
newPage.copy(diagram.getPages().getPage("Page-1"))
Insert a Blank Page into a Visio Drawing
Aspose.Diagram for Python via Java can insert a new blank page into the Microsoft Office Visio drawing. This example topic describes how to do so.
The add
method, exposed by the Pages collection, allows developers to add a new blank page in the Visio diagram. The page ID should be assigned.
Insert a Blank Page Programming Sample
The following piece of code inserts a blank page in the Visio Drawing:
import jpype | |
import asposediagram | |
jpype.startJVM() | |
from asposediagram.api import * | |
lic = License() | |
lic.setLicense("Aspose.Total.Product.Family.lic") | |
# load diagram | |
diagram = Diagram("Drawing1.vsdx") | |
# it calculates max page id | |
max_page_id = 0 | |
if diagram.getPages().getCount() != 0: | |
max_page_id = diagram.getPages().get(0).getID() | |
for i in range(0, diagram.getPages().getCount() - 1): | |
if max_page_id < diagram.getPages().get(i).getID(): | |
max_page_id = diagram.getPages().get(i).getID() | |
# Initialize a new page object | |
newPage = Page() | |
# Set name | |
newPage.setName("new page") | |
# Set page ID | |
newPage.setID(max_page_id + 1) | |
# Or try the Page constructor | |
# Page newPage = Page(MaxPageId + 1) | |
# Add a new blank page | |
diagram.getPages().add(newPage) | |
# Save diagram | |
diagram.save("InsertBlankPageInVisio_Out.vsdx", SaveFileFormat.VSDX) | |
jpype.shutdownJVM() |
Move Page position in the Visio drawing
Aspose.Diagram for Python via Java API can move page position in the Visio drawing. The moveTo
method, exposed by the Page
class, helps developers to move the page position.
Move Page position Programming Sample
The MoveTo member takes the target page index as a parameter to move the position of page in the Visio drawing:
# import diagram
diagram = Diagram(dataDir + "Drawing1.vsdx")
newPage = Page(1)
# move page in the diagram
newPage.moveTo(2)
diagram.save(dataDir + "Drawing1.vsdx", SaveFileFormat.VSDX)