简体   繁体   中英

Digital signature is not copied with iTextSharp

I have this method that split a pdf of many pages into many files for each page. I noticed that if a pdf contains a digital signature or a shape(rectangle for example) iTextsharp don't copy those things into the splitted file. Are there any reason about it? Am I missing something here?

 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. You are doing it wrong! Please read 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 StackOverflow .
  2. Your PDF document is digitally signed . This means that you can not extract pages from it and you can not merge it with another document without breaking the digital signature. Read how to add blank page in digitally signed pdf using java? to find out the error in your design. You are trying to do something that is not done (in general, not limited to iText or iTextSharp).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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