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#ですが、VB.NETなどの他の.NET言語を使用して同じことを達成できます。
次のコードスニペットは、Aspose.PDF.Drawingライブラリでも機能します。
ドキュメントを扱う際、通常はそれに含まれるページ数を知りたいと思います。Aspose.PDFを使用すると、これには2行のコードもかかりません。
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ファイルの作成中に、ファイルをシステムやストリームに保存せずにページ数を取得する必要が生じることがあります(目次の作成など)。この要件に対応するために、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.