C# / .NET Core – Generate thumbnail image of PDF or Office file

July 13th, 2021 / by api2pdf /

Intro

If you are writing c# / .net core code, you might be tasked with generating an image preview or thumbnail image of a PDF or Microsoft Office file like Word, Powerpoint, Excel, or even email files. If you ended up on this article you probably have been doing research for hours and getting nowhere. Maybe you ended up down a rabbit hole of looking up Libreoffice and docker containers and who knows what. Your frustration is mounting. Totally get it. Thankfully we created a REST API for you to solve this for you in minutes.

Our API will take your PDF, docx, xlsx, pptx, eml, msg, or any other file that can be reasonable opened by LibreOffice and take an image of the first page. So if you have a multi-page file, it will only grab the first page as an image. This is incredibly useful for those building galleries of image previews for files.

Generate thumbnail image of a PDF or Microsoft Office file with C# / .NET Core

Step 1) Open up your package manager and run the command

Install-Package Api2Pdf -Version 2.0.0

Step 2) Grab an API key from https://portal.api2pdf.com. Only takes 60 seconds.

Step 3) Use the sample code below and replace “YOUR-API-KEY” with the api key you acquired in step 2.

var a2pClient = new Api2Pdf("YOUR-API-KEY");
var request = new LibreFileConversionRequest
{
    Url = "https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"
}
var apiResponse = a2pClient.LibreOffice.Thumbnail(request);
Console.WriteLine(apiResponse.FileUrl);
Console.ReadLine();

And that’s it! Modify the code as you see fit. Hopefully this saves you time and makes generating thumbnail images of PDFs and office files easy and painless for those writing C# / .NET core code.

See full github library

We have a whole .net based client library for our API that does a lot more than just this. Check out the full library capabilities here: https://github.com/Api2Pdf/api2pdf.dotnet

Tags: , , , , , , , , , , , , , , ,

Comments are closed.