Zum Fußzeileninhalt springen
IRONPDF NUTZEN

Wie man eine PDF-Datei in C# öffnet

As one of the most popular formats for digital documents, PDF allows users to generate invoices, print bank statements, and so much more. PDFs also allow users to sign documents digitally, as well as provide secure authentication. Learn about IronPDF abilities for creating, reading, and editing PDFs with ease. In this article, we are going to generate a PDF file in C# using IronPDF's C# integration and read the PDF using Acrobat Reader/Adobe Reader. We are also going to read the PDF file in C# using IronPDF.

How to Open PDF in C#

  1. Open Visual Studio and install the IronPdf NuGet Package
  2. Adding references to the code - enabling the use of available classes and functions
  3. Declare a common object for ChromePdfRenderer
  4. Using the RenderHtmlAsPdf function
  5. Using System.Diagnostics.Process.Start

1. Open Visual Studio and Install the NuGet Package

Open Visual Studio and go to the "File menu." Select "New Project," then select Console Application/Windows Forms/WPF Application. IronPDF can be used on all applications. You can also use it in apps such as Webform, MVC/MVC Core.

How to Open a PDF File in C#, Figure 1: Create a new project in Visual Studio Create a new project in Visual Studio

Enter the project name and select the file path in the appropriate text box. Then click the "Create" button. Next, select the required .NET Framework. Now the project will generate the structure for the selected application. If you have selected the console application, it will now open the Program.cs file where you can enter the code and build/run the application.

How to Open a PDF File in C#, Figure 2: Configure .Net project in Visual Studio Configure .Net project in Visual Studio

Next Install NuGet Package Install IronPdf from NuGet

Left-click the project and a menu will pop up. Select NuGet Package Manager from the menu and search for IronPDF. Select the first result in the NuGet Package dialog and click the Install/Download option.

How to Open a PDF File in C#, Figure 3: Install IronPdf package in NuGet Package Manager Install IronPdf package in NuGet Package Manager

Alternatively:

In Visual Studio go to Tools -> NuGet Package Manager -> Package Manager Console

Enter the following code on the Package Manager Console tab.

Install-Package IronPdf

Now the package will download/install on the current project and it is ready to use in the code.

2. Adding Reference to the Code - Enabling the use of available classes and functions

Add the reference IronPdf to the code as shown below. This will allow us to use the class and functions available from IronPdf in our code.

3. Declare a Common Object for ChromePdfRenderer

Declaring a common object for ChromePdfRenderer from IronPDF will help you convert any web page or HTML snippet into a PDF using IronPDF. By creating a common object, we will be able to use it without creating any more objects of the same class, allowing us to reuse the code more than once. Multiple functions can be used to create PDF files with IronPDF. We can use strings, transform URLs into PDF, or HTML files and convert them into PDFs, which can then be saved to the desired location.

We can also use a static function without creating any object for the ChromePdfRenderer. The static function is as follows:

We can use any one of these static methods to generate a PDF file. We can also include setting various PDF document options such as margins, titles, DPI, headers, footers, text, etc. By using ChromePdfRenderOptions, we can pass parameters to any one of these static methods.

We can declare the ChromePdfRenderOptions as common or individual for every PDF document. It is very simple and easy to use. We are going to use any one of the non-static functions to generate a PDF file and save it to a default location.

4. Using the RenderHtmlAsPdf

We can use any one of the above IronPDF functions to create a PDF. If you are using the function name RenderHtmlAsPdf, then pass any string as a parameter and then use the SaveAs Pdf file option from IronPDF function to save the PDF at the desired file path. While using the SaveAs function, we need to pass the filename and location as parameters, or if we are using a Windows application, we can use the SaveAs dialog to save the PDF file to the desired location. With the help of an HTML string, we can format the PDF document. Also, we can use CSS for designing text in PDF via HTML, and we can use any HTML tag to design a PDF document, as IronPDF does not have any restrictions on using HTML tags.

When we use large HTML text, it is difficult to add all the HTML text to the text box, so we can use another method which we have referred to above as RenderHtmlFileAsPdf, which will help us to convert all the HTML into a PDF document. With this method, we can add large HTML files. Also, we can include an external CSS file in these HTML files, as well as external images, etc.

IronPDF also helps us print data from any links using the RenderUrlAsPdf function. This function processes the link to generate a PDF and saves the PDF files to the desired file path using the SaveAs function. This IronPDF function will include the CSS and all the images available on the site.

The following code shows an example of the IronPDF function.

using IronPdf; // Ensure you add the IronPdf namespace

// Create an instance of the ChromePdfRenderer class
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render a PDF from a simple HTML string
PdfDocument pdf = renderer.RenderHtmlAsPdf("Hello IronPdf");

// Specify the path where the resulting PDF will be saved
var outputPath = "DemoIronPdf.pdf";

// Save the PDF document to the specified path
pdf.SaveAs(outputPath);

// Open the resulting PDF document using the default associated application
System.Diagnostics.Process.Start(outputPath);
using IronPdf; // Ensure you add the IronPdf namespace

// Create an instance of the ChromePdfRenderer class
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render a PDF from a simple HTML string
PdfDocument pdf = renderer.RenderHtmlAsPdf("Hello IronPdf");

// Specify the path where the resulting PDF will be saved
var outputPath = "DemoIronPdf.pdf";

// Save the PDF document to the specified path
pdf.SaveAs(outputPath);

// Open the resulting PDF document using the default associated application
System.Diagnostics.Process.Start(outputPath);
Imports IronPdf ' Ensure you add the IronPdf namespace

' Create an instance of the ChromePdfRenderer class
Private renderer As New ChromePdfRenderer()

' Render a PDF from a simple HTML string
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("Hello IronPdf")

' Specify the path where the resulting PDF will be saved
Private outputPath = "DemoIronPdf.pdf"

' Save the PDF document to the specified path
pdf.SaveAs(outputPath)

' Open the resulting PDF document using the default associated application
System.Diagnostics.Process.Start(outputPath)
$vbLabelText   $csharpLabel

This example shows how we can use the IronPDF function to generate a PDF file from a string. In this code, we have created an instance object for the ChromePdfRenderer, and then by using the instance object with the help of RenderHtmlAsPdf, we generate the PDF file. Then, by using the SaveAs IronPDF function, we can save the PDF file on the given path. If we don't specify a file path, it will be saved at the execution location in the program.

5. Using System.Diagnostics.Process.Start to Preview the PDF file

For this last step, we are using System.Diagnostics.Process.Start to preview a PDF file. This function invokes the command line function to open the PDF file from the path. If we have a PDF reader, it will open the saved PDF file in the reader. If we do not have a PDF reader, it will open with a dialog, and from the dialog, we need to select the program to open the PDF.

How to Open a PDF File in C#, Figure 4: The PDF file displayed in a default PDF Reader The PDF file displayed in a default PDF Reader

We can read PDF files using IronPDF, and this will read the PDF document line-by-line. We are even able to open a password-restricted PDF file using IronPDF. The following code demonstrates how to read a PDF document.

using IronPdf; // Ensure you add the IronPdf namespace

// Open a password-protected PDF
PdfDocument pdf = PdfDocument.FromFile("encrypted.pdf", "password");

// Extract all text from the PDF document
string allText = pdf.ExtractAllText();

// Extract all images from the PDF document
IEnumerable<System.Drawing.Image> allImages = pdf.ExtractAllImages();

// Iterate through each page in the document
for (var index = 0; index < pdf.PageCount; index++)
{
    // Page numbers are typically 1-based, so add 1 to the index
    int pageNumber = index + 1;

    // Extract text from the current page
    string text = pdf.ExtractTextFromPage(index);

    // Extract images from the current page
    IEnumerable<System.Drawing.Image> images = pdf.ExtractImagesFromPage(index);
}
using IronPdf; // Ensure you add the IronPdf namespace

// Open a password-protected PDF
PdfDocument pdf = PdfDocument.FromFile("encrypted.pdf", "password");

// Extract all text from the PDF document
string allText = pdf.ExtractAllText();

// Extract all images from the PDF document
IEnumerable<System.Drawing.Image> allImages = pdf.ExtractAllImages();

// Iterate through each page in the document
for (var index = 0; index < pdf.PageCount; index++)
{
    // Page numbers are typically 1-based, so add 1 to the index
    int pageNumber = index + 1;

    // Extract text from the current page
    string text = pdf.ExtractTextFromPage(index);

    // Extract images from the current page
    IEnumerable<System.Drawing.Image> images = pdf.ExtractImagesFromPage(index);
}
Imports IronPdf ' Ensure you add the IronPdf namespace

' Open a password-protected PDF
Private pdf As PdfDocument = PdfDocument.FromFile("encrypted.pdf", "password")

' Extract all text from the PDF document
Private allText As String = pdf.ExtractAllText()

' Extract all images from the PDF document
Private allImages As IEnumerable(Of System.Drawing.Image) = pdf.ExtractAllImages()

' Iterate through each page in the document
For index = 0 To pdf.PageCount - 1
	' Page numbers are typically 1-based, so add 1 to the index
	Dim pageNumber As Integer = index + 1

	' Extract text from the current page
	Dim text As String = pdf.ExtractTextFromPage(index)

	' Extract images from the current page
	Dim images As IEnumerable(Of System.Drawing.Image) = pdf.ExtractImagesFromPage(index)
Next index
$vbLabelText   $csharpLabel

The above code shows how we can read PDF files using IronPDF. IronPDF first reads the PDF document from the entered string filename, and it also allows users to include a password if there is one. It will read all the lines. This is very useful when we need to get data from a PDF, as it reduces the amount of manual work and does not require any human supervision.

Check out our code samples on PDF security and password handling.

Conclusion

IronPDF provides a simple and easy way to create a PDF with straightforward steps. The IronPDF library can be used in various environments such as Windows Forms, mobile apps, and web apps using .NET Framework or .Net Core's latest version. We don't need a separate library for each platform. We only need IronPDF to generate the PDF.

IronPDF offers a free trial key and you can currently buy five products from Iron Software for a bundled price package.

You can download a C# file project to help get started with IronPdf.

Häufig gestellte Fragen

Wie kann ich ein PDF in C# erstellen?

Mit IronPDF können Sie ein PDF in C# erstellen, indem Sie die Methode RenderHtmlAsPdf verwenden, die HTML-Zeichenketten oder -Dateien in das PDF-Format konvertiert. Anschließend können Sie das PDF mit der Methode SaveAs speichern.

Welche Schritte sollte ich befolgen, um IronPDF in einem C#-Projekt einzurichten?

Um IronPDF in einem C#-Projekt einzurichten, installieren Sie das IronPDF-NuGet-Paket über den NuGet-Paket-Manager von Visual Studio. Fügen Sie anschließend die erforderlichen Verweise in Ihrem Code hinzu und verwenden Sie die Klassen und Methoden von IronPDF zur PDF-Bearbeitung.

Wie öffne ich eine PDF-Datei in C#?

Sie können eine PDF-Datei in C# mit IronPDF öffnen, indem Sie zuerst das PDF-Dokument mit den Methoden von IronPDF laden und es dann mit System.Diagnostics.Process.Start anzeigen, wodurch die PDF-Datei mit dem Standard-PDF-Reader gestartet wird.

Kann IronPDF mit kennwortgeschützten PDF-Dateien umgehen?

Ja, IronPDF kann passwortgeschützte PDF-Dateien verarbeiten. Sie müssen das Passwort beim Öffnen der Datei mit den Funktionen von IronPDF eingeben, um auf geschützte PDF-Dokumente zugreifen und diese bearbeiten zu können.

Wie extrahiere ich Text aus einem PDF mit C#?

Um mit C# Text aus einer PDF-Datei zu extrahieren, können Sie die ExtractAllText -Methode von IronPDF verwenden, die den Textinhalt eines PDF-Dokuments abruft und zurückgibt.

Ist es möglich, CSS-Stile zu PDFs hinzuzufügen, die mit C# erstellt wurden?

Ja, das ist möglich. IronPDF ermöglicht das Hinzufügen von CSS-Stilen zu PDFs, indem diese in den HTML-Inhalt integriert werden, den Sie in PDFs umwandeln, was eine reiche Formatierung und Gestaltung ermöglicht.

Welche Umgebungen werden für die PDF-Manipulation mit IronPDF unterstützt?

IronPDF unterstützt mehrere Umgebungen, einschließlich Windows Forms, mobilen Apps und Web-Apps, die sowohl mit dem .NET Framework als auch mit .NET Core entwickelt wurden, was Flexibilität bei der Anwendungsentwicklung bietet.

Wie kann ich IronPDF ausprobieren, bevor ich es kaufe?

IronPDF bietet eine kostenlose Testversion an. Mit dem Testschlüssel können Sie die Funktionen und Möglichkeiten von IronPDF erkunden, bevor Sie sich zum Kauf entscheiden.

Was sind die Vorteile der Verwendung von IronPDF zur Erstellung von PDFs in C#?

IronPDF vereinfacht den Prozess der PDF-Erstellung in C#, indem es leistungsstarke Funktionen wie HTML-zu-PDF-Konvertierung, Kennwortschutz und Inhaltsextraktion bietet, die das Handling von PDFs in .NET-Anwendungen erleichtern.

Wie kann ich ein erstelltes PDF in C# ohne Speichern in der Vorschau anzeigen?

Um ein erstelltes PDF in C# ohne Speichern in der Vorschau anzuzeigen, erstellen Sie das PDF mit IronPDF und verwenden dann System.Diagnostics.Process.Start, um das PDF direkt mit der Standard-PDF-Reader-Anwendung zu öffnen.

.NET 10-Kompatibilität: Unterstützt IronPDF .NET 10-Projekte?

Ja – IronPDF ist vollständig mit .NET 10 kompatibel. Es unterstützt die Verwendung in .NET 10-Projekten sofort, einschließlich der HTML-zu-PDF-Konvertierung, des URL-Renderings und aller Funktionen des ChromePdfRenderers. Für die Nutzung von IronPDF in .NET 10-Anwendungen ist keine zusätzliche Konfiguration erforderlich.

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