如何在 C# | IronPDF 中使用 IronPDF 存取所有 PDF DOM 物件

如何在C#中訪問所有PDF DOM對象

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

要在C#中訪問PDF DOM對象,使用IronPDF的 ObjectModel 屬性,該屬性提供對PDF文件中文本、圖像和路徑對象的程式化訪問,允許您直接讀取、修改、翻譯、縮放和刪除元素。

快速入門:使用IronPDF訪問和更新PDF DOM元素

開始使用IronPDF的DOM訪問功能來操作PDF文件。 此指南展示如何訪問PDF DOM、選擇頁面以及修改文本對象。 載入您的PDF,訪問所需頁面,並用幾行代碼更新內容。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 複製並運行這段程式碼。

    var objs = IronPdf.ChromePdfRenderer.RenderUrlAsPdf("https://example.com").Pages.First().ObjectModel;
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronPDF

    arrow pointer

如何在PDF中訪問DOM對象?

ObjectModel 是從 PdfPage 對象中訪問的。 首先,匯入目標PDF並訪問其 Pages 屬性。 從那裡,選擇任意頁面以訪問 ObjectModel 屬性。 這使得可以程式化地與PDF內容進行互動,類似於操作HTML DOM元素。

處理PDF DOM對象時,您可以訪問PDF文件的底層結構。 這包括構成PDF視覺表示的文本元素、圖像、向量圖形(paths)及其它內容。 IronPDF提供一種面向對象的方法來操作與C#應用程式整合的PDF。

: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;
$vbLabelText   $csharpLabel
IronPDF調試器顯示具有邊界框座標和變換屬性的TextObjects集合

ObjectModel 屬性包含 PathObjectTextObject。 每個對象都包含其頁面索引、邊界框、縮放和翻譯的信息。 這些信息可以修改。 對於渲染選項,您可以自定義這些對象的顯示方式。 使用自定義邊距時,理解對象定位是很重要的。

<ImageObject>:

  • Height:圖像的高度
  • Width:圖像的寬度
  • ExportBytesAsJpg:將圖像導出為JPG字節數組的方法

<PathObject>:

  • FillColor:路徑的填充顏色
  • StrokeColor:路徑的描邊顏色
  • Points:定義路徑的點集合

<TextObject>:

  • Color:文本的顏色
  • Contents:實際的文本內容

每種類型的對象都提供針對其內容類型量身定制的方法和屬性。 當您需要提取文本和圖像或修改特定內容時,這些對象提供了詳細的控制。 這在PDF表單中進行程式化操作表單字段時非常有用。

如何檢索字形信息和邊界框?

指定精確的字形和自定義字體時,檢索邊界框和字形信息至關重要。 IronPDF提供這些信息以實現光栅精確的定位,當在現有的PDF上繪製文本和位圖時使用。

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();
$vbLabelText   $csharpLabel
調試器顯示PDF字形對象屬性,包括座標、邊界和文本內容細節

字形信息包括定位數據、字體度量和字符特定細節,以便於高級PDF操作。 這使得創建處理複雜排版和佈局要求的PDF處理應用程式成為可能。 使用自定義字體時,這種字形級別的訪問確保在各系統上準確渲染。


如何翻譯PDF對象?

通過重新定位元素如文本或圖像,調整PDF佈局。 通過更改其 Translate 屬性移動對象。 此功能是IronPDF的PDF轉換能力的一部分。

下面的例子使用CSS Flexbox居中文本文本渲染HTML。 它訪問第一個 TextObject 並通過給 Translate 屬性分配新的 PointF 來翻譯它。 這使文本向右移動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");
$vbLabelText   $csharpLabel

翻譯結果的樣子如何?

輸出顯示"Centered"從其原始位置向右移動200點,向上移動150點。

PDF翻譯前後的對比顯示已保留的文本位置和格式

翻譯操作保持對象的原始屬性,例如字體、大小和顏色,而僅改變位置。 這非常適合進行佈局調整而不影響視覺外觀。 此功能在重新定位動態生成內容時,與頁眉和頁腳一起使用效果良好。


如何縮放PDF對象?

使用 Scale 屬性調整PDF對象大小。 此屬性作為一個乘數。 大於1的值會增加大小,而介於0和1之間的值會減小大小。 縮放對於動態佈局和調整內容以適應頁面尺寸至關重要。 參考縮放PDF對象指南了解更多示例。

該例演示包含圖像的HTML渲染。 它訪問第一個 ImageObject 並通過賦予 PointF 新的 Scale 以0.7的比例縮放到70%。

擴展PDF對象使用什麼代碼?

:path=/static-assets/pdf/content-code-examples/how-to/access-pdf-dom-object-scale.cs
using IronPdf;
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 System.Drawing.PointF(0.7f, 0.7f);

// Save the modified PDF to see the result
pdf.SaveAs("AfterScale.pdf");
$vbLabelText   $csharpLabel

分別對X和Y軸應用不同的縮放因子以進行非均勻縮放。 這對於將內容調整到特定尺寸很有用。 使用自定義紙張尺寸時,縮放有助於確保內容適合頁面邊界。

實際操作中縮放的樣子如何?

輸出顯示圖像縮放到其原始大小的70%。

PDF縮放示例:IRON標誌從大(左)縮放到小(右),箭頭顯示過程

如何刪除PDF對象?

通過訪問PDF DOM集合如 ImageObjectsTextObjects 刪除對象。 在集合上調用 RemoveAt,傳遞要刪除的對象索引。 這對於刪除內容或簡化文件很有用。 請參閱刪除PDF對象示例了解更多。

該代碼加載BeforeScale.pdf並從第一页刪除第一個圖像。

移除對象應使用何種代碼?

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

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

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

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

// Save the modified PDF
pdf.SaveAs("removedFirstImage.pdf");
$vbLabelText   $csharpLabel

當我刪除多個對象時會發生什麼?

刪除後其餘對象的索引會發生變化。 刪除多個對象時,應倒序刪除以保持正確的索引。 這技巧有助於從敏感文件中刪除文本

如何結合多個DOM操作?

IronPDF的DOM訪問使得高級文件處理工作流程成為可能。 結合操作以實現複雜的轉換:

何時應該使用結合操作?

// Example of combining multiple DOM operations
using IronPdf;
using System.Linq;

PdfDocument pdf = PdfDocument.FromFile("complex-document.pdf");

// Iterate through all pages
foreach (var page in pdf.Pages)
{
    var objects = page.ObjectModel;

    // Process text objects
    foreach (var textObj in objects.TextObjects)
    {
        // Change color of specific text
        if (textObj.Contents.Contains("Important"))
        {
            textObj.Color = System.Drawing.Color.Red;
        }
    }

    // Scale down all images by 50%
    foreach (var imgObj in objects.ImageObjects)
    {
        imgObj.Scale = new System.Drawing.PointF(0.5f, 0.5f);
    }
}

pdf.SaveAs("processed-document.pdf");
// Example of combining multiple DOM operations
using IronPdf;
using System.Linq;

PdfDocument pdf = PdfDocument.FromFile("complex-document.pdf");

// Iterate through all pages
foreach (var page in pdf.Pages)
{
    var objects = page.ObjectModel;

    // Process text objects
    foreach (var textObj in objects.TextObjects)
    {
        // Change color of specific text
        if (textObj.Contents.Contains("Important"))
        {
            textObj.Color = System.Drawing.Color.Red;
        }
    }

    // Scale down all images by 50%
    foreach (var imgObj in objects.ImageObjects)
    {
        imgObj.Scale = new System.Drawing.PointF(0.5f, 0.5f);
    }
}

pdf.SaveAs("processed-document.pdf");
$vbLabelText   $csharpLabel

結合操作的常見用例是什麼?

結合DOM操作適用於:

  1. 批量文件處理:處理文件以標準化格式或刪除敏感內容
  2. 動態報告生成:修改模板PDF以實時數據並控制佈局
  3. 內容遷移:提取並重新組織PDF中的內容到新佈局
  4. 可及性提升:通過修改字體大小、對比度或間距來提升文件

這些技術使得強大的PDF處理應用程序能夠處理複雜的修改。 有關文件屬性管理的更多信息,請參閱元數據管理指南

DOM訪問與其他PDF操作方法如何比較?

使用PDF DOM比傳統方法具優勢:

// Example: Selective content modification based on criteria
using IronPdf;
using System.Linq;

PdfDocument report = PdfDocument.FromFile("quarterly-report.pdf");

foreach (var page in report.Pages)
{
    var textObjects = page.ObjectModel.TextObjects;

    // Highlight negative values in financial reports
    foreach (var text in textObjects)
    {
        if (text.Contents.StartsWith("-$") || text.Contents.Contains("Loss"))
        {
            text.Color = System.Drawing.Color.Red;
        }
    }
}

report.SaveAs("highlighted-report.pdf");
// Example: Selective content modification based on criteria
using IronPdf;
using System.Linq;

PdfDocument report = PdfDocument.FromFile("quarterly-report.pdf");

foreach (var page in report.Pages)
{
    var textObjects = page.ObjectModel.TextObjects;

    // Highlight negative values in financial reports
    foreach (var text in textObjects)
    {
        if (text.Contents.StartsWith("-$") || text.Contents.Contains("Loss"))
        {
            text.Color = System.Drawing.Color.Red;
        }
    }
}

report.SaveAs("highlighted-report.pdf");
$vbLabelText   $csharpLabel

此詳細控制無法僅通過HTML轉換為PDF實現,因此DOM訪問對於高級PDF加工至關重要。

準備好看看您還能做什麼了嗎? 檢視教程頁面:編輯PDF

常見問題解答

在 PDF 操作中,ObjectModel 屬性是用來做什麼的?

IronPDF 中的 ObjectModel 屬性提供對 PDF 文件中的文字、影像和路徑物件的程式化存取。它允許開發人員直接從 PDF DOM 讀取、修改、翻譯、縮放和移除元素,就像處理 HTML DOM 元素一樣。

如何在 C# 中存取 PDF DOM 物件?

要使用 IronPDF 存取 PDF DOM 物件,首先匯入您的目標 PDF 文件,然後存取其 Pages 屬性。從那裡,選擇任何頁面,並使用 ObjectModel 屬性。例如: var objs = IronPdf.ChromePdfRenderer.RenderUrlAsPdf("https://example.com").Pages.First().ObjectModel;

我可以透過 PDF DOM 存取哪些類型的物件?

IronPDF 的 ObjectModel 包含三種主要物件類型:ImageObject(具有高度、寬度和 ExportBytesAsJpg 等屬性)、PathObject(具有 FillColor、StrokeColor 和 Points 等屬性)和 TextObject(具有 Color 和 Contents 等屬性)。每種物件都提供針對其特定內容類型的方法。

我可以程式化地修改 PDF 文件內的文字內容嗎?

是的,IronPDF 允許您透過 TextObject 的 Contents 屬性修改文字內容。您只需要幾行程式碼就可以透過 ObjectModel 存取文字物件、更新其內容,並儲存修改後的 PDF 文件。

如何從 PDF 文件匯出影像?

IronPDF 的 ImageObject 提供 ExportBytesAsJpg 方法,可以將圖像匯出為 JPG 位元組陣列。透過 ObjectModel 屬性存取影像,並使用此方法以程式化的方式擷取影像資料。

每個 DOM 物件的位置有哪些資訊?

IronPDF ObjectModel 中的每個物件都包含其頁面索引、邊界框座標、縮放比例和平移等資訊。此定位資料可被讀取與修改,以重新定位或轉換 PDF 中的元素。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 17,803,474 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速取得證據? PM > Install-Package IronPdf
運行範例看著你的HTML程式碼變成PDF檔。