iText 使用裁剪框合并 PDF

发布于 2024-11-19 15:41:30 字数 2976 浏览 4 评论 0原文

有谁知道如何使用iText合并两个pdf(其中一个有cropbox,另一个没有,并且它们具有不同的页面大小)?

这是我用于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的页面上的内容位置错误),有人有更好的代码将内容放在正确的位置吗?

Does any know how to merge two pdf (one of them have cropbox and the other don't have, and they have different page size) using iText?

here is the code, that I use for PDF merge:

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;
    }

But it seems not handling cropbox difference between pages (content mis-position on page that have cropbox) , anyone have a better code to put the content in the right position?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那请放手 2024-11-26 15:41:30

您的做法很困难:

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) );
  }
}

这将复制页面注释以及页面内容。请注意,它不会复制这些注释可能需要正常运行的文档级内容(例如表单字段验证入口点的文档脚本)。

无论页面框大小或旋转如何,它都会逐字复制原始页面。

You're doing it the hard way:

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) );
  }
}

That'll copy page annotations as well as page contents. Note that it Will Not copy document-level stuff those annotations might need to function properly (document script for a form fields' validation entry point for example).

And it'll copy the original page verbatim, regardless of various page box sizes or rotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文