Zum Fußzeileninhalt springen
IRONPDF NUTZEN

C# PNG zu PDF konvertieren (Code-Beispiel-Tutorial)

This article will guide you on how to convert PNG to PDF using the IronPDF image conversion API in C#.

Topics Covered in the Tutorial

In this tutorial, the following topics will be covered:

  1. Introduction to the IronPDF Library
  2. Create a C# Project
  3. Install the IronPDF Library
    • Method 1: NuGet Package Manager Solution
    • Method 2: NuGet Package Manager Console
    • Method 3: Using the DLL file
  4. Add the IronPDF Namespace
  5. Convert JPG Images to PDF Documents
  6. Summary

Requirements when using IronPDF:

  • You should have basic knowledge of C# languages.
  • You should have a basic knowledge of Windows Applications.

1. Introduction to the IronPDF Library

The IronPDF .NET PDF Library solution is a dream for developers, particularly software engineers who use C#. Using this excellent Iron Software tool, you can easily create a core PDF library for .NET. IronPDF will ensure any PDF conversion from different formats is an effortless and time-saving process.

It also enables you to construct a PDF file using HTML5, JavaScript, CSS, and images. You can seamlessly edit, stamp, and add headers and footers to a PDF. Furthermore, it makes it very easy to read PDF text, extract images, or convert images to PDF programmatically.

Some of the important features include:

Here, this tutorial focuses on the conversion of PNG files to PDF documents. IronPDF supports almost every image format for conversion. Supported image formats are JPG, PNG, GIF, TIFF, SVG, and BMP.

2. Create a C# Project

This tutorial will use the latest version of Visual Studio and C# programming language to build the project. As the example code is derived from C# 10.0, you should have some knowledge concerning top-level statements.

Let's begin by creating a C# project.

  • Open Visual Studio.
  • Either create a new C# project or open an existing one.
  • Give a name to the project.
  • Select .NET Core >= 3.1 as 3.1 is supported and works on every device. The latest and most stable version of the .NET Framework is 6.0.

C# Convert PNG to PDF (Code Example Tutorial), Figure 1: Console Program after Creating Project Console program after creating project

3. Install the IronPDF Library

Method 1: NuGet Package Manager Solution

C# Convert PNG to PDF (Code Example Tutorial), Figure 2: Open from Solution Explorer Open from Solution Explorer

Once the NuGet Package Manager Solution is open, browse for the IronPDF library to convert a PNG image to a PDF file. Then click on install.

C# Convert PNG to PDF (Code Example Tutorial), Figure 3: Browse IronPDF Browse IronPDF

Method 2: NuGet Package Manager Console

Using the NuGet Package Manager Console will allow you to install the library effortlessly. Administrative privilege is not required to install the library. A NuGet command will be used to install the IronPDF library in your project. Just utilize the following command to proceed.

Install-Package IronPdf

Method 3: Using DLL File

You can directly download the IronPDF .DLL file from the website. It can be instantly downloaded from the IronPDF official DLL download link.

To reference the library in your project, follow these directions:

  • Right-click the Solution in the Solution Explorer
  • Select "References"
  • Browse for the IronPDF.dll library
  • Click OK

All done! IronPDF is downloaded, installed, and ready to use to convert PNG to PDF format.

4. Add the IronPDF Namespace

Now add the IronPDF namespace to your program. You have to add a given line of code at the top of the file.

using IronPdf;
using IronPdf;
Imports IronPdf
$vbLabelText   $csharpLabel

This will allow you to access all of the functions provided by IronPDF. This line of code must be added to every file where you wish to use the IronPDF features.

5. Convert PNG to PDF format

Converting PNG files to PDF documents is very easy with IronPDF. Only one line of code can achieve this task using the IronPDF's ImageToPdf method. Inside the directory of the project, place the PNG images to be converted inside a folder named assets. The folder must be placed in the location: bin\Debug\net6.0. Then, using System.IO.Directory, enumerate the assets folder with all the PNG files and pass it to the ImageToPdf method for the conversion operation stream. The following code example helps you convert a PNG image to a PDF document and save a Stream object to the disk.

using IronPdf;
using System;
using System.IO;
using System.Linq;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("C# Convert PNG to PDF using IronPDF");

        // Directory is set to the assets folder. Filters for files ending with '.png' or '.PNG'.
        var images = Directory.EnumerateFiles("assets").Where(f => f.ToLower().EndsWith(".png"));

        // Converts the images to a PDF and saves them.
        if (images.Any())
        {
            ImageToPdfConverter.ImageToPdf(images).SaveAs("composite.pdf");
            Console.WriteLine("PNG successfully converted to PDF");
        }
        else
        {
            Console.WriteLine("No PNG files found.");
        }
    }
}
using IronPdf;
using System;
using System.IO;
using System.Linq;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("C# Convert PNG to PDF using IronPDF");

        // Directory is set to the assets folder. Filters for files ending with '.png' or '.PNG'.
        var images = Directory.EnumerateFiles("assets").Where(f => f.ToLower().EndsWith(".png"));

        // Converts the images to a PDF and saves them.
        if (images.Any())
        {
            ImageToPdfConverter.ImageToPdf(images).SaveAs("composite.pdf");
            Console.WriteLine("PNG successfully converted to PDF");
        }
        else
        {
            Console.WriteLine("No PNG files found.");
        }
    }
}
Imports IronPdf
Imports System
Imports System.IO
Imports System.Linq

Public Class Program
	Public Shared Sub Main()
		Console.WriteLine("C# Convert PNG to PDF using IronPDF")

		' Directory is set to the assets folder. Filters for files ending with '.png' or '.PNG'.
		Dim images = Directory.EnumerateFiles("assets").Where(Function(f) f.ToLower().EndsWith(".png"))

		' Converts the images to a PDF and saves them.
		If images.Any() Then
			ImageToPdfConverter.ImageToPdf(images).SaveAs("composite.pdf")
			Console.WriteLine("PNG successfully converted to PDF")
		Else
			Console.WriteLine("No PNG files found.")
		End If
	End Sub
End Class
$vbLabelText   $csharpLabel

In the above code snippet, the asset folder contains only one PNG file. The output file looks like this:

C# Convert PNG to PDF (Code Example Tutorial), Figure 4: Single JPG file to PDF file Single JPG file to PDF file

The same code example can be used to convert multiple PNG images. The output file formats contain three PNG-to-PDF documents.

C# Convert PNG to PDF (Code Example Tutorial), Figure 5: Multiple PNG Images to PDF Document Multiple PNG images to PDF document

6. Summary

This tutorial shows how to convert a PNG image to a PDF document using the IronPDF C# library. The manipulation and configuration of PDF files become remarkably effortless with the IronPDF library function. All that is needed is just a few lines of code to construct a PDF document from PNG files. Whether it's to convert JPG images, PNG images, or numerous other formats, IronPDF is ideal for developers and companies.

Utilize the free version to test it out! Additionally, with a free trial key, you can test the functionality of IronPDF. Furthermore, the current special offer allows you to get five products from IronPDF for the price of just two! Visit this IronPDF licensing information page for more information about licensing.

Häufig gestellte Fragen

Wie kann ich PNG-Bilder mit C# in PDF konvertieren?

Sie können die ImageToPdf-Methode von IronPDF verwenden, um PNG-Bilder einfach in PDF in C# zu konvertieren. Diese Methode ermöglicht es Ihnen, ein einzelnes Bild oder mehrere Bilder in ein einzelnes PDF-Dokument mit minimalem Code zu konvertieren.

Welche Schritte sind nötig, um IronPDF in einem C#-Projekt einzurichten?

Um IronPDF in einem C#-Projekt einzurichten, können Sie es über den NuGet-Paket-Manager in Visual Studio installieren, die NuGet-Paket-Manager-Konsole mit dem Befehl Install-Package IronPdf verwenden oder die DLL-Datei herunterladen und direkt zu Ihren Projektreferenzen hinzufügen.

Kann ich mit dieser Bibliothek auch andere Dateiformate zu PDF konvertieren?

Ja, IronPDF kann verschiedene Dateiformate, einschließlich HTML, JavaScript, CSS und Bilder wie JPG, GIF, TIFF, SVG und BMP in PDF-Dokumente konvertieren.

Welche Voraussetzungen sind erforderlich, um Bilder in C# in PDF zu konvertieren?

Sie sollten über Grundkenntnisse der C#-Programmierung und Erfahrung im Umgang mit Visual Studio und .NET-Anwendungen verfügen, um IronPDF effektiv für die Bild-zu-PDF-Konvertierung nutzen zu können.

Wie binde ich IronPDF in meinen C#-Code für die Bildkonvertierung ein?

Fügen Sie den Namensraum using IronPdf; am Anfang Ihrer C#-Datei hinzu, um auf alle notwendigen Funktionen zuzugreifen, die von IronPDF bereitgestellt werden.

Ist es möglich, mehrere PNG-Bilder in ein einziges PDF zu konvertieren?

Ja, mit IronPDF können Sie mehrere PNG-Bilder in ein einziges PDF-Dokument konvertieren, indem Sie die ImageToPdf-Methode verwenden.

Welche Entwicklungstools werden für die Arbeit mit IronPDF empfohlen?

Es wird empfohlen, die neueste Version von Visual Studio mit .NET Core Version 3.1 oder höher für optimale Kompatibilität und Leistung bei der Arbeit mit IronPDF zu verwenden.

Kann ich IronPDF vor dem Kauf evaluieren?

Ja, Sie können IronPDF mit seiner kostenlosen Version und einem Testschlüssel ausprobieren, sodass Sie seine Funktionen und Funktionalität bewerten können, bevor Sie eine Kaufentscheidung treffen.

Wie kann ich Probleme beim Konvertieren von Bildern in PDF mit IronPDF beheben?

Stellen Sie sicher, dass Sie die richtigen Namensräume und Referenzen in Ihrem Projekt eingeschlossen haben. Überprüfen Sie auf Fehler in Ihrer Codesyntax und vergewissern Sie sich, dass Ihr .NET Framework mit IronPDF kompatibel ist. Für zusätzliche Unterstützung konsultieren Sie die IronPDF-Dokumentation oder Community-Foren.

.NET 10-Kompatibilität: Kann ich IronPDF verwenden, um PNGs in einem .NET 10-Projekt in PDF zu konvertieren?

Ja – IronPDF ist vollständig kompatibel mit .NET 10. Die Bibliothek unterstützt .NET-Versionen wie .NET 10, 9, 8, 7, 6, 5, .NET Core 3.1+, .NET Standard 2.0+ und .NET Framework 4.6.2+, sodass Sie die gleiche ImageToPdf Methode in einem .NET 10-Projekt ohne spezielle Konfiguration verwenden können.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen