Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

c# - Set Metadata in iTextSharp

I am developing an application and i use the iTextSharp library.

I am also reading the iText in action from Manning so i can get references.

In Chapter 12 it has the following code to change the metadata in Java.

PdfReader reader = new PdfReader(src);
PdfStamper stamper =
new PdfStamper(reader, new FileOutputStream(dest));
HashMap<String, String> info = reader.getInfo();
info.put("Title", "Hello World stamped");
info.put("Subject", "Hello World with changed metadata");
info.put("Keywords", "iText in Action, PdfStamper");
info.put("Creator", "Silly standalone example");
info.put("Author", "Also Bruno Lowagie");
stamper.setMoreInfo(info);
stamper.close();

How can i do the same in C#?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Conversion from Java to C# is usually pretty straightforward. By convention, Java properties use get and set prefixes so to convert to C# you just need to drop the prefix and turn it into a .Net getter/setter call. getInfo() becomes Info and setMoreInfo(info) becomes MoreInfo = info. Then you just need to convert the native Java types to their equivalent C# types. In this case the Java FileOutputStream becomes a .Net FileStream and the HashMap<String, String> becomes a Dictionary<String, String>.

Lastly, I've updated the code to reflect recent changes to iTextSharp that now (as of 5.1.1.0) implement IDisposable now.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string workingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string inputFile = Path.Combine(workingFolder, "Input.pdf");
            string outputFile = Path.Combine(workingFolder, "Output.pdf");

            PdfReader reader = new PdfReader(inputFile);
            using(FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)){
                using (PdfStamper stamper = new PdfStamper(reader, fs))
                {
                    Dictionary<String, String> info = reader.Info;
                    info.Add("Title", "Hello World stamped");
                    info.Add("Subject", "Hello World with changed metadata");
                    info.Add("Keywords", "iText in Action, PdfStamper");
                    info.Add("Creator", "Silly standalone example");
                    info.Add("Author", "Also Bruno Lowagie");
                    stamper.MoreInfo = info;
                    stamper.Close();
                }
            }

            this.Close();
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...