Draw and Impress are almost identical in the interfaces that they support. Draw is specifically designed to handle independent graphic objects, whereas Impress is designed for business effects and presentations. The drawing functionality of Draw and Impress is identical, however. The graphic objects are drawn and displayed on "draw pages." By their design, both Draw and Impress support multiple draw pages. The functionality to retrieve a DrawPage object is defined by the com.sun.star.drawing.XDrawPagesSupplier interface. The interface com.sun.star.drawing.XDrawPages defines methods to retrieve, insert, and remove individual pages (see Table 6 ).
Object Method | Description |
---|---|
InsertNewBylndex(index) | Create and insert a new draw page or master page. |
remove(XDrawPage) | Remove a draw page or master page. |
getCount() | Return the number of draw pages. |
getByIndex(index) | Return a specific draw page. |
hasElements() | Return True if there are documents. |
The macro in Listing 10 demonstrates how to iterate through each of the draw pages. Each draw page is exported to a JPG file. The export type is specified by the MediaType property.
oFilter=CreateUnoService("com.sun.star.drawing.GraphicExportFilter") Dim args(1) as new com.sun.star.beans.PropertyValue For i=0 to oDoc.getDrawPages().getcount()-1 oPage = oDoc.getDrawPages().getByIndex(i) name = oPage.name oFilter.setSourceDocument(opage) args(0).Name = "URL" args(0).Value = "file:///c/"&oPage.name&".JPG" args(1).Name = "MediaType" args(1).Value = "image/jpeg" oFilter.filter(args()) Next
Note | The index used to access draw pages is zero based. This means that the first draw page is at location 0. If a document contains four draw pages, they are numbered 0 through 3. This is why the For loop in Listing 10 is from 0 To oDoc.getDrawPages().getcount()-1. |
The macro in Listing 11 creates a new Impress document and then adds a graphic image to the first draw page. The draw image is sized to retain the aspect ratio.
Sub AddProportionalGraphic Dim oDoc 'Newly created Impress document Dim oDrawPage 'The draw page that will contain the graphic image Dim oGraph 'The created graphic image REM Create an Impress presentation document! oDoc = StarDesktop.loadComponentFromURL("private:factory/simpress",_ "_default", 0, Array()) REM Insert a second draw page if desired, REM leaving the first draw page untouched! REM Could use the property DrawPages REM oDrawPage = oDoc.DrawPages.insertNewByIndex(1) REM oDrawPage = oDoc.getDrawPages().insertNewByIndex(1) REM In this case, simply use the first draw page! oDrawPage = oDoc.getDrawPages().getByIndex(0) REM Create a graphics object that can be inserted into the document oGraph = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape") REM Set the URL of the image so that it can be added to the document oGraph.GraphicURL = "http://api.openoffice.org/branding/images/logonew.gif" oDrawPage.add(oGraph) REM If I stop here, there will be a very small graphic image in the REM upper-left corner of the document. This is pretty much useless. REM Although I could simply size the graphic to the same size as the bitmap REM size, I choose instead to size this so that it is as large as possible REM without changing the aspect ratio. REM Determine the ratio of the height to the width for the image and REM also for the draw page. Dim oNewSize As New com.sun.star.awt.Size 'New Image size Dim oBitmapSize As New com.sun.star.awt.Size 'Bitmap size Dim dImageRatio As Double 'Ratio of the height to width Dim dPageRatio As Double 'Ratio of the height to width oBitmapSize = oGraph.GraphicObjectFillBitmap.GetSize dImageRatio = CDbl(oBitmapSize.Height) / CDbl(oBitmapSize.Width) dPageRatio = CDbl(oDrawPage.Height) / CDbl(oDrawPage.Width) REM Compare the ratios to see which is wider, relatively speaking If dPageRatio > dImageRatio Then oNewSize.Width = oDrawPage.Width oNewSize.Height = CLng(CDbl(oDrawPage.Width) * dImageRatio) Else oNewSize.Width = CLng(CDbl(oDrawPage.Height) / dImageRatio) oNewSize.Height = oDrawPage.Height End If REM Center the image on the Impress page! Dim oPosition as new com.sun.star.awt.Point oPosition.X = (oDrawPage.Width - oNewSize.Width)/2 oPosition.Y = (oDrawPage.Height - oNewSize.Height)/2 oGraph.SetSize(oNewSize) oGraph.SetPosition(oPosition) End Sub
As already stated, Impress and Draw documents are very similar in the API that they support. The macro in Listing 12 draws lines in a Draw document (see Figure 6 ).
Sub DrawLinesInDrawDocument Dim oDoc 'Newly created Draw document Dim oDrawPage 'The draw page that will contain the graphics image Dim oShape 'Shape to insert REM Create a new Draw document! oDoc = StarDesktop.loadComponentFromURL("private:factory/sdraw",_ "_default", 0, Array()) REM Use the first draw page oDrawPage = oDoc.getDrawPages().getByIndex(0) Dim i As Long Dim oPos as new com.sun.star.awt.Point Dim oSize as new com.sun.star.awt.Size Dim dStepSize As Double dStepSize = CDbl(oDrawPage.Height) / 10 For i = 0 To 10 oShape = oDoc.createInstance("com.sun.star.drawing.LineShape") oShape.LineColor = rgb(0, 255 - 20 * i, 20 * i) oShape.LineWidth = 250 oPos.X = 0 oPos.Y = CLng(CDbl(i) * dStepSize) oShape.setPosition(oPos) oSize.width = oDrawPage.Width oSize.height= oDrawPage.Height - 2 * oPos.Y oShape.setSize(oSize) oDrawPage.add(oShape) Next End Sub
Although the primary data in Writer and Calc documents is not drawing data, these documents also contain draw pages. More accurately, each Calc document contains a single draw page for each sheet, and each Writer document contains a single draw page for the entire document. In Writer and Calc documents, the page is like a transparent layer containing drawing data on top of the standard document data.
Writer documents do not support the XDrawPagesSupplier interface, because they only contain a single draw page. They do, however, support the XDrawPageSupplier interface, which defines the single object method getDrawPage().
The macro in Listing 12 uses optional draw page properties-namely height and width. The draw page from a Writer document does not contain these properties. The draw page in a Writer document has other peculiarities , however. For example, adding lines to the draw page-as done in Listing 12-adds them as characters at the cursor position rather than interpreting the positions as specific locations in the document. The macro in Listing 13 draws lines to demonstrate this behavior (also see Figure 7 ).
Sub DrawLinesInWriteDocument Dim oDoc 'Newly created Writer document Dim oDrawPage 'The draw page that will contain the graphics image Dim oShape 'Shape to insert REM Create a new Writer document! oDoc = StarDesktop.loadComponentFromURL("private:factory/swriter",_ "_default", 0, Array()) oDrawPage = oDoc.getDrawPage() Dim i As Long Dim oSize as new com.sun.star.awt.Size Dim dStepSize As Double dStepSize = 800 For i = 0 To 10 oShape = oDoc.createInstance("com.sun.star.drawing.LineShape") oShape.LineColor = rgb(255, 255 - 20 * i, 20 * i) oShape.LineWidth = 50 oSize.width = dStepSize - CLng(CDbl(i) * dStepSize / 10)/2 oSize.width = CLng(dStepSize/5 * i - dStepSize) oSize.height= dStepSize oShape.setSize(oSize) oDrawPage.add(oShape) Next End Sub