我编写了一个应用程序,通过解析CSV文件中的数据来处理大量PDF文件。我遇到的问题是我想保存PDF的第一页和第一页。当我使用PdfReader的reader.SelectPages("1")
时,它会导致表单字段变平。如果我发表评论,一切正常。
为什么这种方法会导致所有表单字段变平?然而,它正确导出了一页。
这是一个小提取物:
PdfReader reader = new PdfReader(formFile);
reader.SelectPages("1");
string newFile = Environment.CurrentDirectory + @"\Out" + documentCount + ".pdf";
PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create), '\0', true);
AcroFields fields = stamper.AcroFields;
如果我注释掉第二行,那就没有问题了。我想这适用于知道如何使用iTextSharp的人。
干杯
答案 0 :(得分:1)
如果您只想从现有PDF中提取页面,我建议您查看我在James Welch的博客文章How to extract pages from a PDF document中找到的代码。
以下是博客中的原始代码:
private static void ExtractPages(string inputFile, string outputFile, int start, int end) {
// get input document
PdfReader inputPdf = new PdfReader(inputFile);
// retrieve the total number of pages
int pageCount = inputPdf.NumberOfPages;
if (end < start || end > pageCount) {
end = pageCount;
}
// load the input document
Document inputDoc = new Document(inputPdf.GetPageSizeWithRotation(1));
// create the filestream
using (FileStream fs = new FileStream(outputFile, FileMode.Create)) {
// create the output writer
PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs);
inputDoc.Open();
PdfContentByte cb1 = outputWriter.DirectContent;
// copy pages from input to output document
for (int i = start; i <= end; i++) {
inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(i));
inputDoc.NewPage();
PdfImportedPage page =
outputWriter.GetImportedPage(inputPdf, i);
int rotation = inputPdf.GetPageRotation(i);
if (rotation == 90 || rotation == 270) {
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0,
inputPdf.GetPageSizeWithRotation(i).Height);
} else {
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
inputDoc.Close();
}
}