How to Access All PDF DOM Objects

This article was translated from English: Does it need improvement?
Translated
View the article in English

访问 PDF DOM 对象指的是以类似于操作网页 DOM(文档对象模型)的方式与 PDF 文件的结构进行交互。 在 PDF 的上下文中,DOM 是文档内部结构的表示,允许开发人员以编程方式访问和操作不同的元素,如文本、图像、注释和元数据。

快速入门:使用 IronPDF 访问和更新 PDF DOM 元素

使用 IronPDF 强大的 DOM 访问功能,轻松操作您的 PDF 文档。 本快速指南演示了如何访问 PDF DOM、选择页面和修改文本对象。 加载您的 PDF,访问所需页面,并使用几行代码更新内容,就这么简单。 非常适合希望不费力进入 PDF 操作的开发人员。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var objs = IronPdf.ChromePdfRenderer.RenderUrlAsPdf("https://example.com").Pages.First().ObjectModel;
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程 (5 步)

  1. 下载 C# 库以访问 PDF DOM 对象
  2. 导入或渲染目标 PDF 文档
  3. 访问 PDF 的页面集合并选择所需页面
  4. 使用 ObjectModel 属性查看并与 DOM 对象交互
  5. 保存或导出修改后的 PDF 文档

访问 DOM 对象示例

ObjectModel 可以从 PdfPage 对象中访问。 首先,导入目标 PDF 并访问其 Pages 属性。 从那里选择任意页面,您将能够访问 ObjectModel 属性。

:path=/static-assets/pdf/content-code-examples/how-to/access-pdf-dom-object.cs
using IronPdf;
using System.Linq;

// Instantiate Renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PDF from a URL
PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");

// Access DOM Objects
var objects = pdf.Pages.First().ObjectModel;
Imports IronPdf
Imports System.Linq

' Instantiate Renderer
Private renderer As New ChromePdfRenderer()

' Create a PDF from a URL
Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")

' Access DOM Objects
Private objects = pdf.Pages.First().ObjectModel
$vbLabelText   $csharpLabel
class="content-img-align-center">
class="center-image-wrapper"> Debug

ObjectModel 属性目前由 ImageObjectPathObjectTextObject 组成。 每个对象包含关于它所在的页面索引、其边界框、缩放比例和转换的信息。 这些信息也可以被修改。

ImageObject:

  • Height: 图像的高度。
  • Width: 图像的宽度。
  • ExportBytesAsJpg: 一种将图像导出为 JPG 格式字节数组的方法。

PathObject:

  • FillColor: 路径的填充颜色。
  • StrokeColor: 路径的描边颜色。
  • Points: 定义路径的点集。

TextObject:

  • Color: 文本的颜色。
  • Contents: 实际的文本内容。

检索字符信息和边界框

当您需要指定精确的字符而不仅仅依赖于 Unicode 值以确保文本在与自定义字体组合时按预期显示时,能够检索边界框和字符信息是很有用的。 IronPDF 为开发人员提供了检索此类信息的方法。

我们首先从 PdfPage 对象访问 ObjectModel。 然后,我们深入访问 TextObjects,它返回一个集合。 最后,我们调用 GetGlyphInfo 方法来检索第一个元素的字符和边界框信息。

:path=/static-assets/pdf/content-code-examples/how-to/access-pdf-dom-object-retrieve-glyph.cs
using IronPdf;
using System.Linq;

PdfDocument pdf = PdfDocument.FromFile("invoice.pdf");

var glyph = pdf.Pages.First().ObjectModel.TextObjects.First().GetGlyphInfo();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
class="content-img-align-center">
class="center-image-wrapper"> Glyph Information

<hr

翻译 PDF 对象

有时候您需要通过重新定位元素(如文本或图像)来调整 PDF 的布局。 您可以通过更改其 Translate 属性,轻松将对象移动到页面上的新位置。

下面的代码示例渲染了一个使用 CSS Flexbox 将文本居中在 PDF 中的 HTML 字符串。 然后,我们访问第一个 TextObject,即单词“Centered”。

最后,我们通过为其 Translate 属性分配一个新的 PointF 来翻译 TextObject。 这将文本向右移动 200 点并向上移动 150 点,然后保存修改后的 PDF。

代码示例

:path=/static-assets/pdf/content-code-examples/how-to/access-pdf-dom-object-translate.cs
using IronPdf;
using System.Drawing;
using System.Linq;

// Setup the Renderer
var renderer = new ChromePdfRenderer();

// We use CSS Flexbox to perfectly center the text vertically and horizontally.
var html = @"
<div style='display: flex; justify-content: center; align-items: center; font-size: 48px;'>
    Centered
</div>";

// Render the HTML to a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

// Save the original PDF to see the "before" state
pdf.SaveAs("BeforeTranslate.pdf");

// Access the first text object on the first page
// In this simple HTML, this will be our "Centered" text block.
var textObject = pdf.Pages.First().ObjectModel.TextObjects.First();

// Apply the translation
// This moves the object 200 points to the right and 150 points up from its original position.
textObject.Translate = new PointF(200, 150);

// Save the modified PDF to see the "after" state
pdf.SaveAs("AfterTranslate.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

输出

如您在输出中所见,单词“Centered”从其原始位置向右移动了 200 点,向上移动了 150 点。

class="content-img-align-center">
class="center-image-wrapper"> Translate object

<hr

缩放 PDF 对象

您可以使用 Scale 属性调整任何 PDF 对象(例如文本或图像)的大小。 此属性充当放大倍数。 大于 1 的因子会增加对象的大小,而介于 0 和 1 之间的因子会减小它。

在此示例中,我们渲染了一个包含图像的 HTML 字符串。 然后,我们访问第一个 ImageObject 并将其缩放到原始大小的 70%。我们通过为其 Scale 属性分配一个新的 PointF,其值为两个轴的 0.7。 最后,我们保存修改后的 PDF。

代码示例

:path=/static-assets/pdf/content-code-examples/how-to/access-pdf-dom-object-scale.cs
using IronPdf;
using System.Drawing;
using System.Linq;

// Setup the Renderer
var renderer = new ChromePdfRenderer();

// The image is placed in a div to give it some space on the page.
string html = @"<img src='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTi8LuOR6_A98euPLs-JRwoLU7Nc31nVP15rw&s'>";

// Render the HTML to a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

// Save the PDF before scaling for comparison
pdf.SaveAs("BeforeScale.pdf");

// Access the first image object on the first page
var image = pdf.Pages.First().ObjectModel.ImageObjects.First();

// We scale the image to 70% of its original size on both the X and Y axes.
image.Scale = new PointF(0.7f, 0.7f);

// Save the modified PDF to see the result
pdf.SaveAs("AfterScale.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

输出

输出显示图像缩小到其原始大小的 70%。

class="content-img-align-center">
class="center-image-wrapper"> Scale object

<hr

移除 PDF 对象

您可以通过彻底删除对象(如文本块、形状或图像)来清理 PDF。 该过程包括访问 PDF DOM 对象集合,例如 ImageObjectsTextObjects,并从该集合中删除一个项目。 您可以通过在集合上调用 RemoveAt 方法并传递要删除对象的索引来删除对象。

在以下代码中,我们加载在前一个示例中创建的 BeforeScale.pdf 文件,并从第一页中删除第一个图像。

:path=/static-assets/pdf/content-code-examples/how-to/access-pdf-dom-object-remove.cs
using IronPdf;
using IronSoftware.Pdfium.Dom;
using System.Linq;

// Load the PDF file we created in the Scale example
 PdfDocument pdf = PdfDocument.FromFile("BeforeScale.pdf");

 // Access DOM Objects
 IPdfPageObjectModel objects = pdf.Pages.First().ObjectModel;

 // Remove first image
 objects.ImageObjects.RemoveAt(0);

 // Save the modified PDF
 pdf.SaveAs("removedFirstImage.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

准备好看看您还能做些什么吗? 请查看我们的教程页面:编辑 PDF

常见问题解答

如何在 C# 中访问 PDF DOM 对象?

要在 C# 中访问 PDF DOM 对象,可以使用 IronPDF。下载 IronPDF 库,导入或渲染 PDF 文档,然后访问页面集合。从那里,您可以使用 ObjectModel 属性与各种 DOM 对象(如文本、图像和注释)进行交互。

我可以在 PDF DOM 中与哪些类型的对象交互?

在 PDF DOM 中,您可以与 ImageObjectPathObjectTextObject 等对象交互。这些对象允许您访问和修改诸如大小、颜色和内容等属性。

如何使用 C# 修改 PDF 中的文本内容?

您可以通过使用 IronPDF 在 PdfPageObjectModel 中访问 TextObject 来修改 PDF 中的文本内容。然后,您可以更改 ColorContents 等属性来更新文本。

PDF DOM 中 ImageObject 的一些常见属性是什么?

PDF DOM 中的 ImageObject 包含 HeightWidth 等属性和 ExportBytesAsJpg 等方法,允许您将图像导出为 JPG 格式的字节数组。

我可以更改 PDF 文档中路径的填充颜色吗?

是的,您可以通过在 PDF DOM 中使用 IronPDF 访问 PathObject 并修改 FillColor 属性来更改 PDF 文档中路径的填充颜色。

使用 IronPDF 访问 PDF DOM 完全稳定吗?

使用 IronPDF 访问 PDF DOM 目前是实验性功能,在访问文本对象时可能会导致内存泄漏,因此使用时应谨慎。

ObjectModel 在 IronPDF 中是什么?

IronPDF 中的 ObjectModelPdfPage 对象的一个属性,提供了对 PDF DOM 的访问权限,从而可以编程方式与 PDF 元素(如文本、图像和路径)进行交互。

如何将 PDF 中的图像导出为 JPEG 格式?

您可以通过使用 IronPDF 访问 PDF DOM 中的 ImageObject 并使用 ExportBytesAsJpg 方法将图像导出为 JPEG 格式的字节数组。

IronPDF 在处理 PDF DOM 访问时是否与 .NET 10 兼容?

是的——IronPDF 完全支持 .NET 10,包括通过ObjectModel访问 PDF DOM 等功能。它在 .NET 10 项目中开箱即用,与之前的版本一样,无需任何特殊设置。([ironpdf.com](https://ironpdf.com/blog/net-help/net-10-features/?utm_source=openai))

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布