C# iText将PDF与cropbox合并

C# iText将PDF与cropbox合并,c#,java,pdf,pdf-generation,itext,C#,Java,Pdf,Pdf Generation,Itext,有人知道如何合并两个pdf文件吗?一个有cropbox,另一个没有,并且使用iText时页面大小不同 以下是我用于PDF合并的代码: public bool MergeFiles(string destinationFile, string[] sourceFiles) { bool bSucess = true; if (System.IO.File.Exists(destinationFile)) System.IO.File

有人知道如何合并两个pdf文件吗?一个有cropbox,另一个没有,并且使用iText时页面大小不同

以下是我用于PDF合并的代码:

public bool MergeFiles(string destinationFile, string[] sourceFiles)
    {
        bool bSucess = true;

        if (System.IO.File.Exists(destinationFile))
            System.IO.File.Delete(destinationFile);

        string[] sSrcFile;
        sSrcFile = new string[2];


        string[] arr = new string[2];
        for (int i = 0; i <= sourceFiles.Length - 1; i++)
        {
            if (sourceFiles[i] != null)
            {
                if (sourceFiles[i].Trim() != "")
                    arr[i] = sourceFiles[i].ToString();
            }
        }

        if (arr != null)
        {

            sSrcFile = new string[2];

            for (int ic = 0; ic <= arr.Length - 1; ic++)
            {
                sSrcFile[ic] = arr[ic].ToString();
            }
        }

        string sPDFPath = Server.MapPath(@"pdftemp/");
        Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();


        try
        {
            int f = 0;

            PdfReader reader = new PdfReader(sSrcFile[f]);
            int n = reader.NumberOfPages;
            Console.WriteLine("There are " + n + " pages in the original file.");
            Document document = new Document(PageSize.A4);

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
            PdfDate st = new PdfDate(DateTime.Today);


            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage page;

            int rotation;
            while (f < sSrcFile.Length)
            {
                int i = 0;
                while (i < n)
                {
                    i++;

                    document.SetPageSize(PageSize.A4);
                    document.NewPage();
                    page = writer.GetImportedPage(reader, i);

                    rotation = reader.GetPageRotation(i);
                    if (rotation == 90 || rotation == 270)
                    {
                        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                    }
                    else
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                    Console.WriteLine("Processed page " + i);
                }

                f++;
                if (f < sSrcFile.Length)
                {
                    reader = new PdfReader(sSrcFile[f]);
                    n = reader.NumberOfPages;
                    Console.WriteLine("There are " + n + " pages in the original file.");
                }
            }

            document.Close();

        }
        catch (Exception e)
        {
            bSucess = false;
        }

        return bSucess;
    }

但似乎无法处理cropbox页面之间的差异,在有cropbox的页面上,内容位置错误,是否有更好的代码将内容放在正确的位置?

您的做法很难:

PdfReader reader = null;
PdfCopy copier = new PdfCopy(outputStream);
String paths[] = ...;
for (String path : paths) {
  reader = new PdfReader(path);
  for (int pageNum = 1; pageNum <= reader.getNumberOfPages(); ++pageNum) {
    copier.addPage(copier.getImportedPage(reader, pageNum) );
  }
}
这将复制页面注释以及页面内容。请注意,它不会复制文档级别的内容,例如,这些注释可能需要为表单字段的验证入口点正确运行文档脚本

它将逐字复制原始页面,而不考虑页面框的大小或旋转