数字签名不使用 iTextSharp 复制
Digital signature is not copied with iTextSharp
我有这种方法,可以将包含许多页面的 pdf 拆分为每个页面的多个文件。
我注意到如果 pdf 包含数字签名或形状(例如矩形),iTextsharp 不会将这些内容复制到拆分文件中。
有什么理由吗?
我在这里遗漏了什么吗?
private static void MultipleSplitPdf(IResult result, List<string> fileNames, string directoryforsplit, out List<string> outFileNames)
{
if (fileNames.Count == 0)
{
result.SetError("no files!");
outFileNames = null;
return;
}
else
{
try
{
outFileNames = new List<string>();
if (FileManager.DeleteDir(result, directoryforsplit) == false)
return;
DirectoryInfo dirInfo;
if (FileManager.GetDirectory(result, directoryforsplit, out dirInfo) == false)
return;
int counter = 1;
foreach (string fileName in fileNames)
{
// we create a reader for a certain document
PdfReader reader = new PdfReader(fileName);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
//Console.WriteLine("There are " + n + " pages in the original file.");
int pagenumber = n;
//if (pagenumber == 1)
//{
// FileInfo a = new FileInfo(fileName);
// a.CopyTo(directoryforsplit);
//}
//else
for (int i = 1; i <= pagenumber; i++)
{
iTextSharp.text.Document document1 = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(i));
string splittedFilePath = directoryforsplit + "\Split" + counter.ToString() + ".pdf";
PdfWriter writer1 = PdfWriter.GetInstance(document1, new FileStream(splittedFilePath, FileMode.Create));
document1.Open();
PdfContentByte cb1 = writer1.DirectContent;
PdfImportedPage page;
int rotation;
document1.SetPageSize(reader.GetPageSizeWithRotation(i));
document1.NewPage();
page = writer1.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
counter++;
document1.Close();
}
}
// step 1: creation of a document-object
//iTextSharp.text.Document document2 = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(pagenumber));
// step 2: we create a writer that listens to the document
//PdfWriter writer2 = PdfWriter.GetInstance(document2, new FileStream(args[2], FileMode.Create));
// step 3: we open the document
//document2.Open();
//PdfContentByte cb2 = writer2.DirectContent;
// step 4: we add content
//while (i < pagenumber - 1)
//{
//}
//while (i < n)
//{
// i++;
// document2.SetPageSize(reader.GetPageSizeWithRotation(i));
// document2.NewPage();
// page = writer2.GetImportedPage(reader, i);
// rotation = reader.GetPageRotation(i);
// if (rotation == 90 || rotation == 270)
// {
// cb2.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
// }
// else
// {
// cb2.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
// }
// Console.WriteLine("Processed page " + i);
//}
//// step 5: we close the document
//document2.Close();
FileInfo[] wordFiles = dirInfo.GetFiles();
foreach (var item in wordFiles.OrderBy(i => i.LastWriteTime))
outFileNames.Add(dirInfo.FullName + "\" + item.Name);
}
catch (Exception e)
{
outFileNames = null;
result.SetError(e.Message + "\n" + e.StackTrace);
return;
}
}
}
- 你做错了!请阅读Chapter 6 of iText in Action and you'll discover that you should use
PdfStamper
or PdfCopy
, NOT a combination of Document
/PdfWriter
to split or merge documents. As documented, your code removes all interactive features! See How to merge documents correctly? and read the documentation! This book is available for free: The Best iText Questions on Whosebug。
- 您的 PDF 文档已数字签名。这意味着您无法从中提取页面并且您无法在不破坏数字签名的情况下将其与其他文档合并。阅读 how to add blank page in digitally signed pdf using java? 找出设计中的错误。您正在尝试做一些未完成的事情(一般而言,不限于 iText 或 iTextSharp)。
我有这种方法,可以将包含许多页面的 pdf 拆分为每个页面的多个文件。 我注意到如果 pdf 包含数字签名或形状(例如矩形),iTextsharp 不会将这些内容复制到拆分文件中。 有什么理由吗? 我在这里遗漏了什么吗?
private static void MultipleSplitPdf(IResult result, List<string> fileNames, string directoryforsplit, out List<string> outFileNames)
{
if (fileNames.Count == 0)
{
result.SetError("no files!");
outFileNames = null;
return;
}
else
{
try
{
outFileNames = new List<string>();
if (FileManager.DeleteDir(result, directoryforsplit) == false)
return;
DirectoryInfo dirInfo;
if (FileManager.GetDirectory(result, directoryforsplit, out dirInfo) == false)
return;
int counter = 1;
foreach (string fileName in fileNames)
{
// we create a reader for a certain document
PdfReader reader = new PdfReader(fileName);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
//Console.WriteLine("There are " + n + " pages in the original file.");
int pagenumber = n;
//if (pagenumber == 1)
//{
// FileInfo a = new FileInfo(fileName);
// a.CopyTo(directoryforsplit);
//}
//else
for (int i = 1; i <= pagenumber; i++)
{
iTextSharp.text.Document document1 = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(i));
string splittedFilePath = directoryforsplit + "\Split" + counter.ToString() + ".pdf";
PdfWriter writer1 = PdfWriter.GetInstance(document1, new FileStream(splittedFilePath, FileMode.Create));
document1.Open();
PdfContentByte cb1 = writer1.DirectContent;
PdfImportedPage page;
int rotation;
document1.SetPageSize(reader.GetPageSizeWithRotation(i));
document1.NewPage();
page = writer1.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
counter++;
document1.Close();
}
}
// step 1: creation of a document-object
//iTextSharp.text.Document document2 = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(pagenumber));
// step 2: we create a writer that listens to the document
//PdfWriter writer2 = PdfWriter.GetInstance(document2, new FileStream(args[2], FileMode.Create));
// step 3: we open the document
//document2.Open();
//PdfContentByte cb2 = writer2.DirectContent;
// step 4: we add content
//while (i < pagenumber - 1)
//{
//}
//while (i < n)
//{
// i++;
// document2.SetPageSize(reader.GetPageSizeWithRotation(i));
// document2.NewPage();
// page = writer2.GetImportedPage(reader, i);
// rotation = reader.GetPageRotation(i);
// if (rotation == 90 || rotation == 270)
// {
// cb2.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
// }
// else
// {
// cb2.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
// }
// Console.WriteLine("Processed page " + i);
//}
//// step 5: we close the document
//document2.Close();
FileInfo[] wordFiles = dirInfo.GetFiles();
foreach (var item in wordFiles.OrderBy(i => i.LastWriteTime))
outFileNames.Add(dirInfo.FullName + "\" + item.Name);
}
catch (Exception e)
{
outFileNames = null;
result.SetError(e.Message + "\n" + e.StackTrace);
return;
}
}
}
- 你做错了!请阅读Chapter 6 of iText in Action and you'll discover that you should use
PdfStamper
orPdfCopy
, NOT a combination ofDocument
/PdfWriter
to split or merge documents. As documented, your code removes all interactive features! See How to merge documents correctly? and read the documentation! This book is available for free: The Best iText Questions on Whosebug。 - 您的 PDF 文档已数字签名。这意味着您无法从中提取页面并且您无法在不破坏数字签名的情况下将其与其他文档合并。阅读 how to add blank page in digitally signed pdf using java? 找出设计中的错误。您正在尝试做一些未完成的事情(一般而言,不限于 iText 或 iTextSharp)。