Converting from Java to C # is usually quite simple. By convention, Java properties use the get and set prefixes, so to convert to C # you just need to remove 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, Java FileOutputStream becomes .Net FileStream , and HashMap<String, String> becomes Dictionary<String, String> .
Finally, I updated the code to reflect the recent iTextSharp changes that are now (starting with 5.1.1.0) implementing 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(); } } }
source share