0

C# iText7 How to delete pdfcanvas after adding Image

PdfCanvas canvas = new PdfCanvas(document.GetPage(startPage));
PdfXObject pdfObject=canvas.AddImage(imageData, x, y, imgHeight, false, false);   
mkl
  • 90,588
  • 15
  • 125
  • 265
  • 1
    What do you mean by *"delete pdfcanvas"*? Do you want to undo the adding of the image? Or do you merely mean something like _closing_? – mkl Jun 23 '22 at 13:55
  • Yes, undo added image,What should I do? – fadaidechengxuyuan Jun 24 '22 at 15:07
  • You need to parse the page content stream, identify the instructions drawing your image, and remove them. `PdfCanvas` is not really a _container_ of the data you add to it, it merely knows where to insert it in the PDF under construction. – mkl Jun 24 '22 at 18:20
  • How to parse the content stream? I haven't found any examples in the official examples. I searched a lot of information and couldn't find them. Maybe my search method is wrong. Can you provide some code examples? – fadaidechengxuyuan Jun 26 '22 at 03:48
  • For a quick look at the page content stream contents simply use the `PdfPage` method `GetContentBytes()`. To parse it instruction by instruction while keeping track of the current graphics state, use the `...parser` namespace, in particular the `PdfCanvasProcessor`. To additionally write back a manipulated version of the content stream, consider porting the `PdfCanvasEditor` from [this answer](https://stackoverflow.com/a/40999180/1729265) from Java to C# and extending it accordingly. – mkl Jun 26 '22 at 06:08

0 Answers0