iTextSharp.LGPLv2.Core icon indicating copy to clipboard operation
iTextSharp.LGPLv2.Core copied to clipboard

Is there a easy way to convert from iTextSharp Image to SKBitmap?

Open petarpetrovt opened this issue 2 years ago • 0 comments

I want to export images from PDF as PNGs.


using (var reader = new PdfReader(filePath))
{
	for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
	{
		// Create a new parser object to extract images from the page
		PdfDictionary pageDictionary = reader.GetPageN(pageNumber);
		PdfDictionary resourcesDictionary = pageDictionary.GetAsDict(PdfName.Resources);
		PdfDictionary xObjectDictionary = resourcesDictionary.GetAsDict(PdfName.Xobject);

		if (xObjectDictionary == null)
		{
			continue;
		}

		foreach (PdfName name in xObjectDictionary.Keys)
		{
			PdfObject obj = xObjectDictionary.Get(name);
			if (!obj.IsIndirect())
			{
				continue;
			}

			PdfDictionary imageDictionary = (PdfDictionary)PdfReader.GetPdfObject(obj);
			if (imageDictionary == null
				|| !imageDictionary.Get(PdfName.Subtype).Equals(PdfName.Image))
			{
				continue;
			}

			var image = Image.GetInstance((PrIndirectReference)obj);

			// TODO: convert to SKBitmap and save as PNG
		}
	}
}

petarpetrovt avatar Mar 27 '23 13:03 petarpetrovt