Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Cada página PDF contém propriedades de Recursos e Conteúdos. Recursos podem ser imagens e formulários, por exemplo, enquanto o conteúdo é representado por um conjunto de operadores PDF. Cada operador tem seu nome e argumento. Este exemplo usa operadores para adicionar uma imagem a um arquivo PDF.
O seguinte trecho de código também funciona com a biblioteca Aspose.PDF.Drawing.
Para adicionar uma imagem a um arquivo PDF existente:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddImageToPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "AddImage.pdf"))
{
// Set coordinates for the image placement
int lowerLeftX = 100;
int lowerLeftY = 100;
int upperRightX = 200;
int upperRightY = 200;
// Get the page where image needs to be added
var page = document.Pages[1];
// Load image into stream
using (var imageStream = new FileStream(dataDir + "AddImage.jpg", FileMode.Open))
{
// Add image to Images collection of Page Resources
page.Resources.Images.Add(imageStream);
// Using GSave operator: this operator saves the current graphics state
page.Contents.Add(new Aspose.Pdf.Operators.GSave());
// Create Rectangle and Matrix objects to define image positioning
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 });
// Using ConcatenateMatrix operator: defines how the image must be placed
page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
// Retrieve the added image and use Do operator to draw it
var ximage = page.Resources.Images[page.Resources.Images.Count];
page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
// Using GRestore operator: this operator restores the graphics state
page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
}
// Save PDF document
document.Save(dataDir + "AddImage_out.pdf");
}
}
Há também uma maneira alternativa e mais fácil de adicionar uma Imagem a um arquivo PDF. Você pode usar o método AddImage da classe PdfFileMend. O método AddImage requer a imagem a ser adicionada, o número da página na qual a imagem precisa ser adicionada e as informações de coordenadas. Depois disso, salve o arquivo PDF atualizado usando o método Close. O seguinte trecho de código mostra como adicionar uma imagem em um arquivo PDF existente.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddImageToPDFUsingPdfFileMender()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Define image file and output PDF file paths
var imageFileName = Path.Combine(dataDir, "Images", "Sample-01.jpg");
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add first page with specified size
var page = document.Pages.Add();
page.SetPageSize(Aspose.Pdf.PageSize.A3.Height, Aspose.Pdf.PageSize.A3.Width);
// Add second page
page = document.Pages.Add();
// Create PdfFileMend object
var mender = new Aspose.Pdf.Facades.PdfFileMend(document);
// Add image to the first page using the mender
mender.AddImage(imageFileName, 1, 0, 0, (float)page.CropBox.Width, (float)page.CropBox.Height);
// Save PDF document
document.Save(dataDir + "AddImageMender_out.pdf");
}
}
Às vezes, é necessário recortar uma imagem antes de inseri-la em um PDF. Você pode usar o método AddImage()
para suportar a adição de imagens recortadas:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddCroppedImageToPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Define image file and output PDF file paths
var imageFileName = Path.Combine(dataDir, "Images", "Sample-01.jpg");
var outputPdfFileName = dataDir + "Example-add-image-mender.pdf";
// Open PDF document
using (var document = new Aspose.Pdf.Document())
{
// Open image stream
using (var imgStream = File.OpenRead(imageFileName))
{
// Define the rectangle where the image will be placed on the PDF page
var imageRect = new Aspose.Pdf.Rectangle(17.62, 65.25, 602.62, 767.25);
// Crop the image to half its original width and height
var w = imageRect.Width / 2;
var h = imageRect.Height / 2;
var bbox = new Aspose.Pdf.Rectangle(imageRect.LLX, imageRect.LLY, imageRect.LLX + w, imageRect.LLY + h);
// Add page
var page = document.Pages.Add();
// Insert the cropped image onto the page, specifying the original position (imageRect)
// and the cropping area (bbox)
page.AddImage(imgStream, imageRect, bbox);
}
// Save PDF document to the specified file path
document.Save(dataDir + "AddCroppedImageMender_out.pdf");
}
}
Se não soubermos as dimensões da imagem, há uma grande chance de obter uma imagem distorcida na página. O seguinte exemplo mostra uma das maneiras de evitar isso.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddingImageAndPreserveAspectRatioIntoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Load the image
using (var bitmap = System.Drawing.Image.FromFile(dataDir + "InputImage.jpg"))
{
// Get the original width and height of the image
int width = bitmap.Width;
int height = bitmap.Height;
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Define the scaled width and height while preserving the aspect ratio
int scaledWidth = 400;
int scaledHeight = scaledWidth * height / width;
// Add the image to the page
page.AddImage(dataDir + "InputImage.jpg", new Aspose.Pdf.Rectangle(10, 10, scaledWidth, scaledHeight));
// Save PDF document
document.Save(dataDir + "PreserveAspectRatio.pdf");
}
}
}
Diferentes tipos de compressão podem ser aplicados sobre imagens para reduzir seu tamanho. O tipo de compressão aplicada à imagem depende do ColorSpace da imagem de origem, ou seja, se a imagem é Colorida (RGB), então aplique compressão JPEG2000, e se for Preto & Branco, então a compressão JBIG2/JBIG2000 deve ser aplicada. Portanto, identificar cada tipo de imagem e usar um tipo apropriado de compressão criará a melhor/otimizada saída.
Um arquivo PDF pode conter elementos como Texto, Imagem, Gráfico, Anexo, Anotação etc., e se o arquivo PDF de origem contiver imagens, podemos determinar o espaço de cor da imagem e aplicar a compressão apropriada para reduzir o tamanho do arquivo PDF. O seguinte trecho de código mostra os passos para identificar se a imagem dentro do PDF é Colorida ou Preto & Branco.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractImageTypesFromPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Counters for grayscale and RGB images
int grayscaled = 0;
int rgb = 0;
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "ExtractImages.pdf"))
{
// Iterate through all pages in the document
foreach (Aspose.Pdf.Page page in document.Pages)
{
Console.WriteLine("--------------------------------");
var abs = new Aspose.Pdf.ImagePlacementAbsorber();
page.Accept(abs);
// Get the count of images on the current page
Console.WriteLine("Total Images = {0} on page number {1}", abs.ImagePlacements.Count, page.Number);
// Iterate through all the image placements on the page
int image_counter = 1;
foreach (Aspose.Pdf.ImagePlacement ia in abs.ImagePlacements)
{
// Determine the color type of the image
var colorType = ia.Image.GetColorType();
switch (colorType)
{
case Aspose.Pdf.ColorType.Grayscale:
++grayscaled;
Console.WriteLine("Image {0} is Grayscale...", image_counter);
break;
case Aspose.Pdf.ColorType.Rgb:
++rgb;
Console.WriteLine("Image {0} is RGB...", image_counter);
break;
}
image_counter += 1;
}
}
}
}
É possível controlar a qualidade de uma imagem que está sendo adicionada a um arquivo PDF. Use o método sobrecarregado Replace na classe XImageCollection.
O seguinte trecho de código demonstra como converter todas as imagens do documento em JPEGs que usam 80% de qualidade para compressão.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ReplaceImagesInPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "ReplaceImages.pdf"))
{
// Iterate through all pages in the document
foreach (Aspose.Pdf.Page page in document.Pages)
{
int idx = 1;
// Iterate through all images in the page's resources
foreach (Aspose.Pdf.XImage image in page.Resources.Images)
{
using (var imageStream = new MemoryStream())
{
// Save the image as JPEG with 80% quality
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
// Replace the image in the page's resources
page.Resources.Images.Replace(idx, imageStream, 80);
idx = idx + 1;
}
}
}
// Save PDF document
document.Save(dataDir + "ReplaceImages_out.pdf");
}
}
Colocar uma forma vetorial sobre a imagem bitmap base funciona como uma máscara, expondo apenas a parte do design base que se alinha com a forma vetorial. Todas as áreas fora da forma serão ocultadas.
O trecho de código carrega um PDF, abre dois arquivos de imagem e aplica essas imagens como máscaras de estêncil às duas primeiras imagens na primeira página do PDF.
A máscara de estêncil pode ser adicionada pelo método ‘XImage.AddStencilMask(Stream maskStream)’:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddStencilMasksToImages()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "AddStencilMasksToImages.pdf"))
{
// Open the first mask image file
using (var fs1 = new FileStream(dataDir + "mask1.jpg", FileMode.Open))
{
// Open the second mask image file
using (var fs2 = new FileStream(dataDir + "mask2.png", FileMode.Open))
{
// Apply stencil mask to the first image on the first page
document.Pages[1].Resources.Images[1].AddStencilMask(fs1);
// Apply stencil mask to the second image on the first page
document.Pages[1].Resources.Images[2].AddStencilMask(fs2);
}
}
// Save PDF document
document.Save(dataDir + "AddStencilMasksToImages_out.pdf");
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.