0

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 Answer 1

0

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@ Qasim....Thanks for your reply and the problem is like i dont know the file names in a folder as i may have some 1000+ files so is it possible to provide different title to each and every pdf file in that folder from an excel sheet.Can you help me with this....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.