Customizing PDF headers with both text and graphics is a common requirement for cloud‑based reporting solutions. Aspose.PDF Cloud SDK for .NET lets you programmatically add text and image to PDF header in C# with simple API calls. In this tutorial, you will see a complete code example, a cURL alternative, and step‑by‑step explanations. By the end, you’ll be able to embed branded headers across all pages of your PDFs in a cloud environment.
Add Text and Image to PDF Header in C# - Complete Code Example
The following example demonstrates how to add text and image to PDF header in C# using Aspose.PDF Cloud SDK for .NET.
using System;
using System.Collections.Generic;
using System.IO;
using Aspose.Pdf.Cloud.Sdk.Api;
using Aspose.Pdf.Cloud.Sdk.Client;
using Aspose.Pdf.Cloud.Sdk.Model;
class Program
{
static void Main()
{
// -------------------------------------------------
// Configuration – replace with your actual App SID and API Key
// -------------------------------------------------
var config = new Configuration
{
ApiKey = new Dictionary<string, string> { { "api_key", "YOUR_API_KEY" } },
Host = "https://api.aspose.cloud"
};
var pdfApi = new PdfApi(config);
// -------------------------------------------------
// File names (local)
// -------------------------------------------------
string localPdfPath = "input.pdf";
string localImagePath = "headerImage.png";
string outputPdfPath = "output.pdf";
// -------------------------------------------------
// Upload PDF to Aspose Cloud storage
// -------------------------------------------------
using (var pdfStream = File.OpenRead(localPdfPath))
{
pdfApi.UploadFile(Path.GetFileName(localPdfPath), pdfStream);
}
// -------------------------------------------------
// Upload image to Aspose Cloud storage
// -------------------------------------------------
using (var imgStream = File.OpenRead(localImagePath))
{
pdfApi.UploadFile(Path.GetFileName(localImagePath), imgStream);
}
// -------------------------------------------------
// Build HeaderFooter object with text and image
// -------------------------------------------------
var headerFooter = new HeaderFooter
{
// Text part
TextState = new TextState
{
Value = "Confidential Document",
FontSize = 14,
Font = "Helvetica",
FontStyle = FontStyles.Bold,
ForegroundColor = new Color { A = 255, R = 255, G = 0, B = 0 } // Red color
},
// Image part
Image = new Image
{
File = Path.GetFileName(localImagePath), // reference uploaded image
Width = 100,
Height = 30,
HorizontalAlignment = HorizontalAlignment.Center,
Margin = new MarginInfo { Top = 5, Bottom = 5, Left = 5, Right = 5 }
},
// Header margin (optional)
Margin = new MarginInfo { Top = 10, Bottom = 0, Left = 0, Right = 0 }
};
// -------------------------------------------------
// Apply header to all pages of the PDF
// -------------------------------------------------
pdfApi.PutHeaderFooter(Path.GetFileName(localPdfPath), headerFooter);
// -------------------------------------------------
// Download the updated PDF
// -------------------------------------------------
using (var resultStream = pdfApi.GetDocument(Path.GetFileName(localPdfPath)))
using (var fileStream = File.Create(outputPdfPath))
{
resultStream.CopyTo(fileStream);
}
// -------------------------------------------------
// Optional: clean up uploaded files from cloud storage
// -------------------------------------------------
pdfApi.DeleteFile(Path.GetFileName(localPdfPath));
pdfApi.DeleteFile(Path.GetFileName(localImagePath));
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Insert Text and Image to PDF Header Cloud with cURL
You can perform the same operation using the REST API. Below are the essential cURL commands.
# 1. Obtain an access token (replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET)
curl -X POST "https://api.aspose.cloud/connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
# 2. Upload the source PDF
curl -X PUT "https://api.aspose.cloud/v3.0/pdf/storage/file/input.pdf" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/pdf" \
--data-binary "@input.pdf"
# 3. Upload the header image (PNG)
curl -X PUT "https://api.aspose.cloud/v3.0/pdf/storage/file/headerImage.png" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: image/png" \
--data-binary "@headerImage.png"
# 4. Apply header with text and image
curl -X PUT "https://api.aspose.cloud/v3.0/pdf/input.pdf/headerfooter" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"TextState": {
"Value": "Confidential Document",
"FontSize": 14,
"Font": "Helvetica",
"FontStyle": "Bold",
"ForegroundColor": { "A":255,"R":255,"G":0,"B":0 }
},
"Image": {
"File": "headerImage.png",
"Width": 100,
"Height": 30,
"HorizontalAlignment": "Center",
"Margin": { "Top":5,"Bottom":5,"Left":5,"Right":5 }
},
"Margin": { "Top":10,"Bottom":0,"Left":0,"Right":0 }
}'
# 5. Download the updated PDF
curl -X GET "https://api.aspose.cloud/v3.0/pdf/input.pdf" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o output.pdf
<!--CODE_SNIPPET_END]-->
For more details on each endpoint, see the [official API documentation](https://reference.aspose.cloud/pdf/).
## Breaking Down Add Text to PDF Header in C#
Understanding how the code adds text and image to PDF header in C# helps you adapt it for other scenarios. Here is a concise walkthrough:
1. **Configuration Setup** – The `Configuration` object stores your API key and host URL.
<!--CODE_SNIPPET_START-->
```csharp
var [config](https://docs.fileformat.com/programming/config/) = new Configuration
{
ApiKey = new Dictionary<string, string> { { "api_key", "YOUR_API_KEY" } },
Host = "https://api.aspose.cloud"
};
-
Uploading Files –
UploadFilesends the local PDF and image to Aspose Cloud storage.pdfApi.UploadFile(Path.GetFileName(localPdfPath), pdfStream); pdfApi.UploadFile(Path.GetFileName(localImagePath), imgStream); -
Creating HeaderFooter – The
HeaderFootermodel combines aTextStatefor the text and anImageobject for the picture.var headerFooter = new HeaderFooter { TextState = new TextState { Value = "Confidential Document", FontSize = 14, Font = "Helvetica", FontStyle = FontStyles.Bold, ForegroundColor = new Color { A = 255, R = 255, G = 0, B = 0 } }, Image = new Image { File = Path.GetFileName(localImagePath), Width = 100, Height = 30, HorizontalAlignment = HorizontalAlignment.Center } }; -
Applying the Header –
PutHeaderFooterattaches the header to every page of the specified PDF.pdfApi.PutHeaderFooter(Path.GetFileName(localPdfPath), headerFooter); -
Downloading and Cleanup –
GetDocumentretrieves the modified PDF, andDeleteFileremoves temporary files from cloud storage.var resultStream = pdfApi.GetDocument(Path.GetFileName(localPdfPath)); pdfApi.DeleteFile(Path.GetFileName(localPdfPath)); pdfApi.DeleteFile(Path.GetFileName(localImagePath));
For a deeper dive into the HeaderFooter class, refer to the API reference.
Installing and Configuring Aspose.PDF Cloud SDK for .NET
-
Add the NuGet package
dotnet add package Aspose.PDF-Cloud -
Download the SDK binaries - You can also obtain the latest release from the official download page: Aspose.PDF Cloud SDK for .NET Download.
-
Prerequisites - .NET 6.0 or later, an Aspose Cloud account, and valid client credentials (App SID and API Key).
-
Initialize the client - Use the same configuration code shown in the complete example to connect to the cloud service.
With the SDK installed and credentials ready, you can start to add text and image to PDF header in C# instantly.
Conclusion
Adding text and image to PDF header in C# becomes straightforward when you leverage the power of Aspose.PDF Cloud SDK for .NET. The library handles file uploads, header construction, and document retrieval with just a few API calls, making it ideal for cloud‑based PDF processing pipelines. Remember to secure your API key, respect usage limits, and test the header layout with different page sizes to avoid overflow. For production deployments you will need a commercial license; pricing details are available on the product page and you can obtain a temporary license for evaluation from the temporary license page. Start integrating branded headers today and give your PDFs a professional look.
FAQs
-
How do I add text and image to PDF header in C# using the cloud library?
Use theHeaderFootermodel to defineTextStateandImage, then callPutHeaderFooter. The full code example in this article shows the exact steps. -
Can I insert text and image to PDF header cloud without writing C# code?
Yes, the same operation can be performed with REST calls. The cURL section provides a ready‑to‑use script that uploads files and applies the header. -
What image formats are supported for the header?
The API accepts common formats such as PNG and JPEG. Keep the image dimensions reasonable (e.g., 100 x 30) to fit within the header margin. -
Do I need a license to run this in production?
A commercial license is required for production use. Visit the product page for pricing and use the temporary license page to evaluate the SDK before purchasing.