1

I have a problem when I edit the metadata of a pdf with iTextSharp. I save a word document in pdf with Word. The field called "Producer" is filled by word with the text "Microsoft Word 210". After, I edit the metadata with ITextSharp and iTextSharp tries to edit this field in order to add the text "modified using iTextSharp 4.1.6".

The result is Producer(þÿMicrosoft® Word 2010; modified using iTextSharp 4.1.6 by 1T3XT). In adobe reader, the field PDF Producer in document properties shows chinese characters.

Adobe can read the field if I remove manually the characters þÿ.

Do you know why I have this problem ? What can I do to solve this problem ?

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44

1 Answers1

0

Just for reference, this works with iText 2.1.7. It is Java code, but probably works too for C#.

    import java.io.File;
import java.io.FileOutputStream;

import org.junit.Test;

import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfString;

public class AppTest {

    @Test
    public void testApp() throws Exception {
        PdfReader reader = new PdfReader(AppTest.class.getResourceAsStream("/msword2010.pdf"));

        FileOutputStream fos = new FileOutputStream(new File("target", "modified_msword2010.pdf"));
        PdfStamper stamper = new PdfStamper(reader, fos, '\0', true);

        PdfDictionary infoDict = stamper.getReader().getTrailer().getAsDict(PdfName.INFO);
        String producerCleaned = null;

        if (infoDict != null) {
            PdfString producer = (PdfString) infoDict.get(PdfName.PRODUCER);
            if (producer != null) {
                producerCleaned = producer.toUnicodeString();
                PdfString cleanStrObj = new PdfString(producerCleaned);
                infoDict.put(PdfName.PRODUCER, cleanStrObj);
            }
        }

        stamper.close();
    }
}
beat
  • 1,857
  • 1
  • 22
  • 36