我找到了重新编写旧代码的方法,这些代码将PDF文件签名为新文件,这标志着来自Web服务的MemoryStreams(字节数组).简单吧?那是昨天.今天我无法让它发挥作用.
这是旧代码,它使用FileStreams并且它可以工作:
public static string OldPdfSigner(PdfReader pdfReader, string destination, string password, string reason, string location, string pathToPfx)
{
using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, FileAccess.Read))
{
...
using (PdfStamper st = PdfStamper.CreateSignature(pdfReader, new FileStream(destination, FileMode.Create, FileAccess.Write), '\0'))
{
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.SetCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.Reason = reason;
sap.Location = location;
return destination;
}
}
}
Run Code Online (Sandbox Code Playgroud)
下面是我自己重做的内容,它抛出System.ObjectDisposedException:无法访问已关闭的Stream.
public static byte[] PdfSigner(PdfReader pdfReader, string password, string reason, string location, string pathToPfx)
{
using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, …Run Code Online (Sandbox Code Playgroud) 我试图使用javascript获取位置并传递坐标以在邮票上应用戳记,但它无法正常工作.下面是我用来捕获鼠标指针坐标的函数.
function divMove(e){
var div = document.getElementById('stamp');
div.style.position = 'absolute';
//div.style.top = e.clientY + 'px';
//div.style.left = e.clientX + 'px';
var box = div.getBoundingClientRect();
mouse_top = e.clientY;
mouse_left = e.clientX;
var diff_x = mouse_left - box.left;
var diff_y = mouse_top - box.top;
div.style.top = ((Number(div.style.top.replace("px", "")) - 1) + diff_y) +"px";
div.style.left = ((Number(div.style.left.replace("px", "")) - 1) + diff_x) +"px";
document.getElementById("data").innerHTML =
"mouse_top:" + mouse_top + "<br>mouse_left:" + mouse_left
}
Run Code Online (Sandbox Code Playgroud)
以下是使用iText处理冲压件的后端代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException …Run Code Online (Sandbox Code Playgroud) 我的web方法在我的%temp%文件夹中创建一个pdf文件,该文件有效.然后,我想使用下面的代码向该文件添加一些自定义字段(meta).
该类PdfStamper生成一个IOException,无论我使用它的.Close()方法还是using块刚结束.仍然保留文件句柄的过程是webdev Web服务器本身(我在VS2010 SP1中进行调试).
private string AddCustomMetaData(string guid, int companyID, string filePath)
{
try
{
PdfReader reader = new PdfReader(filePath);
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
PdfStamper st = new PdfStamper(reader, fs);
Dictionary<string, string> info = reader.Info;
info.Add("Guid", guid);
info.Add("CompanyID", companyID.ToString());
st.MoreInfo = info;
st.Close();
}
reader.Close();
return guid;
}
catch (Exception e)
{
return e.Message;
}
}
Run Code Online (Sandbox Code Playgroud)
无论我尝试什么,它都会不断抛出异常st.Close();,更确切地说:
该进程无法访问文件'C:\ Users [我的用户名]\AppData\Local\Temp\53b96eaf-74a6-49d7-a715-6c2e866a63c3.pdf',因为它正由另一个进程使用.
要么我忽视了一些显而易见的事情,要么就是PdfStamper我还没有意识到的课程有问题.使用的itextsharp的版本是5.3.3.0和5.4.0.0,问题是相同的.
任何见解将不胜感激. …
我遇到了一个 Web API 的问题,该 API 接收 PDF 文档(PDF 表单)作为 base64 编码字符串,将其放入文件中,填充一些字段(使用iTextSharpPdfStamper类)并将结果发送回Base64 编码的字符串,再次被制成文件。
当我随后尝试使用Foxit Reader打开此文件/文档时,我仍然可以编辑这些字段,但是当使用Adobe Acrobat Reader尝试打开此文件/文档时,这些字段不再可编辑。
在福昕阅读器中打开属性页并查看文档的安全属性时,它说一切都是允许的。然而,当在Adobe Acrobat Reader中执行相同操作时,它表示不允许评论、填写字段和签名(这是在将其发送到 API 之前的情况)。
不幸的是,我无法规定最终用户正在使用哪种 PDF 阅读器,所以我需要它是通用的。
我可以看到许多其他人都发布了类似的问题,但我没有在那里看到我的特定问题。
有谁知道这里发生了什么以及如何解决它?
编辑:添加代码
我正在这样做(稍微简化了一点,但相同):
PdfReader reader = new PdfReader("path to PDF form file");
stamp = new PdfStamper(reader, new FileStream("path to filled PDF form file", FileMode.Create));
AcroFields form = stamp.AcroFields;
IDictionary<string, AcroFields.Item> fs = form.Fields;
foreach (var f in fs)
{
form.SetField(f.Key, "some value");
}
stamp.Close();
Run Code Online (Sandbox Code Playgroud)
编辑:添加了示例 PDF …
我正在 Libre Office 中创建表单控件并将文档导出为 pdf。
\n尝试使用 itextsharp(即 C# 程序)设置控件(文本框)的文本只会清空该框。
\n但是,如果我使用 acrobat reader 打开 pdf 并编辑框中的文本,则将文档结果保存在 pdf 中,以便可以写入该文本框。
\n为什么我必须这样做?
\n##错误重现
\n单击 libre office 中的工具栏图标。
\n\n在文档中拖出一个正方形。
\n\n\n导出为 pdf:
\n\n时间:2019-03-17 标签:c#code
\nif (openFileDialog1.ShowDialog() == DialogResult.OK)\n{\n if (saveFileDialog1.ShowDialog() == DialogResult.OK)\n {\n using (var fs = new FileStream(saveFileDialog1.FileName, FileMode.Create))\n {\n var reader = new PdfReader(openFileDialog1.FileName);\n {\n using (var pdfStamper = new PdfStamper(reader, fs))\n {\n var acroFields = pdfStamper.AcroFields;\n\n acroFields.SetField("currentdate", DateTime.Now.ToString());\n\n pdfStamper.FormFlattening …Run Code Online (Sandbox Code Playgroud) pdfstamper ×5
itext ×4
c# ×3
asp.net ×1
itext7 ×1
itextsharp ×1
java ×1
javascript ×1
libreoffice ×1
memorystream ×1
pdf ×1
pdf-form ×1