Set metadata to iTextSharp

I am developing an application and I am using the iTextSharp library.

I also read iText in action from Manning, so I can get links.

In chapter 12, he has the following code for modifying 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 #?

+6
source share
3 answers

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(); } } } 
+11
source

I just did this after finding the right place in the view window of the PdfWriter object, it changes the "PDF Creator" to PDF, because it is not available by default:

 private static void ReplacePDFCreator(PdfWriter writer) { Type writerType = writer.GetType(); PropertyInfo writerProperty = writerType.GetProperties(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).Where(p => p.PropertyType == typeof(PdfDocument)).FirstOrDefault(); PdfDocument pd = (PdfDocument)writerProperty.GetValue(writer); Type pdType = pd.GetType(); FieldInfo infoProperty = pdType.GetFields(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).Where(p => p.Name == "info").FirstOrDefault(); PdfDocument.PdfInfo pdfInfo = (PdfDocument.PdfInfo)infoProperty.GetValue(pd); PdfString str = new PdfString("YOUR NEW PDF CREATOR HERE"); pdfInfo.Remove(new PdfName("Producer")); pdfInfo.Put(new PdfName("Producer"), str); } 

I got a suggestion from "@ yannic-donot-text" and it is much easier !:

 private static void ReplacePDFCreator(PdfWriter writer) { writer.Info.Put(new PdfName("Producer"), new PdfString("YOUR NEW PDF CREATOR HERE")); } 

I thought it was only archived, but I appreciate the cooperation of more educated people :)

thanks!

+7
source

  public void pdfproperties() { string inputFile = @"D:\1.pdf"; string outputFile = @"D:\48.pdf"; PdfReader reader = new PdfReader(inputFile); foreach (KeyValuePair<string, string> KV in reader.Info) { reader.Info.Remove(KV.Key); } using (FileStream FS = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (Document Doc = new Document()) { using (PdfCopy writer = new PdfCopy(Doc, FS)) { Doc.Open(); Doc.AddTitle("Add Title"); Doc.AddSubject("Add Subject"); Doc.AddKeywords("Add Keywords"); Doc.AddCreator("Application Creator"); Doc.AddAuthor("Add Author"); for (int i = 1; i <= reader.NumberOfPages; i++) { writer.AddPage(writer.GetImportedPage(reader, i)); } writer.Info.Put(new PdfName("Producer"), new PdfString("Producer Name")); Doc.Close(); } } } } 
0
source

Source: https://habr.com/ru/post/897104/


All Articles