I use PDFWriter library to generate a PDF file and add an image to it. Now I need to delete the image from the pdf file once the user click "Delete" button. Here is part of the code:
PDFWriter pdfWriter;
pdfWriter.ModifyPDF(inputFilePath, ePDFVersion13, "");
PDFParser& parser = pdfWriter.GetModifiedFileParser();
ObjectIDType imageID = pdfWriter.GetObjectsContext().GetInDirectObjectsRegistry().AllocateNewObjectID();
StoreInDatabase(imageID);
PDFModifiedPage modifiedPage(&pdfWriter, 0, true);
AbstractContentContext* pContentContext = modifiedPage.StartContentContext();
pContentContext->q();
pContentContext->cm(1, 0, 0, 1, imagePositionX, imagePositionY);
pContentContext->Do(modifiedPage.GetCurrentResourcesDictionary()->AddImageXObjectMapping(imageID));
pContentContext->Q();
modifiedPage.EndContentContext();
modifiedPage.WritePage();
// Create image xobject
PDFFormXObject* imageXObject = pdfWriter.CreateFormXObjectFromPNGFile(imagePath, imageID);
pdfWriter.EndPDF();
The above code works for adding the image to the pdf as a watermark. Now I need to delete the image from the pdf:
PDFWriter pdfWriter;
pdfWriter.ModifyPDF(strFilePath, ePDFVersion13, strFilePath);
PDFParser& parser = pdfWriter.GetModifiedFileParser();
PDFParser* pdfParser = new PDFParser(parser);
PDFModifiedPage modifiedPage(&pdfWriter, pageIndex, true);
AbstractContentContext* pContentContext = modifiedPage.StartContentContext();
ObjectIDType imageID = GetImageIdFromDatabase();
pdfWriter.GetObjectsContext().GetInDirectObjectsRegistry().DeleteObject(imageID);
modifiedPage.EndContentContext();
modifiedPage.WritePage();
pdfWriter.EndPDF();
However, after I run this code and open the pdf file, it displays the following dialog:
After I click OK, the pdf displayed correctly.
Is there any way that I can delete the added image without showing the error dialog ?
Here is the something about XObject: https://github.com/galkahana/PDF-Writer/wiki/Using-Form-XObjects
I tried with
pdfWriter.GetObjectsContext().StartNewIndirectObject(imageID);
pdfWriter.GetObjectsContext().EndIndirectObject();
But none of them can solve my issue.