跳至页脚内容
使用IRONPDF

如何在.NET MAUI中嵌入文本显示PDF

This is a tutorial that will walk you through showing PDF-embedded text in .NET MAUI using IronPDF.

.NET Multi-platform App UI (MAUI) simplifies multi-platform app development. With the new and improved version of Xamarin.Forms, developers can create apps for Android, iOS, macOS, and Windows with a single project. PDF files, known for preserving fonts, images, and layout, are commonly managed using this technology.

The IronPDF library offers powerful PDF handling capabilities in this context. Developers can leverage the power of IronPDF to work with embedded text effortlessly, simplifying the process of generating and manipulating PDF files while adhering to default settings for consistent rendering.

Understanding PDF Embedded Text and Font Embedding

PDF-embedded text, or PDF fonts, are text entities embedded in a PDF file. It is crucial when it comes to consistency and accurate rendering across PDF viewer applications, including popular applications like Adobe InDesign. By embedding fonts within the PDF document, the correct fonts are preserved, regardless of the type of PDF viewer application used, or whether the specific font is installed on the viewer's device.

Embedding fonts can sometimes increase the size of the PDF document, but it is crucial to maintain the original document's look and feel. The Adobe PDF settings often determine if the fonts in a PDF are embedded or not.

There are different kinds of embedded fonts in a PDF document:

  1. Embedded fonts: The entire font is embedded in the document.
  2. Subset embedded fonts: Only a subset of the fonts used in the original document is embedded.
  3. No embedded fonts: No fonts are embedded in the document.

In Adobe Acrobat, you can verify whether fonts are embedded by checking the document properties. By default, fonts are embedded in a PDF file. However, these settings can be changed using Adobe Acrobat Pro or other similar tools.

The term 'flattened PDF' is often used in the context of PDF documents where all fonts have been embedded, making the file self-contained and ensuring it appears the same across all systems and PDF viewers.

IronPDF C#

IronPDF Documentation is a powerful C# PDF library that allows developers to generate, read, and edit PDF files in .NET applications. You can learn to generate PDF files from HTML with IronPDF. An interesting feature of IronPDF is the ability to work with embedded text in PDF files. The ability to embed fonts in a PDF file is crucial for preserving the document's original appearance, even when the PDF file is viewed or printed on a system that does not have access to the original fonts used in the document. Let's understand how to display embedded text in a PDF using IronPDF in .NET MAUI.

前提条件

Before starting with the tutorial, make sure you have the following requirements fulfilled:

  1. .NET MAUI: Microsoft's unified UI toolkit that allows you to create apps for Android, iOS, macOS, and Windows with a single shared codebase. You can download the .NET MAUI from the Microsoft website.
  2. Visual Studio 2022 (or later): A powerful and user-friendly Integrated Development Environment (IDE) for .NET programming. You can download Visual Studio from the Microsoft website. Ensure you have the .NET MAUI workloads installed on Visual Studio 2022.
  3. IronPDF library: This is a PDF processing library for .NET, and we will use it to interact with the PDF files. You can install IronPDF via NuGet, which is a package manager for the Microsoft development platform.
  4. An Adobe PDF file: For the sake of this tutorial, we will need a PDF file.

Create a .NET MAUI App

Follow these steps to create a new .NET MAUI App:

Launch Visual Studio 2022: After launching, navigate to File > New > Project. In the project template window, select .NET MAUI App and then click on Next.

How to Display PDF Embedded Text in .NET MAUI, Figure 1: Create a .NET MAUI App in Visual Studio Create a .NET MAUI App in Visual Studio

Name your project: In the next window, you'll need to give your project a name. Let's name it IronPDF_Read_and_View. Choose a location to save your project and then click Next.

How to Display PDF Embedded Text in .NET MAUI, Figure 2: Configure the project 配置项目

Select Framework: Select .NET Framework from the drop-down list. Select the latest .NET Framework for a smooth process and click the "Create" button.

How to Display PDF Embedded Text in .NET MAUI, Figure 3: .NET Framework selection .NET 框架选择

安装 IronPDF。

After creating the .NET MAUI App, the next step is to install the IronPDF library. Here's how you can do that:

  1. Open the NuGet Package Manager: Navigate to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

    How to Display PDF Embedded Text in .NET MAUI, Figure 4: Navigate to NuGet Package Manager Navigate to NuGet Package Manager

  2. Search for IronPDF: In the opened window, click on Browse and type IronPdf in the search box.

    How to Display PDF Embedded Text in .NET MAUI, Figure 5: Search for IronPDF in NuGet Package Manager UI Search for IronPDF in NuGet Package Manager UI

  3. Install IronPDF: Once you see IronPDF in the search results, click on it. Make sure that the checkbox for your project in the right panel is checked and then click on Install.

Accept any prompts for permissions or terms of usage that appear during the installation process.

You can also install IronPDF using the NuGet Package Manager Console by using the following command:

Install-Package IronPdf

Building the User Interface

Now let's go ahead and build the user interface (UI) for this application. The MainPage.xaml file will serve as the landing page. It will have a button to open the PDF file, and labels to show the selected file's name and its content.

Let's proceed with creating the user interface:

Open the MainPage.xaml file: This file contains the layout of the main page. You can find this file under the Pages folder in the Solution Explorer.

Define the Layout: We are going to use a <ScrollView> control which allows the user to scroll through the contents of the page when it cannot fit entirely on the screen. Inside the ScrollView, we will use a <StackLayout> for stacking our controls vertically. Inside the StackLayout, we have three <Frame> controls. Each Frame is used to hold a distinct section of our page, providing a neat and organized appearance.

Add Controls: The first Frame has a <VerticalStackLayout> which holds a Label and a Button. The Label displays the application name, and the Button allows the user to open a PDF file. The Clicked attribute is assigned the method OpenAndReadFile which will be defined later in the code-behind file.

<VerticalStackLayout
    Spacing="25"
    Padding="30,0"
    VerticalOptions="Center">
    <Label
        Text="IronPDF MAUI Application"
        SemanticProperties.HeadingLevel="Level1"
        SemanticProperties.Description="IronPDF MAUI Application"
        FontSize="30"
        HorizontalOptions="Center"
        FontAttributes="Bold"

    />
    <Button
        x:Name="openFileBtn"
        Text="Open PDF File"
        SemanticProperties.Hint="Open PDF File"
        Clicked="OpenAndReadFile"
        HorizontalOptions="Center" />
</VerticalStackLayout>
<VerticalStackLayout
    Spacing="25"
    Padding="30,0"
    VerticalOptions="Center">
    <Label
        Text="IronPDF MAUI Application"
        SemanticProperties.HeadingLevel="Level1"
        SemanticProperties.Description="IronPDF MAUI Application"
        FontSize="30"
        HorizontalOptions="Center"
        FontAttributes="Bold"

    />
    <Button
        x:Name="openFileBtn"
        Text="Open PDF File"
        SemanticProperties.Hint="Open PDF File"
        Clicked="OpenAndReadFile"
        HorizontalOptions="Center" />
</VerticalStackLayout>
XML

The second Frame has a <HorizontalStackLayout> which holds two Labels. The first Label is for displaying the static text "Selected File Name:", and the second Label named fileName displays the name of the selected file.

<HorizontalStackLayout
    Spacing="25"
    Padding="30,0"
    VerticalOptions="Center">
    <Label
        Text="Selected File Name: "
        SemanticProperties.HeadingLevel="Level2"
        SemanticProperties.Description="Selected File Name"
        FontSize="18"
        HorizontalOptions="Center"
        FontAttributes="Bold"
    />
    <Label
        x:Name="fileName"
        Text=""
        SemanticProperties.HeadingLevel="Level3"
        SemanticProperties.Description="Selected File Name"
        FontSize="18"
        HorizontalOptions="Center" 
     />
</HorizontalStackLayout>
<HorizontalStackLayout
    Spacing="25"
    Padding="30,0"
    VerticalOptions="Center">
    <Label
        Text="Selected File Name: "
        SemanticProperties.HeadingLevel="Level2"
        SemanticProperties.Description="Selected File Name"
        FontSize="18"
        HorizontalOptions="Center"
        FontAttributes="Bold"
    />
    <Label
        x:Name="fileName"
        Text=""
        SemanticProperties.HeadingLevel="Level3"
        SemanticProperties.Description="Selected File Name"
        FontSize="18"
        HorizontalOptions="Center" 
     />
</HorizontalStackLayout>
XML

The third Frame has a <VerticalStackLayout> which holds two Labels. The first Label displays the static text "PDF Content", and the second Label named content displays the content of the PDF file.


<VerticalStackLayout>
    <Label
        Text="PDF Content"
        SemanticProperties.HeadingLevel="Level2"
        FontSize="25"
        FontAttributes="Bold"
        HorizontalOptions="Center" 
    />
    <Label
        x:Name="content"
        FontSize="18"
        HorizontalTextAlignment="Start"
    />
</VerticalStackLayout>

<VerticalStackLayout>
    <Label
        Text="PDF Content"
        SemanticProperties.HeadingLevel="Level2"
        FontSize="25"
        FontAttributes="Bold"
        HorizontalOptions="Center" 
    />
    <Label
        x:Name="content"
        FontSize="18"
        HorizontalTextAlignment="Start"
    />
</VerticalStackLayout>
XML

Your final MainPage.xaml should look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                x:Class="IronPDF_Read_and_View.MainPage">
    <ScrollView>
        <StackLayout>
            <Frame>
                <VerticalStackLayout
                    Spacing="25"
                    Padding="30,0"
                    VerticalOptions="Center">
                    <Label
                        Text="IronPDF MAUI Application"
                        SemanticProperties.HeadingLevel="Level1"
                        SemanticProperties.Description="IronPDF MAUI Application"
                        FontSize="30"
                        HorizontalOptions="Center"
                        FontAttributes="Bold"

                    />
                    <Button
                        x:Name="openFileBtn"
                        Text="Open PDF File"
                        SemanticProperties.Hint="Open PDF File"
                        Clicked="OpenAndReadFile"
                        HorizontalOptions="Center" />
                </VerticalStackLayout>
            </Frame>
            <Frame>
                <HorizontalStackLayout
                        Spacing="25"
                        Padding="30,0"
                        VerticalOptions="Center">
                    <Label
                            Text="Selected File Name: "
                            SemanticProperties.HeadingLevel="Level2"
                            SemanticProperties.Description="Selected File Name"
                            FontSize="18"
                            HorizontalOptions="Center"
                            FontAttributes="Bold"

                        />
                    <Label
                            x:Name="fileName"
                            Text=""
                            SemanticProperties.HeadingLevel="Level3"
                            SemanticProperties.Description="Selected File Name"
                            FontSize="18"
                            HorizontalOptions="Center" 
                        />
                </HorizontalStackLayout>
            </Frame>
            <Frame>
                <VerticalStackLayout>
                    <Label
                            Text="PDF Content"
                            SemanticProperties.HeadingLevel="Level2"
                            FontSize="25"
                        FontAttributes="Bold"
                            HorizontalOptions="Center" 
                        />
                    <Label
                        x:Name="content"
                        FontSize="18"
                        HorizontalTextAlignment="Start"
                        />
                </VerticalStackLayout>
            </Frame>
        </StackLayout>
    </ScrollView>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                x:Class="IronPDF_Read_and_View.MainPage">
    <ScrollView>
        <StackLayout>
            <Frame>
                <VerticalStackLayout
                    Spacing="25"
                    Padding="30,0"
                    VerticalOptions="Center">
                    <Label
                        Text="IronPDF MAUI Application"
                        SemanticProperties.HeadingLevel="Level1"
                        SemanticProperties.Description="IronPDF MAUI Application"
                        FontSize="30"
                        HorizontalOptions="Center"
                        FontAttributes="Bold"

                    />
                    <Button
                        x:Name="openFileBtn"
                        Text="Open PDF File"
                        SemanticProperties.Hint="Open PDF File"
                        Clicked="OpenAndReadFile"
                        HorizontalOptions="Center" />
                </VerticalStackLayout>
            </Frame>
            <Frame>
                <HorizontalStackLayout
                        Spacing="25"
                        Padding="30,0"
                        VerticalOptions="Center">
                    <Label
                            Text="Selected File Name: "
                            SemanticProperties.HeadingLevel="Level2"
                            SemanticProperties.Description="Selected File Name"
                            FontSize="18"
                            HorizontalOptions="Center"
                            FontAttributes="Bold"

                        />
                    <Label
                            x:Name="fileName"
                            Text=""
                            SemanticProperties.HeadingLevel="Level3"
                            SemanticProperties.Description="Selected File Name"
                            FontSize="18"
                            HorizontalOptions="Center" 
                        />
                </HorizontalStackLayout>
            </Frame>
            <Frame>
                <VerticalStackLayout>
                    <Label
                            Text="PDF Content"
                            SemanticProperties.HeadingLevel="Level2"
                            FontSize="25"
                        FontAttributes="Bold"
                            HorizontalOptions="Center" 
                        />
                    <Label
                        x:Name="content"
                        FontSize="18"
                        HorizontalTextAlignment="Start"
                        />
                </VerticalStackLayout>
            </Frame>
        </StackLayout>
    </ScrollView>
</ContentPage>
XML

When users press the "Open PDF File" button, it will trigger the OpenAndReadFile method. This method will be defined in our MainPage.xaml.cs (code-behind) file. Our labels, fileName, and content, will show the file name of the selected PDF file and the content of the PDF file respectively.

The Code Behind MainPage.xaml.cs

The logic of the application resides in the MainPage.xaml.cs file, also known as the code-behind file. Here, we define the OpenAndReadFile method which is responsible for opening the file picker, allowing the user to select a PDF file, extracting the content of the selected PDF file, and displaying it on the UI.

Open MainPage.xaml.cs: Find this file in the Solution Explorer under the Pages folder. This is where we'll add our method.

Add the filePath field: At the top of the MainPage class, declare a string field named filePath. We will use this field to store the path of the selected file.

string filePath = string.Empty;
string filePath = string.Empty;
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Initialize Components: In the MainPage constructor, call the InitializeComponent method. This method is called automatically to initialize the page and its controls.

public MainPage()
{
    InitializeComponent();
}
public MainPage()
{
    InitializeComponent();
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Implement the OpenAndReadFile method: This method is marked as async because we're going to use the await keyword inside it. The FilePicker.PickAsync method is used to open the file picker. When the user selects a file, the file name is stored in the fileName label, and the file path in the filePath field. The IronPDF library is used to open the PDF document and extract all text from it. The extracted text is then assigned to the content label.

private async void OpenAndReadFile(object sender, EventArgs e)
{
    FileResult result = await FilePicker.PickAsync();
    fileName.Text = result.FileName;
    filePath = result.FullPath;

    IronPdf.License.LicenseKey = "Your-License-Key";

    // Read PDF File
    var document = PdfDocument.FromFile(filePath);
    var pdfContent = document.ExtractAllText();
    content.Text = pdfContent;
}
private async void OpenAndReadFile(object sender, EventArgs e)
{
    FileResult result = await FilePicker.PickAsync();
    fileName.Text = result.FileName;
    filePath = result.FullPath;

    IronPdf.License.LicenseKey = "Your-License-Key";

    // Read PDF File
    var document = PdfDocument.FromFile(filePath);
    var pdfContent = document.ExtractAllText();
    content.Text = pdfContent;
}
Private Async Sub OpenAndReadFile(ByVal sender As Object, ByVal e As EventArgs)
	Dim result As FileResult = Await FilePicker.PickAsync()
	fileName.Text = result.FileName
	filePath = result.FullPath

	IronPdf.License.LicenseKey = "Your-License-Key"

	' Read PDF File
	Dim document = PdfDocument.FromFile(filePath)
	Dim pdfContent = document.ExtractAllText()
	content.Text = pdfContent
End Sub
$vbLabelText   $csharpLabel

The OpenAndReadFile method is now complete. It will be triggered when the user clicks on the Open PDF File button. You need to replace "Your-License-Key" with your actual IronPDF license key.

Here is the complete code:

using IronPdf;

public partial class MainPage : ContentPage
{
    string filePath = string.Empty;

    public MainPage()
    {
        InitializeComponent();
    }

    private async void OpenAndReadFile(object sender, EventArgs e)
    {
        FileResult result = await FilePicker.PickAsync();
        fileName.Text = result.FileName;
        filePath = result.FullPath;
        IronPdf.License.LicenseKey = "Your-License-Key";

        // Read PDF File
        var document = PdfDocument.FromFile(filePath);
        var pdfContent = document.ExtractAllText();
        content.Text = pdfContent;
    }
}
using IronPdf;

public partial class MainPage : ContentPage
{
    string filePath = string.Empty;

    public MainPage()
    {
        InitializeComponent();
    }

    private async void OpenAndReadFile(object sender, EventArgs e)
    {
        FileResult result = await FilePicker.PickAsync();
        fileName.Text = result.FileName;
        filePath = result.FullPath;
        IronPdf.License.LicenseKey = "Your-License-Key";

        // Read PDF File
        var document = PdfDocument.FromFile(filePath);
        var pdfContent = document.ExtractAllText();
        content.Text = pdfContent;
    }
}
Imports IronPdf

Partial Public Class MainPage
	Inherits ContentPage

	Private filePath As String = String.Empty

	Public Sub New()
		InitializeComponent()
	End Sub

	Private Async Sub OpenAndReadFile(ByVal sender As Object, ByVal e As EventArgs)
		Dim result As FileResult = Await FilePicker.PickAsync()
		fileName.Text = result.FileName
		filePath = result.FullPath
		IronPdf.License.LicenseKey = "Your-License-Key"

		' Read PDF File
		Dim document = PdfDocument.FromFile(filePath)
		Dim pdfContent = document.ExtractAllText()
		content.Text = pdfContent
	End Sub
End Class
$vbLabelText   $csharpLabel

Running the Application

Now that we have successfully set up the UI and defined the behavior of the application, it's time to see our application in action!

Start the Application: To run the application, you can either press F5 on your keyboard or click on the green 'Start Debugging' button in the toolbar at the top of Visual Studio. Ensure that the right target device or emulator is selected in the dropdown menu next to the 'Start Debugging' button.

Use the Application: Once the application launches, you will see a screen with the title "IronPDF MAUI Application" and a button labeled "Open PDF File".

How to Display PDF Embedded Text in .NET MAUI, Figure 6: The UI of the IronPDF MAUI Application The UI of the IronPDF MAUI Application

Open a PDF File: Click on the "Open PDF File" button. This will open a file picker, allowing you to browse and select a PDF file from your device or emulator.

How to Display PDF Embedded Text in .NET MAUI, Figure 7: File Selection Dialog File Selection Dialog

View the Content: Upon selecting a PDF file, the file name will be displayed under "Selected File Name:", and the content of the selected PDF file will be displayed under "PDF Content".

How to Display PDF Embedded Text in .NET MAUI, Figure 8: Display PDF content from selected PDF file Display PDF content from selected PDF file

Please note that if the selected PDF file is very large, it might take a few seconds to extract and display the text. Also, remember that the format of the extracted text may not match exactly with the original layout of the PDF file, as the ExtractAllText method details extract the embedded text content.

结论

This tutorial demonstrated how to build a .NET MAUI application using the IronPDF library to extract and display the text content from a PDF file. This project is a great example of how powerful and versatile .NET MAUI and the IronPDF library are when working with PDF files in your applications.

In addition to extracting texts and images in PDF files, the IronPDF library also supports a wide range of functionalities including interacting with PDF forms, dividing PDF files, rasterizing PDF pages to images, authentication behind HTML login forms, customizing headers and footers in PDF documents as well as supporting CSS files for pixel-perfect PDF files.

IronPDF is a commercial product with robust functionality for PDF manipulation, and it offers a free trial of IronPDF for you to test out its capabilities. If you find the product beneficial for your development needs and decide to use it for production, license plans start from $799.

常见问题解答

如何在.NET MAUI应用程序中显示PDF嵌入文本?

要在.NET MAUI应用程序中显示PDF嵌入文本,您可以使用IronPDF库。IronPDF允许您从PDF文件中提取文本并在您的应用程序中呈现。您需要在.NET MAUI中设置用户界面,以便用户选择和显示PDF内容。

为什么在PDF文档中嵌入字体很重要?

在PDF文档中嵌入字体很重要,因为它确保文档的外观在不同系统和PDF查看器中保持一致,无论用户设备上是否安装了字体。

如何在.NET MAUI项目中安装IronPDF?

要在.NET MAUI项目中安装IronPDF,请使用Visual Studio的NuGet包管理器。搜索IronPDF并将其安装到您的项目中。或者,您可以使用NuGet包管理器控制台命令:Install-Package IronPdf

在.NET MAUI应用程序中使用IronPDF有哪些好处?

IronPDF为.NET MAUI应用程序提供了多种好处,包括提取和呈现PDF文本、管理嵌入字体,并确保跨不同平台的高质量PDF呈现。它还提供了一套强大的PDF操作功能,并支持跨平台开发。

IronPDF能从任何PDF文件中提取文本吗?

是的,IronPDF可以从PDF文件中提取文本。然而,提取的文本格式可能会根据PDF的创建方式而略有不同于原始PDF布局。

设置.NET MAUI应用程序以处理PDF有哪些步骤?

设置.NET MAUI应用程序以处理PDF涉及安装.NET MAUI和Visual Studio与必要的工作负载,通过NuGet集成IronPDF库,并开发用户界面与PDF文件进行交互。此过程还包括编写C#代码以提取和显示PDF内容。

有免费的IronPDF版本可用于开发吗?

IronPDF提供免费的试用版本,允许开发人员测试其功能。对于生产用途,提供各种许可计划以满足不同需求。

使用IronPDF可以实现哪些类型的PDF操作?

使用IronPDF,您可以进行多种PDF操作,包括文本提取、与PDF表单交互、拆分PDF、将PDF页转换为图像,以及自定义文档页眉和页脚。

IronPDF 是否支持 .NET 10?我能否将其与 .NET 10 MAUI 项目一起使用?

是的——IronPDF 完全兼容 .NET 10。它支持 .NET 10 MAUI 项目,无需任何自定义变通方案即可立即利用 .NET 10 的性能改进和语言特性。IronPDF 同时支持 .NET 9、8、7、6、5、Core、Standard 和 Framework 版本,以及 .NET 10。(ironpdf.com)

在资源有限的设备(例如 iOS 或 Android)上使用 MAUI 中的 IronPDF 的 ExtractAllText 功能处理大型 PDF 时,是否存在任何限制或已知问题?

IronPDF 的ExtractAllText虽然能很好地处理标准 PDF 文件,但在移动设备上从超大型文档中提取嵌入文本时速度会变慢,并且会占用大量内存。开发者应考虑对大型文件实现分页或分块提取,并确保设备有足够的内存。此外,为了保证 UI 的响应速度,可能需要在后台线程中使用,以避免阻塞 MAUI 应用的主线程。(以上内容基于 .NET MAUI 的通用最佳实践以及 IronPDF 对 PDF 提取的处理方式。)

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。