Tutorial: How to Convert Diagram Files to Another Format

Learning Objectives

In this tutorial, you’ll learn how to:

  • Authenticate with the Aspose.Diagram Cloud API
  • Convert diagram files (like VDX, VSDX) to various formats (PDF, PNG, etc.)
  • Handle API responses and manage converted files
  • Implement conversion in different programming languages

Prerequisites

Before starting this tutorial, make sure you have:

  • An Aspose Cloud account (sign up at Aspose Cloud Dashboard)
  • Your Client ID and Client Secret from the dashboard
  • A diagram file you want to convert (a sample VDX or VSDX)
  • Basic understanding of RESTful API concepts
  • Development environment for your preferred language (if following SDK examples)

Introduction

Converting diagram files between formats is a common requirement in many applications. Whether you need to transform a Visio diagram into a PDF for sharing, an image for presentations, or another format for compatibility, Aspose.Diagram Cloud API provides a straightforward way to accomplish this.

In this tutorial, we’ll learn how to use the POST /diagram/{name}/SaveAs API endpoint to convert a diagram file from one format to another.

Step 1: Authentication - Getting Access Token

Before making API requests, you need to authenticate with Aspose Cloud services to obtain an access token.

Try it yourself:

Use the following cURL command to get your access token:

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"

Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your actual credentials from the Aspose Cloud Dashboard.

The response will contain an access token that looks like this:

{
  "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
  "expires_in": 3600,
  "token_type": "Bearer"
}

Save this access token as you’ll need it for subsequent API calls.

Step 2: Upload Your Diagram File

Before converting, you need to upload your diagram file to the Aspose Cloud storage.

Note: If your file is already in the storage, you can skip this step.

Try it yourself:

Use the PUT request to upload your file:

curl -v "https://api.aspose.cloud/v3.0/diagram/storage/file/YOUR_DIAGRAM_FILE.vdx" \
-X PUT \
-H "Content-Type: application/octet-stream" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
--data-binary @/path/to/your/file.vdx

Replace:

  • YOUR_DIAGRAM_FILE.vdx with your desired filename in storage
  • YOUR_ACCESS_TOKEN with the token from Step 1
  • /path/to/your/file.vdx with the actual path to your local file

Step 3: Convert Diagram File to Another Format

Now that your file is in storage, you can convert it to your desired format.

Try it yourself:

Use the following cURL command to convert the file:

curl -v "https://api.aspose.cloud/v3.0/diagram/YOUR_DIAGRAM_FILE.vdx/SaveAs?IsOverwrite=true&newfilename=converted_file.pdf" \
-X POST \
-d '{"Format":"pdf"}' \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Replace:

  • YOUR_DIAGRAM_FILE.vdx with your uploaded file name
  • YOUR_ACCESS_TOKEN with your access token
  • Modify newfilename=converted_file.pdf if you want a different output filename
  • Change the format in the request body {"Format":"pdf"} to your desired output format (e.g., “png”, “svg”, “html”, etc.)

Understanding the parameters:

  • IsOverwrite: Set to true to overwrite existing files with the same name
  • newfilename: The name for the converted file
  • Format: The target format for conversion (in the request body)

Step 4: Download or Access the Converted File

After a successful conversion, you can download your converted file from storage.

Try it yourself:

curl -v "https://api.aspose.cloud/v3.0/diagram/storage/file/converted_file.pdf" \
-X GET \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
--output converted_file.pdf

Replace:

  • YOUR_ACCESS_TOKEN with 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 = "file_get_1.vdx";
            
            // Convert file to PDF
            string format = "pdf";
            string newFileName = "converted_file.pdf";
            bool isOverwrite = true;
            
            var request = new SaveAsRequest(fileName, format, newFileName, isOverwrite);
            var result = api.SaveAs(request);
            
            Console.WriteLine("File 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 DiagramConvertExample {
    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 = "file_get_1.vdx";
            
            // Convert file to PDF
            String format = "pdf";
            String newFileName = "converted_file.pdf";
            Boolean isOverwrite = true;
            
            SaveAsRequest request = new SaveAsRequest(fileName, format, newFileName, isOverwrite);
            SaveAsResponse response = api.saveAs(request);
            
            System.out.println("File 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 = "file_get_1.vdx"

# Convert file to PDF
format_type = "pdf"
new_file_name = "converted_file.pdf"
is_overwrite = True

# Execute request
request = SaveAsRequest(file_name, format_type, new_file_name, is_overwrite)
result = api.save_as(request)

print("File 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 = "file_get_1.vdx";

// Convert file to PDF
const formatType = "pdf";
const newFileName = "converted_file.pdf";
const isOverwrite = true;

// Execute request
api.saveAs(fileName, formatType, newFileName, isOverwrite)
    .then((result) => {
        console.log("File 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

Authentication Errors

  • Issue: “Unauthorized” or “Invalid credentials” response
  • Solution: Double-check your Client ID and Client Secret. Ensure you’re using the correct ones from your Aspose Cloud Dashboard.

File Not Found Errors

  • Issue: “File not found” or similar response
  • Solution: Verify that the file name in your API request matches exactly the name of the file you uploaded to storage, including extension.

Format Conversion Errors

  • Issue: Error during conversion process
  • Solution: Ensure the source file is valid and not corrupted. Also verify that the target format is supported for your source file type.

What You’ve Learned

In this tutorial, you’ve learned how to:

  • Authenticate with the Aspose.Diagram Cloud API
  • Upload diagram files to cloud storage
  • Convert diagram files from one format to another
  • Handle API responses and troubleshoot common issues
  • Implement the conversion process in different programming languages

Further Practice

To reinforce your learning:

  1. Try converting to different output formats (PNG, SVG, HTML, etc.)
  2. Experiment with different parameter settings
  3. Modify the code examples to handle multiple files in a batch process
  4. Create a simple user interface that allows users to upload and convert files

Next Tutorial

Ready to enhance your diagram conversion skills? Continue to Tutorial: How to Convert Specific Diagram Pages to learn how to convert selected pages from your diagram files.

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.