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是文件內部結構的表示,允許開發人員程式化地存取和操作不同的元素,如文字、圖像、註釋和元數據。

as-heading:2 (快速入門:使用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

翻譯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

縮放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

移除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目前是實驗性功能,可能在存取文字對象時導致記憶體洩漏,因此應謹慎使用。

IronPDF中的ObjectModel是什麼?

IronPDF中的ObjectModelPdfPage對象的屬性,提供對PDF DOM的存取,允許程序化地與PDF元素如文字、圖像和路徑互動。

我如何將PDF中的圖像匯出到JPEG格式?

您可以使用IronPDF存取PDF DOM中的ImageObject,然後使用ExportBytesAsJpg方法將圖像匯出為JPEG格式的字節數組。

Chaknith Bin
軟體工程師
Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。
準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布