文档 doc.Add(pdf) 可能吗?

发布于 2024-12-17 13:56:59 字数 402 浏览 0 评论 0原文

我正在尝试将 pdf 添加到另一个 pdf。文档 doc.Add(IElement) 是我知道要做的事情,但我在添加 pdf 时遇到问题。我尝试添加图像并且成功了。如何将 pdf 文件添加到我的文档中?

iTextSharp.text.Image img;
foreach (var buf in List) {
   myDoc.NewPage();
   img = iTextSharp.text.Image.GetInstance(buf);
   img.ScaleToFit(612f, 792f);
   img.Alignment = iTextSharp.text.Image.ALIGN_CENTER | iTextSharp.text.Image.ALIGN_MIDDLE;
   myDoc.Add(img);
}

I am trying to add pdf to another pdf. Document doc.Add(IElement) is what I know to do and I am having trouble adding the pdf. I tried adding image and that worked. How do I add a pdf file to my document?

iTextSharp.text.Image img;
foreach (var buf in List) {
   myDoc.NewPage();
   img = iTextSharp.text.Image.GetInstance(buf);
   img.ScaleToFit(612f, 792f);
   img.Alignment = iTextSharp.text.Image.ALIGN_CENTER | iTextSharp.text.Image.ALIGN_MIDDLE;
   myDoc.Add(img);
}

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

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

发布评论

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

评论(1

々眼睛长脚气 2024-12-24 13:56:59

您不能使用 Document.Add() 但它仍然非常简单。首先,您需要创建一个 PdfReader 对象来读取源文档。然后使用目标文档的 PdfWriter 并在要导入的每个页面上调用其 GetImportedPage(reader, pageNumber) 方法。这将为您提供一个 PdfImportedPage 对象,您可以将其传递给 PdfWriter.DirectContent.AddTemplate()

下面是一个针对 iTextSharp 5.1.1.0 的完整工作 C# 2010 WinForms 应用程序,展示了所有这些步骤。首先,它创建一个示例文件 (File1),然后创建第二个文件 (File2) 并向其中添加第一个文件。它还做了一些额外的事情,以展示一些边缘情况,特别是如何处理旋转页面。具体细节请参见代码注释。

using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //The names of the two files that we'll create
            string File1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File1.pdf");
            string File2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File2.pdf");

            //Create a test file to merge into the second file
            using (FileStream fs = new FileStream(File1, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //We'll create this one landscape to show some checks that need to be done later
                using (Document doc = new Document(PageSize.TABLOID.Rotate()))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                    {
                        doc.Open();
                        doc.Add(new Paragraph("File 1"));
                        doc.Close();
                    }
                }
            }

            //Create our second file
            using (FileStream fs = new FileStream(File2, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //Create this one as a regular US Letter portrait sized document
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                    {
                        doc.Open();

                        doc.Add(new Paragraph("File 2"));

                        //Create a PdfReader to read our first file
                        PdfReader r = new PdfReader(File1);

                        //Store the number of pages
                        int pageCount = r.NumberOfPages;

                        //Variables which will be set in the loop below
                        iTextSharp.text.Rectangle rect;
                        PdfImportedPage imp;

                        //Loop through each page in the source document, remember that page indexes start a one and not zero
                        for (int i = 1; i <= pageCount; i++)
                        {
                            //Get the page's dimension and rotation
                            rect = r.GetPageSizeWithRotation(i);

                            //Get the actual page
                            imp = writer.GetImportedPage(r, i);

                            //These two commands must happen in this order
                            //First change the "default page size" to match the current page's size
                            doc.SetPageSize(rect);
                            //then add a new blank page which we'll fill with our actual page
                            doc.NewPage();

                            //If the document has been rotated one twist left or right (90 degrees) then we need to add it a little differently
                            if (rect.Rotation == 90 || rect.Rotation == 270)
                            {
                                //Add the page accounting for the rotation
                                writer.DirectContent.AddTemplate(imp, 0, -1, 1, 0, 0, rect.Height);
                            }
                            else
                            {
                                //Add the page normally
                                writer.DirectContent.AddTemplate(imp, 0, 0);
                            }

                        }

                        doc.Close();
                    }
                }
            }
            this.Close();
        }
    }
}

You can't use Document.Add() but it's still pretty easy. First you need to create a PdfReader object to read your source document. Then use the PdfWriter of your destination document and call its GetImportedPage(reader, pageNumber) method on each page that you want to import. This will give you a PdfImportedPage object that you can pass to PdfWriter.DirectContent.AddTemplate().

Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.1.0 that shows off all of these steps. First it creates a sample file (File1) and then creates a second file (File2) and adds the first file to it. It does some extra things, too, to show off some edge cases, specifically how to handle rotated pages. See the code comments for specific details.

using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //The names of the two files that we'll create
            string File1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File1.pdf");
            string File2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File2.pdf");

            //Create a test file to merge into the second file
            using (FileStream fs = new FileStream(File1, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //We'll create this one landscape to show some checks that need to be done later
                using (Document doc = new Document(PageSize.TABLOID.Rotate()))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                    {
                        doc.Open();
                        doc.Add(new Paragraph("File 1"));
                        doc.Close();
                    }
                }
            }

            //Create our second file
            using (FileStream fs = new FileStream(File2, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //Create this one as a regular US Letter portrait sized document
                using (Document doc = new Document(PageSize.LETTER))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                    {
                        doc.Open();

                        doc.Add(new Paragraph("File 2"));

                        //Create a PdfReader to read our first file
                        PdfReader r = new PdfReader(File1);

                        //Store the number of pages
                        int pageCount = r.NumberOfPages;

                        //Variables which will be set in the loop below
                        iTextSharp.text.Rectangle rect;
                        PdfImportedPage imp;

                        //Loop through each page in the source document, remember that page indexes start a one and not zero
                        for (int i = 1; i <= pageCount; i++)
                        {
                            //Get the page's dimension and rotation
                            rect = r.GetPageSizeWithRotation(i);

                            //Get the actual page
                            imp = writer.GetImportedPage(r, i);

                            //These two commands must happen in this order
                            //First change the "default page size" to match the current page's size
                            doc.SetPageSize(rect);
                            //then add a new blank page which we'll fill with our actual page
                            doc.NewPage();

                            //If the document has been rotated one twist left or right (90 degrees) then we need to add it a little differently
                            if (rect.Rotation == 90 || rect.Rotation == 270)
                            {
                                //Add the page accounting for the rotation
                                writer.DirectContent.AddTemplate(imp, 0, -1, 1, 0, 0, rect.Height);
                            }
                            else
                            {
                                //Add the page normally
                                writer.DirectContent.AddTemplate(imp, 0, 0);
                            }

                        }

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