开发者

How does one save the .MoreInfo property of a PDF with iTextSharp?

开发者 https://www.devze.com 2023-02-25 17:08 出处:网络
I currently have the following class that I\'m trying to add a Hashtable of metadata properties to a PDF.The problem is, even though it appears to assign the hashtable to the stamper.MoreInfo property

I currently have the following class that I'm trying to add a Hashtable of metadata properties to a PDF. The problem is, even though it appears to assign the hashtable to the stamper.MoreInfo property it doesn't appear to save the MoreInfo p开发者_JAVA技巧roperty once the stamper is closed.

    public class PdfEnricher
{
    readonly IFileSystem fileSystem;

    public PdfEnricher(IFileSystem fileSystem)
    {
        this.fileSystem = fileSystem;
    }

    public void Enrich(string pdfFile, Hashtable fields)
    {
        if (!fileSystem.FileExists(pdfFile)) return;

        var newFile = GetNewFileName(pdfFile);
        var stamper = GetStamper(pdfFile, newFile);

        SetFieldsAndClose(stamper, fields);
    }

    string GetNewFileName(string pdfFile)
    {
        return fileSystem.GetDirectoryName(pdfFile) + @"\NewFileName.pdf";
    }

    static void SetFieldsAndClose(PdfStamper stamper, Hashtable fields)
    {
        stamper.MoreInfo = fields;
        stamper.FormFlattening = true;
        stamper.Close();
    }

    static PdfStamper GetStamper(string pdfFile, string newFile)
    {
        var reader = new PdfReader(pdfFile);
        return new PdfStamper(reader, new FileStream(newFile, FileMode.Create));
    }
}

Any ideas?


As always, Use The Source.

  • In this case, I saw a possibility fairly quickly (Java source btw):

    public void close() throws DocumentException, IOException {
      if (!hasSignature) {
        stamper.close( moreInfo );
        return;
      }

Does this form already have signatures of some sort? Lets see when hasSignatures would be true.

That can't be the case with your source. hasSignatures is only set when you sign a PDF via PdfStamper.createSignature(...), so that's clearly not it.

  • Err... how are you checking that your MoreInfo was added? It won't be in the XMP metadata. MoreInfo is added directly to the Doc Info dictionary. You see them in the "Custom" tab of Acrobat (and most likely Reader, though I don't have it handy at the moment).

  • Are you absolutely sure MoreInfo isn't null, and all its values aren't null?

  • The Dictionary is just passed around by reference, so any changes (in another thread) would be reflected in the PDF as it was written.


The correct way to iterate through a document's "Doc info dictionary":

PdfReader reader = new PdfReader(somePath);
Map<String, String> info = reader.getInfo();

for (String key : info.keySet()) {
  System.out.println( key + ": " + info.get(key) );
}

Note that this will go through all the fields in the document info dictionary, not just the custom ones. Also be aware that changes made the the Map from getInfo() will not carry over to the PDF. The map is new'ed, populated, and returned.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号