Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
연산자는 페이지에 그래픽 모양을 그리는 것과 같은 특정 작업을 수행하도록 지정하는 PDF 키워드입니다. 연산자 키워드는 초기 슬래시 문자(2Fh)가 없기 때문에 명명된 객체와 구별됩니다. 연산자는 콘텐츠 스트림 내에서만 의미가 있습니다.
콘텐츠 스트림은 페이지에 그려질 그래픽 요소를 설명하는 명령으로 구성된 PDF 스트림 객체입니다. PDF 연산자에 대한 더 많은 세부정보는 PDF 사양에서 확인할 수 있습니다.
이 주제는 Aspose.PDF와 함께 연산자를 사용하는 방법을 설명합니다. 선택된 예제는 개념을 설명하기 위해 PDF 파일에 이미지를 추가합니다. PDF 파일에 이미지를 추가하려면 다양한 연산자가 필요합니다. 이 예제에서는 GSave, ConcatenateMatrix, Do, 및 GRestore 연산자를 사용합니다.
PDF 파일에 이미지를 추가하려면:
다음 코드 스니펫은 Aspose.PDF.Drawing 라이브러리와 함께 작동합니다.
다음 코드 스니펫은 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");
}
}
이 주제는 GSave/GRestore 연산자, ContatenateMatrix 연산자를 사용하여 xForm의 위치를 지정하고 Do 연산자를 사용하여 페이지에 xForm을 그리는 방법을 보여줍니다.
아래 코드는 PDF 파일의 기존 내용을 GSave/GRestore 연산자 쌍으로 감쌉니다. 이 접근 방식은 기존 내용의 끝에서 초기 그래픽 상태를 가져오는 데 도움이 됩니다. 이 접근 방식이 없으면 기존 연산자 체인의 끝에 원치 않는 변환이 남을 수 있습니다.
// 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");
}
}
연산자 클래스는 PDF 조작을 위한 훌륭한 기능을 제공합니다. PDF 파일에 PdfContentEditor 클래스의 DeleteImage 메서드를 사용하여 제거할 수 없는 그래픽이 포함된 경우, 대신 연산자 클래스를 사용하여 제거할 수 있습니다.
다음 코드 스니펫은 그래픽을 제거하는 방법을 보여줍니다. PDF 파일에 그래픽에 대한 텍스트 레이블이 포함되어 있는 경우, 이 접근 방식을 사용하면 PDF 파일에 남아 있을 수 있습니다. 따라서 이러한 이미지를 삭제하기 위한 대체 방법으로 그래픽 연산자를 검색하십시오.
// 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.