iTextSharp - update PDF catalog in SharePoint Document Library

≡放荡痞女 提交于 2019-12-11 19:49:52

问题


I'm trying to store and update a string in the PDF file catalog using the iTextSharp library. I'm able to do this without issue in a standard desktop application but I'm unable to get it to save on a SharePoint server (As part of a SharePoint feature which is written in C#).

I have an SPFile which is a recently added PDF file in the SharePoint Document Library.

I'm able to read an existing catalog property out of the file by using

            reader = new PdfReader(File.OpenBinaryStream());
            PdfName name = new PdfName(propertyName);
            PdfString propertyValue = (PdfString)reader.Catalog.Get(name);

Where propertyName and property Value are the key value pair stored in the PDF catalog.

I'm stuck on how to update that property and have the file saved back to the SharePoint document library. Is there any way I can do that? reader.catalog.put doesn't seem to actually change the file, or at least the changes aren't being saved to the document library.


回答1:


Can you please provide a little bit more of code? In your sample I see that you create PdfReader, but it is not possible to use PdfReader for updating Pdf document. You should use PdfStamper for that.

I have tried the following piece of code and it works in my SharePoint 2013 autohosted application. The code below changes document title of a PDF document from library.

//Open binary stream from library
ClientResult<Stream> stream = file.OpenBinaryStream();
clientContext.Load(file);
clientContext.ExecuteQuery();

//Initialize PdfReader
var reader = new PdfReader(stream.Value);

//We will write output file into memory. You can use temp file of course.
var ms = new MemoryStream();

//Initialize PdfStamper
var stamper = new PdfStamper(reader, ms);

//Tell stamper not to close stream when stamper itself is being closed
stamper.Writer.CloseStream = false;

//Change Title to "New Document Title"
var moreInfo = new Dictionary<string, string>();
moreInfo["Title"] = "New Document Title";
stamper.MoreInfo = moreInfo;

//Close stamper, readed
stamper.Close();
reader.Close();

//Flush memory stream and set it position to start
ms.Flush();
ms.Position = 0;

//Update library item with new content
var bi = new FileSaveBinaryInformation();
bi.ContentStream = ms;
file.SaveBinary(bi);
clientContext.Load(file);
clientContext.ExecuteQuery();

//Finally, close the stream
ms.Close();

That's all. Hope it helps.



来源:https://stackoverflow.com/questions/21894553/itextsharp-update-pdf-catalog-in-sharepoint-document-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!