我使用下面给出的c#代码使用itextsharp从pdf文档中提取页面。 该页面被提取得很好但是它以某种方式弄乱了acro字段。 我能够在提取的页面acro字段上查看和输入数据,但是当我从代码中检查acro字段的数量时,它将显示为“0”
要提取的代码:
public void ExtractPage(string sourcePdfPath, string outputPdfPath, int pageNumber)
{
PdfReader reader = null;
Document document = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
try
{
// Intialize a new PdfReader instance with the contents of the source Pdf file:
reader = new PdfReader(sourcePdfPath);
// Capture the correct size and orientation for the page:
document = new Document(reader.GetPageSizeWithRotation(pageNumber));
// Initialize an instance of the PdfCopyClass with the source
// document and an output file stream:
pdfCopyProvider = new PdfCopy(document,
new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
document.Open();
// Extract the desired page number:
importedPage = pdfCopyProvider.GetImportedPage(reader, pageNumber);
pdfCopyProvider.AddPage(importedPage);
document.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}
检查Acro字段的代码:
private static int GetNumberOfSignatures(string FileName)
{
int best = 0; if (Path.GetExtension(FileName) != ".pdf" || !File.Exists(FileName)) return 0;
var form = new PdfReader(FileName).AcroFields;
var formFields = form.Fields;
foreach (var cur in formFields)
{
if (cur.Key.ToLower().StartsWith("signature_placeholder_"))
{
int val = 0;
if (!int.TryParse(cur.Key.Substring(22), out val)) val = 0;
if (val > best) best = val;
}
if (cur.Key.ToLower() == "signature_placeholder" && best < 1) best = 1;
}
return best;
}
答案 0 :(得分:0)
您使用错误的代码提取页面。您应该使用PdfStamper
代替PdfCopy
。例如:如果要创建一个只包含现有文档第4页的新PDF,则应使用以下代码:
PdfReader reader = new PdfReader(sourcePdfPath);
reader.SelectPages("4");
PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create, FileAccess.ReadWrite);
stamper.Close();
有关SelectPages()
方法参数的概述,请参阅我对PDF Page re-ordering using itext的回答
请注意,从签名的PDF文档中提取页面将始终使签名无效。相反的情况将违反有关数字签名的规范。