Add layer to document

Hello,

I am trying to add a layer in a document, which is already loaded in a PdfDocumentView, but the UI is not upaded. I have to unlaod the document and then load it again. Is there anything I am missing that can be done in order to refresh the document without the need to reload it. Here is a snippet of the code I am using:

           //glfullpath path to the loaded document

            document = new PdfLoadedDocument(glfullpath);

            var page = document.Pages[pageIndex-1];


            PdfPageLayer layer = page.Layers.Cast<PdfPageLayer>().FirstOrDefault(x => x.Name == "test");


            if (layer == null)

            {

                layer = page.Layers.Add("test", true);

            }


            layer.Graphics.SetTransparency(0.5f, 0.5f, PdfBlendMode.Multiply);

            layer.Graphics.DrawRectangle(new PdfSolidBrush(new PdfColor(color.R, color.G, color.B)), rectangleF);


            document.Save();

            //LeftDocument is the  PdfDocumentView

            LeftDocument.Unload(true);

            LeftDocument.Load(glfullpath);

I also tried to add the layer directly to LeftDocument.LoadedDocument, but the result was the same.

Thanks in advance.

Looking forward for your reply.

Regards,

Stefan


1 Reply 1 reply marked as answer

MS Manigandan Shanmugam Syncfusion Team June 26, 2023 01:51 PM UTC

Stefan, In PdfViewerControl While loading a document, the loaded document was maintained as a Syncfusion.Pdf.Base.PdfLoadedDocument instance. With this instance we can get information about the loaded PDF document.

In order to refresh the document, we need to save loadedDocument and reload it in PdfViewer. In the reload operation, the unloading will handle internally in loading, so no need unload the document explicitly. When we load the document, it unloads the existing document.

As per your requirement of adding layers to documents, we have created a sample that loads a document using pdfLoadedDocument, adds the layer, saves that document, and loads that saved document.

Please find the attached sample for your reference.



Attachment: sample_c9230b5b.zip

Marked as answer
Loader.
Up arrow icon