How to remove Metadata from an exist PDF file?

Hi,

How can I remove metadata from an exist PDF file?

According to the link, I tried to set the metadata properties to default. 

However, I got the PdfViewer error when tried to reload or save the file.


              using var pdfDoc = new PdfLoadedDocument(stream);
              XmpMetadata xmpMetadata = pdfDoc.DocumentInformation.XmpMetadata;


              // Set PDFSchema
              xmpMetadata.PDFSchema.Producer = string.Empty;
              xmpMetadata.PDFSchema.PDFVersion = string.Empty;
              xmpMetadata.PDFSchema.Keywords = string.Empty;


              //XMP Basic Schema
              var basic = xmpMetadata.BasicSchema;
              //Set the basic details of the document
              basic.BaseURL = new Uri("about:blank");
              basic.CreatorTool = string.Empty;
              basic.Label = string.Empty;
              basic.MetadataDate = DateTime.Now;


              pdfDoc.DocumentInformation.Remove(nameof(pdfDoc.DocumentInformation.Author));
              pdfDoc.DocumentInformation.Remove(nameof(pdfDoc.DocumentInformation.Creator));
              pdfDoc.DocumentInformation.Remove(nameof(pdfDoc.DocumentInformation.Keywords));
              pdfDoc.DocumentInformation.Remove(nameof(pdfDoc.DocumentInformation.Title));
              pdfDoc.DocumentInformation.Remove(nameof(pdfDoc.DocumentInformation.ModificationDate));
              pdfDoc.DocumentInformation.Remove(nameof(pdfDoc.DocumentInformation.Producer));
              pdfDoc.DocumentInformation.Remove(nameof(pdfDoc.DocumentInformation.Subject));
              pdfDoc.DocumentInformation.Remove(nameof(pdfDoc.DocumentInformation.CreationDate));


              //Save the document
              MemoryStream stream = new MemoryStream();
              pdfDoc.Save(stream);


              //Close the document
              pdfDoc.Close(true);

Th



5 Replies

JT Jeyalakshmi Thangamarippandian Syncfusion Team April 18, 2024 12:12 PM UTC

Hi Thanh,

We suspect that the issue may be specific to the document itself. Could you please share the input PDF document with us? This will allow us to replicate the problem on our end, enabling us to perform a more in-depth analysis and provide you with a prompt solution.

However, we can remove the metadata completely. You can refer to the KB documentation for further details on how to achieve this: How to remove metadata from a PDF using C# and VB.NET? | Syncfusion

Regards,

Jeyalakshmi T



TH Thanh Hai Dang April 23, 2024 03:25 AM UTC

Hi  Jeyalakshmi T,

I can not upload the files because size issue. So, you can download file in your github repo example. The file names are "Annotation1.pdf" and "PDF_Succinctly.pdf"

I also referred this link, however, the  CompressionOptions didn't exist in the class

PdfLoadedDocument.


I used the  loadedDocument.Compress(options); too, but it not work.

Regards,

TH



AG Anantha Gokula Raman Jeyaraman Syncfusion Team April 23, 2024 03:45 PM UTC

Hi Thanh,

We were unable to reproduce the reported issue.

The provided code removes all the meta data in the PDF document (PDF_Succinctly.pdf) as expected.

Original

Removed

Kindly use the following link to download the working console sample, https://www.syncfusion.com/downloads/support/common/6146/ze/RemoveMetadataSample_cfc475de.zip

Regards,
Anantha Gokula Raman J



TH Thanh Hai Dang September 7, 2024 02:59 PM UTC

Hi  Anantha Gokula Raman J, 

Unfortunately, I missed notification for this topic. Could you re-open permission to access your sample?

Thanks and regards



JT Jeyalakshmi Thangamarippandian Syncfusion Team September 9, 2024 12:09 PM UTC

Hi Thanh,


The provided sample link is working properly on our end. Please try the link below to download the sample:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/RemoveMetadataSample_cfc475de-222418882


Regards,

Jeyalakshmi T


Loader.
Up arrow icon