This tutorial helps you understand how to create a PDF document with NET REST API. You will learn to develop a PDF creator with C# Low Code API by creating a new PDF file and adding some text to it. Detailed steps and a sample code are provided for working with this feature.
Prerequisite
- Create an account API credentials
- Download Aspose.PDF Cloud SDK for Dotnet
- Setup C# project with the above SDK
Steps to Make a PDF with C# REST API
- Set Client ID and Client Secret for the API
- Create an instance of the PdfApi class with client credentials
- Create a new PDF file using the PutCreateDocument API call
- Create a Paragraph and set some text in it using Segment and TextLine classes
- Add the paragraph to the newly created PDF file on the first page using the PutAddText method
- Download the final PDF file and save it to a stream
- Save the output PDF file on the local disk
Code for PDF Creator Software with C# Low Code API
using System; | |
using System.IO; | |
using Aspose.Pdf.Cloud.Sdk.Api; | |
using Aspose.Pdf.Cloud.Sdk.Model; | |
using System.Collections.Generic; | |
namespace Aspose.PDF.Cloud.Examples.Kb | |
{ | |
public class PdfTasks | |
{ | |
public static void CreatePDF() | |
{ | |
try | |
{ | |
PdfApi pdfApi = new PdfApi("Client ID", "Client Secret"); | |
DocumentResponse response = pdfApi.PutCreateDocument("newPdfFile"); | |
if (response != null && response.Status.Equals("OK")) | |
{ | |
Paragraph para = PrepareParagraph("This is first text for new PDF"); | |
pdfApi.PutAddText("newPdfFile", 1, para); | |
var stream = pdfApi.DownloadFile("newPdfFile", null, null); | |
using (var fileStream = File.Create("output.pdf")) | |
{ | |
stream.Seek(0, SeekOrigin.Begin); | |
stream.CopyTo(fileStream); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} | |
} | |
private static Paragraph PrepareParagraph(string text) | |
{ | |
List<TextLine> textLines = new List<TextLine>(); | |
Segment segment = new Segment(Value: text); | |
List<Segment> segments = new List<Segment>(); | |
segments.Add(segment); | |
TextLine textLine = new TextLine(Segments: segments); | |
textLines.Add(textLine); | |
Rectangle rectangle = new Rectangle(50.0, 800, 300.0, 850); | |
Paragraph paragraph = new Paragraph(Lines: textLines, Rectangle: rectangle); | |
return paragraph; | |
} | |
} | |
} |
This code snippet enables you to create PDF document online with C# Low Code API. You may supply the file name and a paragraph containing some text in it with the help of Aspose.PDF REST API SDK. Finally, download the output PDF file with the specified text for saving it locally.
Use the above PDF generator with any no-code or low-code apps on any environment supporting this SDK.
The following topic explains the feature of converting a PDF file to a Word document that can be helpful as well: Convert PDF to DOC with NET REST API.