private bool ShouldPdfOptimized(Document pdfDocument, string path) { var imagesTooBigCount = 0; foreach (Page pdfPage in pdfDocument.Pages) { // Only do the check if there is exactly one image on the page (scans) if (pdfPage?.Resources.Images.Count == 1) { XImage image = pdfPage.Resources.Images[1]; var stream = new MemoryStream(); image.Save(stream); var bitmap = Image.FromStream(stream); if (IsImageTooBig(bitmap, stream.Length, path)) { imagesTooBigCount++; // Check if we already skipped the threshold if (imagesTooBigCount * 100.0 / pdfDocument.Pages.Count > settings.AllowTooBigPercentage) { return(true); } } } } // if we come here, we don't enough images that are too big return(false); }
public static void Run() { // ExStart:ExtractImages // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Open document Document pdfDocument = new Document(dataDir + "ExtractImages.pdf"); // Extract a particular image XImage xImage = pdfDocument.Pages[1].Resources.Images[1]; FileStream outputImage = new FileStream(dataDir + "output.jpg", FileMode.Create); // Save output image xImage.Save(outputImage, ImageFormat.Jpeg); outputImage.Close(); dataDir = dataDir + "ExtractImages_out.pdf"; // Save updated PDF file pdfDocument.Save(dataDir); // ExEnd:ExtractImages Console.WriteLine("\nImages extracted successfully.\nFile saved at " + dataDir); }
private byte[] GetImageData(XImage img) { byte[] array; using (MemoryStream memoryStream = new MemoryStream()) { img.Save(memoryStream, ImageFormat.Jpeg); memoryStream.Position = 0; array = memoryStream.ToArray(); } return(array); }
void SaveToolStripMenuItem_Click(object sender, EventArgs e) { if (filename == "") { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "XImage|*.ximg|All Files|*.*"; if (sfd.ShowDialog() == DialogResult.OK) { filename = sfd.FileName; } } if (filename != "") { Image.Save(filename); } }
private static FigureElement ExtractToFigureElement(ITaggedContent taggedContent, Page page, int imageIndex) { ImagePlacementAbsorber imagePlacementAbsorber = new ImagePlacementAbsorber(); page.Accept(imagePlacementAbsorber); XImage xImage = imagePlacementAbsorber.ImagePlacements[imageIndex].Image; FileStream outputImage = new FileStream("temp-image.png", FileMode.Create); xImage.Save(outputImage, ImageFormat.Png); outputImage.Close(); FigureElement figureElement = taggedContent.CreateFigureElement(); figureElement.SetImage("temp-image.png"); figureElement.AlternativeText = "Aspose logo"; return(figureElement); }
public static void Main(string[] args) { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); //open document Document pdfDocument = new Document(dataDir + "input.pdf"); //extract a particular image XImage xImage = pdfDocument.Pages[1].Resources.Images[1]; FileStream outputImage = new FileStream(dataDir + "output.jpg", FileMode.Create); //save output image xImage.Save(outputImage, ImageFormat.Jpeg); outputImage.Close(); //save updated PDF file pdfDocument.Save(dataDir + "output.pdf"); }
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Images(); //open document Document pdfDocument = new Document(dataDir + "ExtractImages.pdf"); //extract a particular image XImage xImage = pdfDocument.Pages[1].Resources.Images[1]; FileStream outputImage = new FileStream(dataDir + "output.jpg", FileMode.Create); //save output image xImage.Save(outputImage, ImageFormat.Jpeg); outputImage.Close(); //save updated PDF file pdfDocument.Save(dataDir + "ExtractImages_out.pdf"); }