Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Un operador es una palabra clave PDF que especifica alguna acción que se debe realizar, como pintar una forma gráfica en la página. Una palabra clave de operador se distingue de un objeto nombrado por la ausencia de un carácter de barra sólida inicial (2Fh). Los operadores solo tienen significado dentro del flujo de contenido.
Un flujo de contenido es un objeto de flujo PDF cuyos datos consisten en instrucciones que describen los elementos gráficos que se deben pintar en una página. Se pueden encontrar más detalles sobre los operadores PDF en la especificación PDF.
Este tema explica cómo usar operadores con Aspose.PDF. El ejemplo seleccionado agrega una imagen a un archivo PDF para ilustrar el concepto. Para agregar una imagen en un archivo PDF, se necesitan diferentes operadores. Este ejemplo utiliza GSave, ConcatenateMatrix, Do y GRestore.
Para agregar una imagen a un archivo PDF:
El siguiente fragmento de código también funciona con la biblioteca Aspose.PDF.Drawing.
El siguiente fragmento de código muestra cómo usar operadores PDF.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddImageUsingPDFOperators()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "PDFOperators.pdf"))
{
// Set coordinates for the image placement
int lowerLeftX = 100;
int lowerLeftY = 100;
int upperRightX = 200;
int upperRightY = 200;
// Get the page where the image needs to be added
var page = document.Pages[1];
// Load the image into a file stream
using (var imageStream = new FileStream(dataDir + "PDFOperators.jpg", FileMode.Open))
{
// Add the image to the page's Resources collection
page.Resources.Images.Add(imageStream);
}
// Save the current graphics state using the GSave operator
page.Contents.Add(new Aspose.Pdf.Operators.GSave());
// Create a rectangle and matrix for positioning the image
var rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
var matrix = new Aspose.Pdf.Matrix(new double[]
{
rectangle.URX - rectangle.LLX, 0,
0, rectangle.URY - rectangle.LLY,
rectangle.LLX, rectangle.LLY
});
// Define how the image must be placed using the ConcatenateMatrix operator
page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
// Get the image from the Resources collection
var ximage = page.Resources.Images[page.Resources.Images.Count];
// Draw the image using the Do operator
page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
// Restore the graphics state using the GRestore operator
page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
// Save PDF document
document.Save(dataDir + "PDFOperators_out.pdf");
}
}
Este tema demuestra cómo usar los operadores GSave/GRestore, el operador ConcatenateMatrix para posicionar un xForm y el operador Do para dibujar un xForm en una página.
El código a continuación envuelve los contenidos existentes de un archivo PDF con el par de operadores GSave/GRestore. Este enfoque ayuda a obtener el estado gráfico inicial al final de los contenidos existentes. Sin este enfoque, transformaciones indeseables podrían permanecer al final de la cadena de operadores existente.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void DrawXFormOnPage()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "DrawXFormOnPage.pdf"))
{
var pageContents = document.Pages[1].Contents;
// Wrap existing contents with GSave/GRestore operators to preserve graphics state
pageContents.Insert(1, new Aspose.Pdf.Operators.GSave());
pageContents.Add(new Aspose.Pdf.Operators.GRestore());
// Add GSave operator to start new graphics state
pageContents.Add(new Aspose.Pdf.Operators.GSave());
// Create an XForm
var form = Aspose.Pdf.XForm.CreateNewForm(document.Pages[1], document);
document.Pages[1].Resources.Forms.Add(form);
form.Contents.Add(new Aspose.Pdf.Operators.GSave());
// Define image width and height
form.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(200, 0, 0, 200, 0, 0));
// Load image into stream
using (var imageStream = new FileStream(dataDir + "aspose-logo.jpg", FileMode.Open))
{
// Add the image to the XForm's resources
form.Resources.Images.Add(imageStream);
}
var ximage = form.Resources.Images[form.Resources.Images.Count];
// Draw the image on the XForm
form.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
form.Contents.Add(new Aspose.Pdf.Operators.GRestore());
// Place and draw the XForm at two different coordinates
// Draw the XForm at (100, 500)
pageContents.Add(new Aspose.Pdf.Operators.GSave());
pageContents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(1, 0, 0, 1, 100, 500));
pageContents.Add(new Aspose.Pdf.Operators.Do(form.Name));
pageContents.Add(new Aspose.Pdf.Operators.GRestore());
// Draw the XForm at (100, 300)
pageContents.Add(new Aspose.Pdf.Operators.GSave());
pageContents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(1, 0, 0, 1, 100, 300));
pageContents.Add(new Aspose.Pdf.Operators.Do(form.Name));
pageContents.Add(new Aspose.Pdf.Operators.GRestore());
// Restore graphics state
pageContents.Add(new Aspose.Pdf.Operators.GRestore());
// Save PDF document
document.Save(dataDir + "DrawXFormOnPage_out.pdf");
}
}
Las clases de operadores proporcionan grandes características para la manipulación de PDF. Cuando un archivo PDF contiene gráficos que no se pueden eliminar utilizando el método DeleteImage de la clase PdfContentEditor, se pueden usar las clases de operadores para eliminarlos en su lugar.
El siguiente fragmento de código muestra cómo eliminar gráficos. Tenga en cuenta que si el archivo PDF contiene etiquetas de texto para los gráficos, pueden persistir en el archivo PDF utilizando este enfoque. Por lo tanto, busque los operadores gráficos para un método alternativo para eliminar tales imágenes.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void RemoveGraphicsObjects()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "RemoveGraphicsObjects.pdf"))
{
// Get the specific page (page 2 in this case)
var page = document.Pages[2];
// Get the operator collection from the page contents
var oc = page.Contents;
// Define the path-painting operators to be removed
var operators = new Aspose.Pdf.Operator[]
{
new Aspose.Pdf.Operators.Stroke(),
new Aspose.Pdf.Operators.ClosePathStroke(),
new Aspose.Pdf.Operators.Fill()
};
// Delete the specified operators from the page contents
oc.Delete(operators);
// Save PDF document
document.Save(dataDir + "NoGraphics_out.pdf");
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.