C# Konwertuj PNG na PDF (Samouczek z przykładem kodu)
This article will guide you on how to convert PNG to PDF using the IronPDF image conversion API in C#.
How to Convert PNG to PDF in C#
- Install C# library to convert PNG to PDF
- Import single or multiple available PNG images in C#
- Convert PNG to PDF using
ImageToPdfmethod - Export generated PDF document to desired location
- Perform 3 & 4 actions in 1 line of C# code
Topics Covered in the Tutorial
W tym samouczku omówione zostaną następujące tematy:
- Wprowadzenie do biblioteki IronPDF
- Utwórz projekt w języku C#
- Zainstaluj bibliotekę IronPDF
- Metoda 1: Rozwiązanie z wykorzystaniem menedżera pakietów NuGet
- Metoda 2: Konsola menedżera pakietów NuGet
- Metoda 3: Korzystanie z pliku DLL
- Dodaj przestrzeń nazw IronPDF
- Konwersja obrazów JPG na dokumenty PDF
- Podsumowanie
Wymagania dotyczące korzystania z IronPDF:
- You should have basic knowledge of C# languages.
- Powinieneś posiadać podstawową wiedzę na temat aplikacji Windows.
1. Wprowadzenie do biblioteki IronPDF
Rozwiązanie IronPDF .NET PDF Library to marzenie programistów, zwłaszcza inżynierów oprogramowania korzystających z języka C#. Korzystając z tego doskonałego narzędzia Iron Software, można z łatwością stworzyć podstawową bibliotekę PDF dla platformy .NET. IronPDF sprawi, że konwersja plików PDF z różnych formatów będzie łatwym i oszczędzającym czas procesem.
It also enables you to construct a PDF file using HTML5, JavaScript, CSS, and images. Możesz płynnie edytować, stemplować oraz dodawać nagłówki i stopki do pliku PDF. Furthermore, it makes it very easy to read PDF text, extract images, or convert images to PDF programmatically.
Niektóre z ważnych funkcji to:
- Twórz dokumenty PDF z HTML4/5, CSS, JavaScript i obrazów.
- Generate PDF documents from URLs.
- Load URLs with custom-network login credentials, HTTP headers, proxies, cookies, user-agents, and form variables, allowing login behind HTML login forms.
- Szyfrowanie i deszyfrowanie plików PDF.
- Merging existing PDF files.
- Creating and editing PDF forms.
Here, this tutorial focuses on the conversion of PNG files to PDF documents. IronPDF obsługuje konwersję niemal każdego formatu obrazu. Supported image formats are JPG, PNG, GIF, TIFF, SVG, and BMP.
2. Utwórz projekt w języku C
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.
Zacznijmy od utworzenia projektu w języku C#.
- Otwórz program Visual Studio.
- Either create a new C# project or open an existing one.
- Nadaj nazwę projektowi.
- 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.
Console program after creating project
3. Zainstaluj bibliotekę IronPDF
Metoda 1: Rozwiązanie z wykorzystaniem menedżera pakietów NuGet
Otwórz z Eksploratora rozwiązań
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.
Przeglądaj IronPDF
Metoda 2: Konsola menedżera pakietów NuGet
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
Metoda 3: Korzystanie z pliku DLL
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:
- Kliknij prawym przyciskiem myszy na Solution w Solution Explorer
- Select "References"
- Wyszukaj bibliotekę IronPDF.dll
- Kliknij OK
Gotowe! IronPDF is downloaded, installed, and ready to use to convert PNG to PDF format.
4. Dodaj przestrzeń nazw IronPDF
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
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
In the above code snippet, the asset folder contains only one PNG file. The output file looks like this:
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.
Multiple PNG images to PDF document
6. Podsumowanie
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. Ponadto, dzięki obecnej ofercie specjalnej możesz otrzymać pięć produktów IronPDF w cenie zaledwie dwóch! Visit this IronPDF licensing information page for more information about licensing.
Często Zadawane Pytania
Jak mogę przekonwertować obrazy PNG do formatu PDF za pomocą języka C#?
Możesz użyć metody ImageToPdf biblioteki IronPDF, aby łatwo konwertować obrazy PNG do formatu PDF w języku C#. Metoda ta pozwala na konwersję pojedynczego obrazu lub wielu obrazów do jednego dokumentu PDF przy użyciu minimalnej ilości kodu.
Jakie kroki należy wykonać, aby skonfigurować IronPDF w projekcie C#?
Aby skonfigurować IronPDF w projekcie C#, można zainstalować go za pomocą menedżera pakietów NuGet w Visual Studio, użyć konsoli menedżera pakietów NuGet z poleceniem Install-Package IronPdf lub pobrać plik DLL i dodać go bezpośrednio do odwołań projektu.
Czy za pomocą tej biblioteki mogę konwertować inne formaty plików do formatu PDF?
Tak, IronPDF może konwertować różne formaty plików, w tym HTML, JavaScript, CSS oraz obrazy, takie jak JPG, GIF, TIFF, SVG i BMP, na dokumenty PDF.
Jakie warunki wstępne są wymagane do konwersji obrazów do formatu PDF w języku C#?
Powinieneś posiadać podstawową wiedzę z zakresu programowania w języku C# oraz znajomość obsługi programu Visual Studio i aplikacji .NET, aby efektywnie wykorzystywać IronPDF do konwersji obrazów do formatu PDF.
Jak włączyć IronPDF do mojego kodu C# w celu konwersji obrazów?
Dodaj przestrzeń nazw using IronPdf; na początku pliku C#, aby uzyskać dostęp do wszystkich niezbędnych funkcji udostępnianych przez IronPDF.
Czy można przekonwertować wiele obrazów PNG do jednego pliku PDF?
Tak, korzystając z IronPDF, można przekonwertować wiele obrazów PNG do jednego dokumentu PDF za pomocą metody ImageToPdf.
Jakie narzędzia programistyczne są zalecane do pracy z IronPDF?
Aby zapewnić optymalną kompatybilność i wydajność podczas pracy z IronPDF, zaleca się korzystanie z najnowszej wersji Visual Studio z .NET Core w wersji 3.1 lub wyższej.
Czy mogę wypróbować IronPDF przed zakupem?
Tak, możesz wypróbować IronPDF, korzystając z jego darmowej wersji i klucza próbnego, co pozwoli Ci ocenić jego cechy i funkcjonalność przed podjęciem decyzji o zakupie.
Jak mogę rozwiązać problemy podczas konwersji obrazów do formatu PDF za pomocą IronPDF?
Upewnij się, że w projekcie uwzględniono prawidłowe przestrzenie nazw i odwołania. Sprawdź, czy w składni kodu nie ma błędów, i upewnij się, że .NET Framework jest zgodny z IronPDF. Aby uzyskać dodatkowe wsparcie, zapoznaj się z dokumentacją IronPDF lub forami społeczności.
Zgodność z .NET 10: Czy mogę używać IronPDF do konwersji plików PNG do formatu PDF w projekcie .NET 10?
Tak — IronPDF jest w pełni kompatybilny z .NET 10. Biblioteka obsługuje wersje .NET, w tym .NET 10, 9, 8, 7, 6, 5, .NET Core 3.1+, .NET Standard 2.0+ oraz .NET Framework 4.6.2+, co pozwala na użycie tej samej metody ImageToPdf w projekcie .NET 10 bez konieczności specjalnej konfiguracji.




