0
votes

Hi I have a C# code snippet which would assign title to the pdf file.But I am trying to do the same for each and every pdf files in a directory.Can any one help me...?

Following is the code snippet " PdfReader pdfReader = new PdfReader(filePath); using (FileStream fileStream = new FileStream(newFilePath, FileMode.Create, FileAccess.Write)) { string title = pdfReader.Info["Title"] as string; Trace.WriteLine("Existing title: " + title);

        PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream); 

        Hashtable newInfo = pdfReader.Info; 

        newInfo["Title"] = "New title"; 

        pdfStamper.MoreInfo = newInfo; 

        pdfReader.Close(); 
        pdfStamper.Close(); 
    } 

"

1

1 Answers

0
votes

Try this code, hope that works for you...

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", "New title");

                    stamper.MoreInfo = info;
                    stamper.Close();
                }
            }

            this.Close();
        }
    }
}