Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Dans ce chapitre, nous allons explorer comment utiliser la puissante classe GraphicsAbsorber
pour interagir avec des graphiques vectoriels dans des documents PDF. Que vous ayez besoin de déplacer, de supprimer ou d’ajouter des graphiques, ce guide vous montrera comment effectuer ces tâches efficacement.
Les graphiques vectoriels sont un élément crucial de nombreux documents PDF, utilisés pour représenter des images, des formes et d’autres éléments graphiques. Aspose.PDF fournit la classe GraphicsAbsorber
, qui permet aux développeurs d’accéder et de manipuler ces graphiques par programmation. En utilisant la méthode Visit
de GraphicsAbsorber
, vous pouvez extraire des graphiques vectoriels d’une page spécifiée et effectuer diverses opérations, telles que les déplacer, les supprimer ou les copier sur d’autres pages.
GraphicsAbsorber
La première étape pour travailler avec des graphiques vectoriels consiste à les extraire d’un document PDF. Voici comment vous pouvez le faire en utilisant la classe GraphicsAbsorber
:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void UsingGraphicsAbsorber()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open the document
using (var document = new Aspose.Pdf.Document(dataDir + "DocumentWithVectorGraphics.pdf"))
{
// Create an instance of GraphicsAbsorber
using (var graphicsAbsorber = new Aspose.Pdf.Vector.GraphicsAbsorber())
{
// Select the first page of the document
var page = document.Pages[1];
// Use the `Visit` method to extract graphics from the page
graphicsAbsorber.Visit(page);
// Step 5: Display information about the extracted elements
foreach (var element in graphicsAbsorber.Elements)
{
Console.WriteLine($"Page Number: {element.SourcePage.Number}");
Console.WriteLine($"Position: ({element.Position.X}, {element.Position.Y})");
Console.WriteLine($"Number of Operators: {element.Operators.Count}");
}
}
}
}
Document
est instancié avec le chemin vers le fichier PDF cible.GraphicsAbsorber
: Cette classe capture tous les éléments graphiques d’une page spécifiée.Visit
est appelée sur la première page, permettant à GraphicsAbsorber
d’absorber les graphiques vectoriels.Une fois que vous avez extrait les graphiques, vous pouvez les déplacer à une position différente sur la même page. Voici comment vous pouvez y parvenir :
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void MoveGraphics()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "DocumentWithVectorGraphics.pdf"))
{
// Create a GraphicsAbsorber instance
using (var graphicsAbsorber = new Aspose.Pdf.Vector.GraphicsAbsorber())
{
// Select the first page of the document
var page = document.Pages[1];
// Extract graphic elements from the page
graphicsAbsorber.Visit(page);
// Temporarily suspend updates to improve performance
graphicsAbsorber.SuppressUpdate();
// Loop through each extracted graphic element and shift its position
foreach (var element in graphicsAbsorber.Elements)
{
var position = element.Position;
// Move graphics by shifting its X and Y coordinates
element.Position = new Aspose.Pdf.Point(position.X + 150, position.Y - 10);
}
// Resume updates and apply changes
graphicsAbsorber.ResumeUpdate();
}
// Save PDF document
document.Save(dataDir + "DocumentWithVectorGraphics_out.pdf");
}
}
X
et Y
.Il existe des scénarios où vous pourriez vouloir supprimer des graphiques spécifiques d’une page. Aspose.PDF propose deux méthodes pour y parvenir :
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void RemoveGraphicsMethod1()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "DocumentWithVectorGraphics.pdf"))
{
// Create GraphicsAbsorber
using (var graphicsAbsorber = new Aspose.Pdf.Vector.GraphicsAbsorber())
{
// Get the first page of the document
var page = document.Pages[1];
// Extract graphic elements from the page
graphicsAbsorber.Visit(page);
// Define the rectangle where graphics will be removed
var rectangle = new Aspose.Pdf.Rectangle(70, 248, 170, 252);
// Temporarily suspend updates for better performance
graphicsAbsorber.SuppressUpdate();
// Iterate through the extracted graphic elements and remove elements inside the rectangle
foreach (var element in graphicsAbsorber.Elements)
{
// Check if the graphic's position falls within the rectangle
if (rectangle.Contains(element.Position))
{
// Remove the graphic element
element.Remove();
}
}
// Resume updates and apply changes
graphicsAbsorber.ResumeUpdate();
}
// Save PDF document
document.Save(dataDir + "DocumentWithVectorGraphics_out.pdf");
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void RemoveGraphicsMethod2()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "DocumentWithVectorGraphics.pdf"))
{
// Create GraphicsAbsorber
using (var graphicsAbsorber = new Aspose.Pdf.Vector.GraphicsAbsorber())
{
// Get the first page of the document
var page = document.Pages[1];
// Define the rectangle where graphics will be removed
var rectangle = new Aspose.Pdf.Rectangle(70, 248, 170, 252);
// Extract graphic elements from the page
graphicsAbsorber.Visit(page);
// Create a collection for the removed elements
var removedElementsCollection = new Aspose.Pdf.Vector.GraphicElementCollection();
// Add elements within the rectangle to the collection
foreach (var item in graphicsAbsorber.Elements.Where(el => rectangle.Contains(el.Position)))
{
removedElementsCollection.Add(item);
}
// Temporarily suppress updates for better performance
page.Contents.SuppressUpdate();
// Delete the selected graphic elements
page.DeleteGraphics(removedElementsCollection);
// Resume updates and apply changes
page.Contents.ResumeUpdate();
}
// Save PDF document
document.Save(dataDir + "DocumentWithVectorGraphics_out.pdf");
}
}
Les graphiques absorbés d’une page peuvent être ajoutés à une autre page dans le même document. Voici deux méthodes pour y parvenir :
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddToAnotherPageMethod1()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "DocumentWithVectorGraphics.pdf"))
{
// Create GraphicsAbsorber
using (var graphicsAbsorber = new Aspose.Pdf.Vector.GraphicsAbsorber())
{
// Get the first and second pages
var page1 = document.Pages[1];
var page2 = document.Pages[2];
// Extract graphic elements from the first page
graphicsAbsorber.Visit(page1);
// Temporarily suppress updates for better performance
page2.Contents.SuppressUpdate();
// Add each graphic element from the first page to the second page
foreach (var element in graphicsAbsorber.Elements)
{
element.AddOnPage(page2); // Add each graphic element to the second page.
}
// Resume updates and apply changes
page2.Contents.ResumeUpdate();
}
// Save PDF document
document.Save(dataDir + "DocumentWithVectorGraphics_out.pdf");
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddToAnotherPageMethod2()
{
// The path to the document directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "DocumentWithVectorGraphics.pdf"))
{
// Create GraphicsAbsorber
using (var graphicsAbsorber = new Aspose.Pdf.Vector.GraphicsAbsorber())
{
// Get the first and second pages
var page1 = document.Pages[1];
var page2 = document.Pages[2];
// Extract graphic elements from the first page
graphicsAbsorber.Visit(page1);
// Temporarily suppress updates for better performance
page2.Contents.SuppressUpdate();
// Add all graphics at once from the first page to the second page
page2.AddGraphics(graphicsAbsorber.Elements);
// Resume updates and apply changes
page2.Contents.ResumeUpdate();
}
// Save PDF document
document.Save(dataDir + "DocumentWithVectorGraphics_out.pdf");
}
}
AddOnPage
pour des ajouts individuels et AddGraphics
pour des ajouts en masse.Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.