繁体   English   中英

数字签名未使用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;
        }
    }

}
  1. 你做错了! 请阅读iText in Action的第6章 ,您会发现应该使用PdfStamperPdfCopy ,而不是使用Document / PdfWriter的组合来拆分或合并文档。 如记录所示,您的代码将删除所有交互式功能! 请参阅如何正确合并文档? 并阅读文档! 本书可免费获得: StackOverflow上的最佳iText问题
  2. 您的PDF文档已经过数字签名 这意味着您不能从中提取页面, 也不能在不破坏数字签名的情况下将其与另一个文档合并 阅读如何使用Java在数字签名的pdf中添加空白页? 找出设计中的错误。 您正在尝试做未完成的事情(通常不限于iText或iTextSharp)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM