Rank: Newbie
Groups: Registered
Joined: 7/14/2017(UTC) Posts: 3  Location: Montgomery, AL Thanks: 3 times
|
I use the following code (that I got from this forum) to add an image to a PDF. That works well. Now we have a situation where the user needs to remove the image from the PDF. How do I remove an image from a PDF? Code:
public static void AddImageToPage(PdfPage page, System.Drawing.Bitmap bmp, Point atPoint)
{
using(PdfImageObject signaturePDFImage = InsertImageToPage(page, bmp, atPoint))
{
InsertIntoDictionary(page, signaturePDFImage);
}
}
private static PdfImageObject InsertImageToPage(PdfPage page, System.Drawing.Bitmap bmp, Point atPoint)
{
var bi = bmp.LockBits(
new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//Create PdfBitmap object from .Net bitmap
var bitmap = new PdfBitmap(
bmp.Width,
bmp.Height,
BitmapFormats.FXDIB_Argb,
bi.Scan0,
bi.Stride);
//Create pdf image object and then set PdfBitmap object into it.
var image = PdfImageObject.Create(page.Document);
image.SetBitmap(bitmap);
//Scale image object to it's actual width and heihgt
image.SetMatrix(bmp.Width, 0, 0, bmp.Height, (float)atPoint.X, (float)atPoint.Y);
page.PageObjects.InsertObject(image);
return image;
}
private static void InsertIntoDictionary(PdfPage page, PdfImageObject image)
{
//Get page dictionary, list of indirect objects and original page content
var pageDict = page.Dictionary;
var list = PdfIndirectList.FromPdfDocument(page.Document);
//Convert contents to array.
PdfTypeArray array = ConvertContentsToArray(pageDict["Contents"], list, pageDict);
//Get stream of image.
IntPtr streamHandle = Pdfium.FPDFImageObj_GenerateStream(image.Handle, page.Handle);
var stream = PdfTypeStream.Create(streamHandle);
//Add image's stream into list of indirect objects and then add it to array.
int num = list.Add(stream);
array.AddIndirect(list, num);
}
public static PdfTypeArray ConvertContentsToArray(PdfTypeBase contents, PdfIndirectList list, PdfTypeDictionary pageDict)
{
//check the original content whether it's an array
if(contents is PdfTypeArray)
return contents as PdfTypeArray; //if contents is a array just return it
else if(contents is PdfTypeIndirect)
{
if((contents as PdfTypeIndirect).Direct is PdfTypeArray)
return (contents as PdfTypeIndirect).Direct as PdfTypeArray; //if contents is a reference to array then return that array
else if((contents as PdfTypeIndirect).Direct is PdfTypeStream)
{
//if contents is a reference to a stream then create a new array and insert stream as a first element of array
var array = PdfTypeArray.Create();
array.AddIndirect(list, (contents as PdfTypeIndirect).Direct);
//Add array into list of indirect objects
list.Add(array);
//And set it as a contents of the page
pageDict.SetIndirectAt("Contents", list, array);
return array;
}
else
throw new Exception("Unexpected content type");
}
else
throw new Exception("Unexpected content type");
}
|