0

I have pdf files in the "C:\\pdfs\\" directory. I want to get these pdf files and insert metadatas from the meta_data.txt file. With my lock of iTextSharp knowledge, I coded like this:

var pdf_files = Directory.GetFiles("C:\\pdfs\\", "*.pdf");
var i = 0;
foreach (var pdf_file in pdf_files)
{

    var read = new PdfReader(pdf_file);
    var size = read.GetPageSizeWithRotation(1);
    var document = new Document(size);
    var write = PdfWriter.GetInstance(document, new FileStream("C:\\temp\\" + "file_" + i, FileMode.Create, FileAccess.Write));

    var datas = File.ReadAllLines("C:\\pdfs\\" + @"meta_data.txt");
    var str = datas[i].Split('@');


    document.AddTitle(str[1]);
    document.AddSubject(str[2]);
    document.AddCreator(str[3]);
    document.AddAuthor(str[4]);
    document.AddKeywords(str[5]);
    document.Open();

    var cb = write.DirectContent;
    for (var pageNum = 1; pageNum <= read.NumberOfPages; pageNum++)
    {
        document.NewPage();
        var page = write.GetImportedPage(read, pageNum);
        cb.AddTemplate(page, 0, 0);
    }
    document.Close();
    read.Close();
    File.Delete(pdf_file);
    File.Move("C:\\temp\\" + "file_" + i, "C:\\created\\" + "file_" + i);

    i++;
}

This codes get an instance of main pdf files, while creation to temp directory, injects metadatas and later move the created directory. I don't find more practical method than this. Anyway, in some pdf files (generated as an original pdf file) there is no problem like this: enter image description here But some other pdf files (generated from scan or very old dated pdf file) are rotated programmatically. And it seems disgusting like this: enter image description here Worse than all, I don't know how I fix the problem. Could you help me how I manage this problem.

  • 2
    The reason is that you chose a non-optimal way to merge documents with itext which does not copy all properties of the original page (because the methods involved were designed for a different use case). Use a merging solution based on `PdfCopy` instead of `PdfWriter`, cf. [this answer](http://stackoverflow.com/a/15945467/1729265). – mkl Feb 19 '17 at 10:54
  • Please read the official documentation: http://developers.itextpdf.com/examples/miscellaneous/adding-metadata – Bruno Lowagie Feb 19 '17 at 11:11

1 Answers1

2

The correct answer to this question looks like this:

PdfReader reader = new PdfReader(src);
using (PdfStamper stamper = new PdfStamper(reader,
    new FileStream("C:\\temp\\" + "file_" + i, FileMode.Create, FileAccess.Write))) {
    Dictionary<String, String> info = reader.Info;
    info["Title"] = "Hello World stamped";
    info["Subject"] = "Hello World with changed metadata";
    info["Keywords"] = "iText in Action, PdfStamper";
    info["Creator"] = "Silly standalone example";
    info["Author"] = "Bruno Lowagie";
    stamper.MoreInfo = info;
}

Habip OĞUZ ignored what I wrote in Chapter 6 of "iText in Action - Second Edition", namely that using Document, PdfWriter and AddTemplate() is wrong when you want to manipulate a single existing PDF. By using AddTemplate(), you throw away plenty of features, such as interactivity, structure, and so on. You also create a PDF that is suboptimal because every page will be stored as a Form XObject.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Oops, indeed. I just saw that the op iterated over a number of input files and thought he wanted to merge and so pointed towards `PdfCopy`. Actually he separately manipulates each pdf, so a `PdfStamper` should be used... – mkl Feb 19 '17 at 17:41
  • @Bruno Lowagie - Thank you so much for your reply. In order to don't mislead other developers, I am wiping out my reply that served bad solution (or a non-solution). –  Feb 20 '17 at 00:03