是文档doc.Add(PDF)可能吗? [英] Is Document doc.Add(pdf) possible?

查看:464
本文介绍了是文档doc.Add(PDF)可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图PDF添加到其它PDF。文档doc.Add(IElement)是我知道该怎么办,我有麻烦添加PDF格式。我尝试添加图像和工作。如何将PDF文件添加到我的文档?

  iTextSharp.text.Image IMG;
的foreach(在列表VAR BUF){
   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);
}


解决方案

您不能使用 Document.Add(),但它仍然是pretty容易。首先,你需要创建一个 PdfReader 对象读取您的源文件。然后使用 PdfWriter 您的目的地文件并调用它的 GetImportedPage(读卡器,PAGENUMBER)方法,你想在每一页上进口。这会给你)一个 PdfImportedPage 对象,您可以传递给 PdfWriter.DirectContent.AddTemplate(

下面是一个完整的工作C#2010 WinForms应用程序针对iTextSharp的5.1.1.0,展示了所有这些步骤。首先,它创建了一个样本文件(文件1 ),然后创建第二个文件(文件2 ),并增加第一个文件它。它做一些额外的东西,也表现出了一些优势情况下,特别是如何处理旋转的页面。见code意见的具体细节。

 使用系统;
使用System.Text;
使用System.Windows.Forms的;
使用System.IO;
使用iTextSharp.text;
使用iTextSharp.text.pdf;命名空间WindowsFormsApplication1
{
    公共部分Form1类:表格
    {
        公共Form1中()
        {
            的InitializeComponent();
        }        私人无效Form1_Load的(对象发件人,EventArgs的发送)
        {
            //我们将创建两个文件的名称
            字符串文件1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),File1.pdf);
            字符串的文件2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),File2.pdf);            //创建一个测试文件合并到第二个文件
            使用(的FileStream FS =新的FileStream(文件1,FileMode.Create,FileAccess.Write,FileShare.None))
            {
                //我们创建这个景观表现出一定的检查,需要稍后完成
                使用(DOC文档=新的文件(PageSize.TABLOID.Rotate()))
                {
                    使用(PdfWriter作家= PdfWriter.GetInstance(DOC,FS))
                    {
                        doc.Open();
                        doc.Add(新段(文件1));
                        doc.Close();
                    }
                }
            }            //创建我们的第二个文件
            使用(的FileStream FS =新的FileStream(文件2,FileMode.Create,FileAccess.Write,FileShare.None))
            {
                //创建这个作为一个普通美国信纸画像尺寸的文档
                使用(DOC文档=新的文件(PageSize.LETTER))
                {
                    使用(PdfWriter作家= PdfWriter.GetInstance(DOC,FS))
                    {
                        doc.Open();                        doc.Add(新段(文件2));                        //创建一个PdfReader阅读我们的第一个文件
                        PdfReader R =新PdfReader(文件1);                        //存储的页数
                        INT = PAGECOUNT r.NumberOfPages;                        //变量,这将在下面的循环中设置
                        iTextSharp.text.Rectangle RECT;
                        PdfImportedPage小鬼;                        //循环源文档中的每一页,请记住,网页的索引开始一个一个而不是零
                        的for(int i = 1; I< =页页次;我++)
                        {
                            //获取页面的尺寸和旋转
                            矩形= r.GetPageSizeWithRotation(ⅰ);                            //获取实际的页面
                            小鬼= writer.GetImportedPage(R,I);                            //这两个命令必须按以下顺序发生
                            //首先改变默认页面大小来匹配当前页面的大小
                            doc.SetPageSize(RECT);
                            //然后添加一个新的空白页面,我们将与我们的实际页面填写
                            doc.NewPage();                            //如果该文档已被旋转一个扭转左或右(90度),那么我们需要以不同的方式添加一点点
                            如果(rect.Rotation == || 90 == rect.Rotation 270)
                            {
                                //添加页面占旋转
                                writer.DirectContent.AddTemplate(IMP,0,-1,1,0,0,rect.Height);
                            }
                            其他
                            {
                                //通常添加的页面
                                writer.DirectContent.AddTemplate(IMP,0,0);
                            }                        }                        doc.Close();
                    }
                }
            }
            this.Close();
        }
    }
}

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

解决方案

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

这篇关于是文档doc.Add(PDF)可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆