itextsharp 合并成一个横纵交替的pdf文件

时间:2021-05-04 05:22:36
有两个pdf文件,横纵向不同,如何用itextsharp将他们合并啊,为什么创建一个docment只能创建一个打印方向的,例如: Document doc = new Document();
            Document hDoc = new Document(PageSize.A4.Rotate());

6 个解决方案

#1


找到了方法了

#2


引用 楼主 wangkiml 的回复:
有两个pdf文件,横纵向不同,如何用itextsharp将他们合并啊,为什么创建一个docment只能创建一个打印方向的,例如: Document doc = new Document();
            Document hDoc = new Document(PageSize.A4.Rotate());




用的什么方法处理的,求教,谢谢!

#3


也遇到这样的问题,楼主是怎么解决的,求教,谢谢

#4


Document doc = new Document();
            //Document hDoc = new Document(PageSize.A4.Rotate());
            PdfReader reader;
            iTextSharp.text.Rectangle re;
            PdfDictionary pd;
            FileStream fs = new FileStream(objectPDFPath + @"\" + objectPDF, FileMode.Create);
            PdfWriter writer = PdfWriter.GetInstance(doc, fs);
            try
            {
                doc.Open();
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage newPage;

                if (doc.IsOpen())
                {
                    foreach (FileInfo file in files)
                    {
                        reader = new PdfReader(file.FullName);
                        int iPageNum = reader.NumberOfPages;
                        for (int j = 1; j <= iPageNum; j++)
                        { 
                            //获取Reader的pdf页的打印方向
                            re = reader.GetPageSize(reader.GetPageN(j));
                            //设置合并pdf的打印方向
                            doc.SetPageSize(re);
                            doc.NewPage();

#5


很有用,解决了头疼的问题,感谢!!

#6


我也是这个问题,没有早看到这个帖子。不过我是这样合并pdf的,原理应该一样,稍微改动了一下,正在测试
byte[] btsmjhs = getXmjhs(sbbh, url, context, out n);
                if (btsmjhs != null)
                {
                    PdfReader reader2 = new PdfReader(btsmjhs);
                    int iPageNum2 = reader2.NumberOfPages;
                    for (int j = 1; j <= iPageNum2; j++)
                    {
                        Rectangle rect = reader2.GetPageSize(reader2.GetPageN(j));
                        if (rect.Width > rect.Height)
                            n = 1;
                        else
                            n = 0;
                        if (n == 1)
                        {
                            stamp.InsertPage(number, PageSize.A4.Rotate());
                        }
                        else if (n == 0)
                        {
                            stamp.InsertPage(number, PageSize.A4);
                        }
                        PdfContentByte under = stamp.GetUnderContent(number);
                        under.AddTemplate(stamp.GetImportedPage(reader2, j), 1, 0, 0, 1, 0, 0);
                        number++;
                        count++;
                    }
                }

#1


找到了方法了

#2


引用 楼主 wangkiml 的回复:
有两个pdf文件,横纵向不同,如何用itextsharp将他们合并啊,为什么创建一个docment只能创建一个打印方向的,例如: Document doc = new Document();
            Document hDoc = new Document(PageSize.A4.Rotate());




用的什么方法处理的,求教,谢谢!

#3


也遇到这样的问题,楼主是怎么解决的,求教,谢谢

#4


Document doc = new Document();
            //Document hDoc = new Document(PageSize.A4.Rotate());
            PdfReader reader;
            iTextSharp.text.Rectangle re;
            PdfDictionary pd;
            FileStream fs = new FileStream(objectPDFPath + @"\" + objectPDF, FileMode.Create);
            PdfWriter writer = PdfWriter.GetInstance(doc, fs);
            try
            {
                doc.Open();
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage newPage;

                if (doc.IsOpen())
                {
                    foreach (FileInfo file in files)
                    {
                        reader = new PdfReader(file.FullName);
                        int iPageNum = reader.NumberOfPages;
                        for (int j = 1; j <= iPageNum; j++)
                        { 
                            //获取Reader的pdf页的打印方向
                            re = reader.GetPageSize(reader.GetPageN(j));
                            //设置合并pdf的打印方向
                            doc.SetPageSize(re);
                            doc.NewPage();

#5


很有用,解决了头疼的问题,感谢!!

#6


我也是这个问题,没有早看到这个帖子。不过我是这样合并pdf的,原理应该一样,稍微改动了一下,正在测试
byte[] btsmjhs = getXmjhs(sbbh, url, context, out n);
                if (btsmjhs != null)
                {
                    PdfReader reader2 = new PdfReader(btsmjhs);
                    int iPageNum2 = reader2.NumberOfPages;
                    for (int j = 1; j <= iPageNum2; j++)
                    {
                        Rectangle rect = reader2.GetPageSize(reader2.GetPageN(j));
                        if (rect.Width > rect.Height)
                            n = 1;
                        else
                            n = 0;
                        if (n == 1)
                        {
                            stamp.InsertPage(number, PageSize.A4.Rotate());
                        }
                        else if (n == 0)
                        {
                            stamp.InsertPage(number, PageSize.A4);
                        }
                        PdfContentByte under = stamp.GetUnderContent(number);
                        under.AddTemplate(stamp.GetImportedPage(reader2, j), 1, 0, 0, 1, 0, 0);
                        number++;
                        count++;
                    }
                }