Share via


Adding metadata to existing pdf using Itextsharp

Question

Tuesday, October 14, 2008 1:59 AM

Hi all,

I am using Itextsharp to add metadata to an existing PDF file. I am sucessfull in doing it. Problem is that while saving this pdf file the documents are rotated anticlockwise. I am not able to find what is the reason but the original file is in the normal direction. Can anybody please help me in solving this. Below is the code.

 

PdfReader reader = new PdfReader(strGetFile);// strGetFile-->original file path

Rectangle size = reader.GetPageSizeWithRotation(1);

Document document = new Document(size);PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strMoveToRepository, FileMode.Create, FileAccess.Write));//strMoveToRepository-->file to save

document.AddTitle(dt.Rows[0].ItemArray[2].ToString());

document.AddSubject(dt.Rows[0].ItemArray[7].ToString());

document.AddCreator(dt.Rows[0].ItemArray[9].ToString());

document.AddHeader("Document Group", dt.Rows[0].ItemArray[4].ToString());

document.AddHeader("Document Name", dt.Rows[0].ItemArray[1].ToString());

document.AddHeader("Contributor", dt.Rows[0].ItemArray[3].ToString());

document.AddHeader("Function", dt.Rows[0].ItemArray[5].ToString());

document.AddHeader("Version", dt.Rows[0].ItemArray[6].ToString());

document.AddHeader("Source", dt.Rows[0].ItemArray[8].ToString());

document.AddCreator("Scorpus");

document.Open();

PdfContentByte cb = writer.DirectContent;for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)

{

document.NewPage();

PdfImportedPage page = writer.GetImportedPage(reader, pageNumber);int rotation = reader.GetPageRotation(pageNumber);

cb.AddTemplate(page, 0, 0);

}

document.Close();

All replies (4)

Tuesday, October 14, 2008 5:25 AM

Hi geetha,

   I have used your code to change the metadata of the pdf file. yes as you told that metainfo is updated. and even for me that document is coming fine as it how in orginal version (not rotated anticlockwise.).

  I think you may be doing some where in coding rotating the document please check it out. or u send your whole code. i can help you out.

 you also check the itext library what you have latest one or not. because mine is latest one and working fine without rotated docu problem.

 Regards,

Prabakaran M
Software Engineer

If this post was useful to you, please Don't forget to click "Mark as Answer".

This will help readers to know which post solved your issue and make their search easy.

 


Wednesday, January 27, 2010 12:18 AM

Hi Geetha, i know this post had been posted long back. I am having a similar issue which u faced. were you able to fix it. I know this issue is coming up with the scanned documents being converted by itextsharp to pdf.

can you help me.

Harish

[email protected]


Wednesday, January 27, 2010 3:31 AM

HI please download the latest version of the Itextsharp library , refer it to your project and follow the below code,

PdfReader reader = new PdfReader(strGetFile);
                        Rectangle size = reader.GetPageSizeWithRotation(1);
                        Document document = new Document(size);
                        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strMoveToRepository, FileMode.Create, FileAccess.Write));
                        document.AddTitle(dt.Rows[0].ItemArray[2].ToString());
                        document.AddSubject(dt.Rows[0].ItemArray[7].ToString());
                        document.AddCreator(dt.Rows[0].ItemArray[9].ToString());
                        document.AddHeader("Document Group", dt.Rows[0].ItemArray[4].ToString());
                        document.AddHeader("Document Name", dt.Rows[0].ItemArray[1].ToString());
                        document.AddHeader("Contributor", dt.Rows[0].ItemArray[3].ToString());
                        document.AddHeader("Function", dt.Rows[0].ItemArray[5].ToString());
                        document.AddHeader("Version", dt.Rows[0].ItemArray[6].ToString());
                        document.AddHeader("Source", dt.Rows[0].ItemArray[8].ToString());
                        document.AddCreator("Scorpus");
                        document.Open();
                        PdfContentByte cb = writer.DirectContent;
                        for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
                        {
                            document.NewPage();
                            PdfImportedPage page = writer.GetImportedPage(reader, pageNumber);
                            cb.AddTemplate(page, 0, 0);
                        }
                        document.Close()

 


Wednesday, January 27, 2010 5:19 AM

Hi, also refer to the below link for topic "Adding a template to the document"    http://itextsharp.sourceforge.net/tutorial/ch40.html.

to rotate a template by 90 degree
change the line of code from cb.AddTemplate(page, 0, 0);
 to cb.AddTemplate(page,0,-1,1,0,0,size.Height);