0

I use the itextsharp library to remove unwanted bookmarks from PDF files.

I developed the following code:

    private static void RemovePDFbookmarks(string filein, string fileout)
    {
        PdfReader pdfReader = new PdfReader(filein);
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileStream(fileout, FileMode.Create));

        document.Open();

        copy.AddDocument(pdfReader);

        document.Close();
        pdfReader.Close();
        copy.Close();
    }

This method creates a copy of the original file. During the following process, I need to delete the original file and rename the new file back to the original file's name.

How can I remove the bookmarks in the original PDF without the copy-delete-rename detour?

Community
  • 1
  • 1
Chris
  • 1
  • 3
  • 1
    Tool/product recommendation questions are off-topic for StackOverflow (which is what you're asking for at the end of your question). – David Makogon May 10 '17 at 11:09
  • Possible duplicate of [Modifying an Existing PDF without creating an new pdf file](http://stackoverflow.com/questions/8141448/modifying-an-existing-pdf-without-creating-an-new-pdf-file) - the first answer gives you a solution (for Java, you should be able to do something similar in C#), the second answer explains you why (it is an operating system limitation - not a limit from the programming language or library used). You can skip the delete part, just copy-rename should be enough. – Amedee Van Gasse May 10 '17 at 11:33
  • Thanks for your comment, @Amedee. As it is possible to modify bookmarks in Adobe, I expected there should be a way to do this with C#. However, I do not know what Adobe is doing behind the scenes... – Chris May 11 '17 at 11:50
  • Behind the scenes Adobe also does copy-rename. Copy to memory, close original file (releasing operating system file locks) and save back to location of original file is a variation. But in case of power loss or other problems during the save, you lose both files. Renaming a file is an atomic operation so less risk of data loss during failure. – Amedee Van Gasse May 11 '17 at 12:11

0 Answers0