0

As I've become interested in iTextSharp I need to learn C#. Since I know a bit of AutoHotkey (simple yet powerful script programming language for Windows) it is easer for me. However, I often come across code written in Java which is said to be easily converted to C#. Unfortunetly, I have some problems with it. Let's have a look at original code written by Bruno Lowagie.

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfDictionary root = reader.getCatalog();
    PdfDictionary names = root.getAsDict(PdfName.NAMES);
    names.remove(PdfName.EMBEDDEDFILES);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
}

This is what I have managed to write on my own:

static void removeFiles(string sourceFilePath, string destFilePath)
{

    try
    {
    // read src file
    FileStream inputStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.None);
    Document source = new Document();
    // open for reading
    PdfWriter reader = PdfReader.GetInstance(inputStream);
    source.Open();

    // create dest file
    FileStream outputStream = new FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
    Document dest = new Document();
    PdfWriter writer = PdfWriter.GetInstance(dest, inputStream); // open stream from src

    // remove embedded files from dest
    PdfDictionary root = dest.getCatalog().getPdfObject();
    PdfDictionary names = root.getAsDictionary(PdfName.Names);
    names.remove(PdfName.EmbeddedFiles);

    // close all
    source.Close();
    dest.Close();   

    }
    catch (Exception ex)
    {
    }

}

Unfortunately, there are many errors such as:

'Document' does not contain a definition for 'getCatalog' and no extension method 'getCatalog' 'PdfReader' does not contain a definition for 'GetInstance'

This is what I have managed to do after countless hours of coding and googling.

Community
  • 1
  • 1
menteith
  • 596
  • 14
  • 51
  • In java the `getCatalog` in a method of `PdfReader` but in C# you are trying to use `Document` – Scary Wombat Jan 10 '17 at 02:39
  • Why are you trying to call `GetInstance` on `PdfReader` class? – alayor Jan 10 '17 at 02:43
  • There are plenty of problems with the code. For instance: `PdfWriter reader = PdfReader.GetInstance(inputStream);` Even if `PdfReader` had a `GetInstance()` method, why would one cast a reader to a writer? Also: it doesn't make sense to use `Document`/`PdfWriter` instead of `PdfStamper`. I'm not a C# developer, but I've fixed some of the obvious errors in my answer. – Bruno Lowagie Jan 10 '17 at 08:07

1 Answers1

0

There are some iTextSharp examples available. See for instance: How to read a PDF Portfolio using iTextSharp

I don't know much about C#, but this is a first attempt to fix your code:

static void RemoveFiles(string sourceFilePath, string destFilePath)
{
    // read src file
    FileStream inputStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.None);
    // open for reading
    PdfReader reader = new PdfReader(inputStream);
    FileStream outputStream = new FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
    PdfStamper stamper = new PdfStamper(reader, outputStream);

    // remove embedded files
    PdfDictionary root = reader.Catalog;
    PdfDictionary names = root.GetAsDict(PdfName.NAMES);
    names.Remove(PdfName.EMBEDDEDFILES);

    // close all
    stamper.Close();
    reader.Close();   
}

Note that I don't understand why you were using Document and PdfWriter. You should use PdfStamper instead. Also: you are only removing the document-level attachments. If there are file attachment annotations, they will still be present in the PDF.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • It works fine, thanks! All I needed to do was to remove `catch (Exception ex)`. Thanks also for pointing to file attachment annotations, because I want them to be removed. I hadn't known the difference between the two earlier. – menteith Jan 10 '17 at 13:57