How to use Aspose.CAD in React
Contents
[
Hide
]
Prerequisites
- Visual Code
- Node.js
Convert dgn image to png and display in browser
In this example, you create a simple conversion program that converts a drawing and saves it as an image.
Creating the React Project
- Ensure that you aren’t already in an React workspace directory.
- Start a new one and then the name of the program or use other programs to create a project such as Visual Code or WebStorm:
npx create-react-app react-example
- Install Aspose.CAD from npm package
npm install aspose-cad
- Create an input type file and img tags to load and display the drawing
<span style="background-color: red"> <input id="file" type="file"/> <img alt="converted" id="image" /> </span>
- In App.tsx, we describe the process of starting the helper processes, processing and saving the image
import React from 'react'; import './App.css'; import {boot} from "aspose-cad/dotnet"; import {Image, PngOptions} from "aspose-cad"; function App() { return ( <div className="App"> <header className="App-header"> <input id="file" type="file"/> <img alt="converted" id="image" /> </header> </div> ); } window.onload = async function () { console.log("loading WASM..."); await boot(); console.log("loaded WASM"); // @ts-ignore document.querySelector('input').addEventListener('change', function() { var reader = new FileReader(); reader.onload = function() { var arrayBuffer = this.result; // @ts-ignore var array = new Uint8Array(arrayBuffer); // LOAD var file = Image.load(array); // SAVE var exportedFilePromise = Image.save(array, new PngOptions()); exportedFilePromise.then((exportedFile: BlobPart) => { console.log(exportedFile); var urlCreator = window.URL || window.webkitURL; var blob = new Blob([exportedFile], { type: 'application/octet-stream' }); var imageUrl = urlCreator.createObjectURL(blob); // @ts-ignore document.querySelector("#image").src = imageUrl; }); } // @ts-ignore reader.readAsArrayBuffer(this.files[0]); }, false); }; export default App;
- Start application
npm start
Execution example
- Choose file.
- Select any DXF, DWG, DGN, DWF, DWFX, IFC, STL, DWT, IGES, PLT, CF2, OBJ, HPGL, IGS, PCL, FBX, PDF, SVG file.
- If the answer is successful, the file will be displayed on the screen and will offer to download it.