Tutorial: How to Convert Specific Diagram Pages

Learning Objectives

In this tutorial, you’ll learn how to:

  • Extract and convert specific pages from diagram files
  • Use page parameters in the Aspose.Diagram Cloud API
  • Control which pages are included in the conversion process
  • Implement page-specific conversions in different programming languages

Prerequisites

Before starting this tutorial, make sure you have:

  • Completed the basic diagram conversion tutorial
  • An Aspose Cloud account with active subscription
  • Your Client ID and Client Secret from the dashboard
  • A multi-page diagram file in your storage (VDX, VSDX, etc.)
  • Development environment for your preferred language

Introduction

Many diagram files contain multiple pages, and sometimes you only need to convert specific pages rather than the entire document. Aspose.Diagram Cloud API allows you to selectively convert individual pages or ranges of pages from your diagram files.

In this tutorial, we’ll learn how to use the extended parameters of the POST /diagram/{name}/SaveAs API to specify which pages to convert.

Step 1: Authentication - Getting Access Token

First, obtain your access token as in the previous tutorial:

Try it yourself:

curl -v "https://api.aspose.cloud/oauth2/token" \
-X POST \
-d 'grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET' \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Save the access token from the response.

Step 2: Understand Page Numbers in Your Diagram

Before converting specific pages, you need to know which pages you want to convert. Page numbers in diagram files typically start with index 0 or 1, depending on the API implementation.

Note: In Aspose.Diagram Cloud API, page numbers are typically 0-based, meaning the first page is indexed as 0.

Step 3: Convert Specific Pages from a Diagram

Now let’s convert only specific pages from your multi-page diagram.

Try it yourself:

Use the following cURL command to convert specific pages:

curl -v "https://api.aspose.cloud/v3.0/diagram/multipage_diagram.vsdx/SaveAs?IsOverwrite=true&newfilename=specific_pages.pdf" \
-X POST \
-d '{"Format":"pdf", "PageIndex": 0, "PageCount": 2}' \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Replace:

  • multipage_diagram.vsdx with your actual multi-page diagram filename
  • YOUR_ACCESS_TOKEN with the token from Step 1
  • PageIndex: Starting page index (0-based)
  • PageCount: Number of pages to convert starting from the PageIndex

This example converts the first two pages (pages 0 and 1) of your diagram to PDF.

Understanding the parameters:

  • PageIndex: The index of the first page to be converted (0-based)
  • PageCount: The number of consecutive pages to convert
  • Format: The target format for conversion (in the request body)
  • IsOverwrite: Whether to overwrite an existing file with the same name
  • newfilename: The name for the converted file

Step 4: Convert Non-Consecutive Pages

If you need to convert non-consecutive pages, you might need to make multiple API calls, one for each specific page, and then potentially combine the results based on your requirements.

Try it yourself:

To convert just page 0 (the first page):

curl -v "https://api.aspose.cloud/v3.0/diagram/multipage_diagram.vsdx/SaveAs?IsOverwrite=true&newfilename=page0.pdf" \
-X POST \
-d '{"Format":"pdf", "PageIndex": 0, "PageCount": 1}' \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"

To convert just page 2 (the third page):

curl -v "https://api.aspose.cloud/v3.0/diagram/multipage_diagram.vsdx/SaveAs?IsOverwrite=true&newfilename=page2.pdf" \
-X POST \
-d '{"Format":"pdf", "PageIndex": 2, "PageCount": 1}' \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Implementation with SDKs

C# Example

using System;
using System.IO;
using Aspose.Diagram.Cloud.Sdk.Api;
using Aspose.Diagram.Cloud.Sdk.Model;

namespace AsposeCloudDiagramExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get Client ID and Client Secret from https://dashboard.aspose.cloud/
            string clientId = "YOUR_CLIENT_ID";
            string clientSecret = "YOUR_CLIENT_SECRET";
            
            // Create API instance
            var api = new DiagramApi(clientId, clientSecret);
            
            // The file name in storage
            string fileName = "multipage_diagram.vsdx";
            
            // Convert specific pages to PDF
            string format = "pdf";
            string newFileName = "specific_pages.pdf";
            bool isOverwrite = true;
            
            // Convert pages 0 and 1 (first two pages)
            int pageIndex = 0;
            int pageCount = 2;
            
            var request = new SaveAsRequest(fileName, format, newFileName, isOverwrite)
            {
                PageIndex = pageIndex,
                PageCount = pageCount
            };
            
            var result = api.SaveAs(request);
            
            Console.WriteLine("Specific pages converted successfully!");
            Console.WriteLine($"Source Document: {result.SaveResult.SourceDocument.Href}");
            Console.WriteLine($"Destination Document: {result.SaveResult.DestDocument.Href}");
        }
    }
}

Java Example

import com.aspose.diagram.cloud.api.DiagramApi;
import com.aspose.diagram.cloud.model.*;
import com.aspose.diagram.cloud.model.requests.*;

public class DiagramConvertSpecificPagesExample {
    public static void main(String[] args) {
        // Get Client ID and Client Secret from https://dashboard.aspose.cloud/
        String clientId = "YOUR_CLIENT_ID";
        String clientSecret = "YOUR_CLIENT_SECRET";
        
        // Create API instance
        DiagramApi api = new DiagramApi(clientId, clientSecret);
        
        try {
            // The file name in storage
            String fileName = "multipage_diagram.vsdx";
            
            // Convert specific pages to PDF
            String format = "pdf";
            String newFileName = "specific_pages.pdf";
            Boolean isOverwrite = true;
            
            // Convert pages 0 and 1 (first two pages)
            Integer pageIndex = 0;
            Integer pageCount = 2;
            
            SaveAsRequest request = new SaveAsRequest(fileName, format, newFileName, isOverwrite);
            request.setPageIndex(pageIndex);
            request.setPageCount(pageCount);
            
            SaveAsResponse response = api.saveAs(request);
            
            System.out.println("Specific pages converted successfully!");
            System.out.println("Source Document: " + response.getSaveResult().getSourceDocument().getHref());
            System.out.println("Destination Document: " + response.getSaveResult().getDestDocument().getHref());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Python Example

import asposediagramcloud
from asposediagramcloud.apis.diagram_api import DiagramApi
from asposediagramcloud.models.requests import SaveAsRequest

# Get Client ID and Client Secret from https://dashboard.aspose.cloud/
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"

# Create API instance
api = DiagramApi(client_id, client_secret)

# The file name in storage
file_name = "multipage_diagram.vsdx"

# Convert specific pages to PDF
format_type = "pdf"
new_file_name = "specific_pages.pdf"
is_overwrite = True

# Convert pages 0 and 1 (first two pages)
page_index = 0
page_count = 2

# Execute request
request = SaveAsRequest(file_name, format_type, new_file_name, is_overwrite)
request.page_index = page_index
request.page_count = page_count

result = api.save_as(request)

print("Specific pages converted successfully!")
print(f"Source Document: {result.save_result.source_document.href}")
print(f"Destination Document: {result.save_result.dest_document.href}")

Node.js Example

const { DiagramApi } = require('asposediagramcloud');

// Get Client ID and Client Secret from https://dashboard.aspose.cloud/
const clientId = "YOUR_CLIENT_ID";
const clientSecret = "YOUR_CLIENT_SECRET";

// Create API instance
const api = new DiagramApi(clientId, clientSecret);

// The file name in storage
const fileName = "multipage_diagram.vsdx";

// Convert specific pages to PDF
const formatType = "pdf";
const newFileName = "specific_pages.pdf";
const isOverwrite = true;

// Convert pages 0 and 1 (first two pages)
const pageIndex = 0;
const pageCount = 2;

// Execute request with page parameters
api.saveAs(fileName, formatType, newFileName, isOverwrite, null, null, pageIndex, pageCount)
    .then((result) => {
        console.log("Specific pages converted successfully!");
        console.log(`Source Document: ${result.body.saveResult.sourceDocument.href}`);
        console.log(`Destination Document: ${result.body.saveResult.destDocument.href}`);
    })
    .catch((error) => {
        console.error("Error:", error);
    });

Troubleshooting Common Issues

Page Index Out of Range

  • Issue: Error message indicating page index is out of range
  • Solution: Verify the total number of pages in your diagram file and ensure your PageIndex and PageCount values are within the valid range. Remember that page indices are 0-based.

No Pages Selected

  • Issue: Empty or incomplete output file
  • Solution: Ensure that PageIndex and PageCount are correctly specified. If PageCount is 0 or missing, no pages may be selected for conversion.

Format Compatibility Issues

  • Issue: Certain page elements not rendering in the output
  • Solution: Not all elements may be supported when converting to certain formats. Try using a different output format or check the documentation for format-specific limitations.

What You’ve Learned

In this tutorial, you’ve learned how to:

  • Extract specific pages from multi-page diagram files
  • Use pagination parameters in the API request
  • Convert specific pages to different formats
  • Handle page selection in different programming languages
  • Troubleshoot common issues related to page-specific conversion

Further Practice

To reinforce your learning:

  1. Try converting different ranges of pages from various diagram files
  2. Convert non-consecutive pages and experiment with combining them
  3. Test different output formats to see how page selection affects each format
  4. Create a utility that allows selecting specific pages by description or content rather than just index

Next Tutorial

Ready to learn more advanced diagram conversion techniques? Continue to Tutorial: Convert Diagram Files with Custom Settings to learn how to apply custom conversion settings for better control over the output.

Helpful Resources

Feedback

Have questions or feedback about this tutorial? We’d love to hear from you! Please post your questions or comments in our support forum.