- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Windows 服务,可以即时合并 PDF,然后将它们移动到另一个位置。在大多数情况下,我无法控制某人想要合并的内容。碰巧经常会处理损坏的 PDF,因此创建新的 PdfDocument 会抛出 PdfException“未找到预告片”。我正在捕获异常并关闭文档,但它在关闭后出现 PDF 本身仍然以某种方式被锁定。我需要删除该目录,但在尝试这样做时它会抛出 IOException 并使服务崩溃。
我已经验证调用 PdfDocument 构造函数是锁定 pdf 的原因,并且在关闭文件后立即保持锁定状态。
有什么想法吗? iText 可以做些什么来帮助解决这个问题,还是我需要想出一些办法来预先检查损坏的 PDF?
进程目录
private void ProcessDirectory(string directoryPath)
{
EventLogManager.WriteInformation("ProcessDirectory");
// DON'T TOUCH THE BACKUPS, ERRORS AND WORK DIRECTORIES. Just in case they were made or renamed after the fact for some reason
if (directoryPath != this._errorsPath && directoryPath != this._backupsPath && directoryPath != this._workPath)
{
string pdfJsonPath = System.IO.Path.Combine(directoryPath, "pdf.json");
if (File.Exists(pdfJsonPath))
{
string workPath = System.IO.Path.Combine(this._workPath, System.IO.Path.GetFileName(directoryPath));
try
{
CopyToDirectory(directoryPath, workPath);
PdfMerge pdfMerge = null;
string jsonPath = System.IO.Path.Combine(workPath, "pdf.json");
using (StreamReader r = Helpers.GetStreamReader(jsonPath))
{
string json = r.ReadToEnd();
pdfMerge = JsonConvert.DeserializeObject<PdfMerge>(json);
}
FillFormFields(workPath, pdfMerge);
if (pdfMerge.Pdfs.Any(p => !String.IsNullOrWhiteSpace(p.OverlayFilename)))
{
ApplyOverlays(workPath, pdfMerge);
}
MergePdfs(workPath, pdfMerge);
//NumberPages(workPath, pdfMerge);
FinishPdf(workPath, pdfMerge);
// Move original to backups directory
if (DoSaveBackups)
{
string backupsPath = System.IO.Path.Combine(this._backupsPath, String.Format("{0}_{1}", System.IO.Path.GetFileName(directoryPath), DateTime.Now.ToString("yyyyMMddHHmmss")));
Directory.Move(directoryPath, backupsPath);
}
else
{
Directory.Delete(directoryPath, true);
}
}
catch (Exception ex)
{
EventLogManager.WriteError(ex);
if (DoSaveErrors)
{
// Move original to errors directory
string errorsPath = System.IO.Path.Combine(this._errorsPath, String.Format("{0}_{1}", System.IO.Path.GetFileName(directoryPath), DateTime.Now.ToString("yyyyMMddHHmmss")));
Directory.Move(directoryPath, errorsPath);
}
else
{
Directory.Delete(directoryPath, true);
}
}
// Delete work directory
// THIS IS WHERE THE IOEXCEPTION OCCURS AND THE SERVICE CRASHES
Directory.Delete(workPath, true);
}
else
{
EventLogManager.WriteInformation(String.Format("No pdf.json file. {0} skipped.", directoryPath));
}
}
}
填充表单字段
private void FillFormFields(string directoryPath, PdfMerge pdfMerge)
{
if (pdfMerge != null && pdfMerge.Pdfs != null)
{
string formPath = String.Empty;
string newFilePath;
PdfDocument document = null;
PdfAcroForm form;
PdfFormField pdfFormField;
foreach (var pdf in pdfMerge.Pdfs)
{
try
{
formPath = System.IO.Path.Combine(directoryPath, pdf.Filename);
newFilePath = System.IO.Path.Combine(
directoryPath,
String.Format("{0}{1}", String.Format("{0}{1}", System.IO.Path.GetFileNameWithoutExtension(pdf.Filename), "_Revised"), System.IO.Path.GetExtension(pdf.Filename)));
// THIS IS WHERE THE PDFEXCEPTOIN OCCURS
document = new PdfDocument(Helpers.GetPdfReader(formPath), new PdfWriter(newFilePath));
form = PdfAcroForm.GetAcroForm(document, true);
if (pdf.Fields != null && pdf.Fields.Count > 0)
{
foreach (var field in pdf.Fields)
{
if (field.Value != null)
{
pdfFormField = form.GetField(field.Name);
if (pdfFormField != null)
{
form.GetField(field.Name).SetValue(field.Value);
}
else
{
EventLogManager.WriteWarning(String.Format("Field '{0}' does not exist in '{1}'", field.Name, pdf.Filename));
}
}
}
}
form.FlattenFields();
}
catch (Exception ex)
{
throw new Exception(String.Format("An exception occurred filling form fields for {0}", pdf.Filename), ex);
}
finally
{
if (document != null)
{
document.Close();
}
}
// Now rename the new one back to the old name
File.Delete(formPath);
File.Move(newFilePath, formPath);
}
}
}
似乎为了正确处理所有内容,您必须将单独的 PdfReader 和 PdfWriter 对象声明为 using 语句并将它们传递到 PdfDocument。像这样:
using (reader = Helpers.GetPdfReader(formPath))
{
using (writer = new PdfWriter(newFilePath))
{
using (document = new PdfDocument(reader, writer))
{
// The rest of the code here
}
}
}
我不确定为什么除了 iText 在处理 PdfDocument 时没有处理单独的 PdfReader 和 PdfWriter 之外,我认为它会处理。
最佳答案
找出哪些 itext7 类实现了 IDisposable(从文档或 Visual Studio 对象浏览器等),并确保在 using block 中使用它们,就像您已经为 StreamReader 使用 block 一样。
编辑:@sourkrause 的解决方案可以缩短为:
using (reader = Helpers.GetPdfReader(formPath))
using (writer = new PdfWriter(newFilePath))
using (document = new PdfDocument(reader, writer))
{
// The rest of the code here
}
关于c# - PdfDocument 在关闭后保持锁定状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51269627/
我对 PDFDocuments 有疑问。如何将 PdfDocument 对象的特定页面复制到另一个 PdfDocument 对象。 我尝试过使用 moveTo 方法,但它不起作用,我也尝试过使用 co
我有一个包含多个 PDF 的列表,我想将它们全部合并到一个 PdfDocument(我使用 iText),然后将这个 PdfDocument 转换成一个 ByteArrayOutputStream(或
我有一个 Windows 服务,可以即时合并 PDF,然后将它们移动到另一个位置。在大多数情况下,我无法控制某人想要合并的内容。碰巧经常会处理损坏的 PDF,因此创建新的 PdfDocument 会抛
我正在使用 Android 创建一个 Pdf 文件 PdfDocument类(class)。我从 XML 文件(ConstraintLayout 中的 LinearLayout)扩展布局,然后在运行时
我想使用 KitKat 中引入的 PdfDocument android 类从 View 生成 PDF 文件。我设法做到了,到目前为止文件生成正常,最终得到了正确的 PDF。唯一的问题是文件很大,一页
我正在使用 Android Studio 并尝试让我的应用生成 PDF。我已经设置了一个 Activity 来生成我希望我的 PDF 具有的内容。如果我让它显示在屏幕上,这很好用。 Activity
嘿,我已经在位图中转换了我的自定义 ListView 。我的位图的大小很大所以我想使用 PdfDocument 类在两个单独的 pdf 文件页面中打印该位图。我已经搜索过相同但没有得到适当的解决方案。
我目前正在尝试拆分 PDF 文件,然后保存每个文件 PdfDocument到一个新文件。问题是我找不到任何方法来附加新的 PdfWriter到现有 PdfDocument .这是我目前用来打开和拆分
我正在桌面和手持设备之间发送文档,并且我想向 PDF 添加元数据 header ,如下所示。 \n {"fileInfoEncodedInJson\": {"filename":"My Prin
因此,在我的应用程序中,我将图像上传到公司服务器 然后使用内置的 PDFdocument 方法将这些图像转换为 PDF, 现在我的问题是我试图通过添加 IF 语句来动态生成 PDF。因此,我的图像被加
我正在尝试编写一个简单的函数来获取 PDF 中的页数。我现在看到的每个代码示例似乎都无法使用 Swift 3,而且 Xcode 推荐的任何内容仍然不起作用。 func pageCount(filepa
我将 pdfdocument 添加到 PDFView 。但它显示出优势,也显示出慢跑。如何删除此边距。并且有什么方法可以将 pdfdocument 设置到左上角的位置。 最佳答案 我认为这有助于解决问
我遇到一些用 C# 编写的 Windows 应用商店应用程序的问题。 该应用程序加载一个在数据库中存储为字节数组的 PDF 文件,并尝试在其自己的 XAML View 中呈现图像对象中的第一页。代码如
我正在尝试使用 Android 原生 PdfDocument 类(来自 api 19)创建一个简单的 PDF 文档。我想要的是有一个 XML 布局文件,例如称为 pdf_doc.xml,然后在创建 P
在 PdfDocument.Page 上绘图时,我遇到了图像变大的问题。该应用程序适用于运行 Android 4.4.4(API 级别 19)的设备。 我正在生成一个 pdf 文档并添加一个图像,如下
PdfDocument document = new PdfDocument(); // crate a page description PageInfo pageI
我有一个 PDFDocument(当然由 PDFPages 组成),我需要一个 CGImageRef 来粘贴到我的 IKImageView 中。 目前,我从 PDFPage 获取数据表示,将其放入 N
有什么方法可以将空白页添加到现有的 PdfDocument 中吗?我创建了一个这样的方法: public void addEmptyPage(PdfDocument pdfDocument){
我在应用程序文档文件夹中存储了一个 PDF 文件。 我有一个 PDFView 作为我的 View Controller View 的 subview 。我可以从文件中加载 PDF 并显示它。没问题。
我正在使用 PDFKit 通过 pdfView.document = document 函数在 PDFView 中显示 PDF 文件。 问题在于,我也在使用 UIDocumentPicker API,
我是一名优秀的程序员,十分优秀!