iTextSharp - 更新SharePoint文档库中的PDF目录 [英] iTextSharp - update PDF catalog in SharePoint Document Library

查看:152
本文介绍了iTextSharp - 更新SharePoint文档库中的PDF目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iTextSharp库存储和更新PDF文件目录中的字符串。我能够在标准桌面应用程序中毫无问题地执行此操作,但我无法将其保存在SharePoint服务器上(作为使用C#编写的SharePoint功能的一部分)。

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#).

我有一个SPFile,这是最近在SharePoint文档库中添加的PDF文件。

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);

其中propertyName和property Value是存储在PDF目录中的键值对。

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

我坚持如何更新该属性并将文件保存回SharePoint文档库。有什么方法可以做到吗? reader.catalog.put似乎没有实际更改文件,或者至少更改没有保存到文档库。

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.

推荐答案

请提供更多代码吗?在您的示例中,我看到您创建了PdfReader,但是无法使用PdfReader更新Pdf文档。您应该使用PdfStamper。

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.

我尝试了以下代码,它可以在我的SharePoint 2013自动主题应用程序中运行。下面的代码从库中更​​改PDF文档的文档标题。

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.

这篇关于iTextSharp - 更新SharePoint文档库中的PDF目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆