Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.PDF for .NET 让您在 .NET 应用程序中读取和设置 PDF 文件中页面的属性。本节展示了如何获取 PDF 文件中的页面数量,获取有关 PDF 页面属性的信息,例如颜色,并设置页面属性。给出的示例是 C# 语言,但您可以使用任何 .NET 语言,例如 VB.NET 来实现相同的功能。
以下代码片段也适用于 Aspose.PDF.Drawing 库。
在处理文档时,您通常想知道它们包含多少页。使用 Aspose.PDF,这只需两行代码。
要获取 PDF 文件中的页面数量:
以下代码片段展示了如何获取 PDF 文件的页面数量。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetNumberOfPagesInAPdfFile()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "GetNumberofPages.pdf"))
{
// Get page count
System.Console.WriteLine("Page Count : {0}", document.Pages.Count);
}
}
有时我们会动态生成 PDF 文件,在创建 PDF 文件时,可能会遇到需求(创建目录等)需要在不将文件保存到系统或流中的情况下获取 PDF 文件的页面计数。因此,为了满足这一需求,在 Document 类中引入了一个方法 ProcessParagraphs。请查看以下代码片段,展示了如何在不保存文档的情况下获取页面计数的步骤。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetPageCountWithoutSavingTheDocument()
{
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Create loop instance
for (var i = 0; i < 300; i++)
{
// Add TextFragment to paragraphs collection of page object
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Pages count test"));
}
// Process the paragraphs in PDF file to get accurate page count
document.ProcessParagraphs();
// Print number of pages in document
Console.WriteLine("Number of pages in document = " + document.Pages.Count);
}
}
PDF 文件中的每个页面都有多个属性,例如宽度、高度、出血框、裁剪框和修剪框。Aspose.PDF 允许您访问这些属性。
有关更多详细信息,请访问 此页面。
Page 类提供与特定 PDF 页面相关的所有属性。PDF 文件的所有页面都包含在 Document 对象的 PageCollection 集合中。
从那里,可以使用索引访问单个 Page 对象,或使用 foreach 循环遍历集合以获取所有页面。一旦访问了单个页面,我们可以获取其属性。以下代码片段展示了如何获取页面属性。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AccessingPageProperties()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "GetProperties.pdf"))
{
// Get page collection
var pageCollection = document.Pages;
// Get particular page
var pdfPage = pageCollection[1];
// Get page properties
System.Console.WriteLine("ArtBox : Height={0},Width={1},LLX={2},LLY={3},URX={4},URY={5}", pdfPage.ArtBox.Height, pdfPage.ArtBox.Width, pdfPage.ArtBox.LLX,
pdfPage.ArtBox.LLY, pdfPage.ArtBox.URX, pdfPage.ArtBox.URY);
System.Console.WriteLine("BleedBox : Height={0},Width={1},LLX={2},LLY={3},URX={4},URY={5}", pdfPage.BleedBox.Height, pdfPage.BleedBox.Width, pdfPage.BleedBox.LLX,
pdfPage.BleedBox.LLY, pdfPage.BleedBox.URX, pdfPage.BleedBox.URY);
System.Console.WriteLine("CropBox : Height={0},Width={1},LLX={2},LLY={3},URX={4},URY={5}", pdfPage.CropBox.Height, pdfPage.CropBox.Width, pdfPage.CropBox.LLX,
pdfPage.CropBox.LLY, pdfPage.CropBox.URX, pdfPage.CropBox.URY);
System.Console.WriteLine("MediaBox : Height={0},Width={1},LLX={2},LLY={3},URX={4},URY={5}", pdfPage.MediaBox.Height, pdfPage.MediaBox.Width, pdfPage.MediaBox.LLX,
pdfPage.MediaBox.LLY, pdfPage.MediaBox.URX, pdfPage.MediaBox.URY);
System.Console.WriteLine("TrimBox : Height={0},Width={1},LLX={2},LLY={3},URX={4},URY={5}", pdfPage.TrimBox.Height, pdfPage.TrimBox.Width, pdfPage.TrimBox.LLX,
pdfPage.TrimBox.LLY, pdfPage.TrimBox.URX, pdfPage.TrimBox.URY);
System.Console.WriteLine("Rect : Height={0},Width={1},LLX={2},LLY={3},URX={4},URY={5}", pdfPage.Rect.Height, pdfPage.Rect.Width, pdfPage.Rect.LLX, pdfPage.Rect.LLY,
pdfPage.Rect.URX, pdfPage.Rect.URY);
System.Console.WriteLine("Page Number : {0}", pdfPage.Number);
System.Console.WriteLine("Rotate : {0}", pdfPage.Rotate);
}
}
Aspose.PDF 允许您 将 PDF 拆分为单个页面 并将其保存为 PDF 文件。获取 PDF 文件中的指定页面并将其保存为新 PDF 是一个非常相似的操作:打开源文档,访问页面,创建新文档并将页面添加到此文档中。
Document 对象的 PageCollection 保存 PDF 文件中的页面。要从此集合中获取特定页面:
以下代码片段展示了如何从 PDF 文件中获取特定页面并将其保存为新文件。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetAParticularPageOfThePdfFile()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
// Get particular page
var pdfPage = document.Pages[2];
// Save the page as PDF file
using (var newDocument = new Aspose.Pdf.Document())
{
newDocument.Pages.Add(pdfPage);
// Save PDF document
newDocument.Save(dataDir + "GetParticularPage_out.pdf");
}
}
}
Page 类提供与 PDF 文档中特定页面相关的属性,包括页面使用的颜色类型 - RGB、黑白、灰度或未定义。
PDF 文件的所有页面都包含在 PageCollection 集合中。ColorType 属性指定页面上元素的颜色。要获取或确定特定 PDF 页面的颜色信息,请使用 Page 对象的 ColorType 属性。
以下代码片段展示了如何遍历 PDF 文件的每个页面以获取颜色信息。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void DeterminePageColor()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
// Iterate through all the page of PDF file
for (var pageCount = 1; pageCount <= document.Pages.Count; pageCount++)
{
// Get the color type information for particular PDF page
Aspose.Pdf.ColorType pageColorType = document.Pages[pageCount].ColorType;
switch (pageColorType)
{
case Aspose.Pdf.ColorType.BlackAndWhite:
Console.WriteLine("Page # -" + pageCount + " is Black and white..");
break;
case Aspose.Pdf.ColorType.Grayscale:
Console.WriteLine("Page # -" + pageCount + " is Gray Scale...");
break;
case Aspose.Pdf.ColorType.Rgb:
Console.WriteLine("Page # -" + pageCount + " is RGB..", pageCount);
break;
case Aspose.Pdf.ColorType.Undefined:
Console.WriteLine("Page # -" + pageCount + " Color is undefined..");
break;
}
}
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.