使用iTextSharp的在C#中旋转PDF [英] Rotating PDF in C# using iTextSharp

查看:264
本文介绍了使用iTextSharp的在C#中旋转PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的函数将PDF拆分成两部分。

虽然它的劈裂PDF格式,内容是出现倒挂。我如何通过180度旋转。

请帮忙。下面是code为同一

 私有静态无效ExtractPages(字符串INPUTFILE,串OUTPUTFILE,
  INT开始,诠释完)
     {
         //获取输入文档
         PdfReader inputPdf =新PdfReader(INPUTFILE);         //检索的页的总数
         INT = PAGECOUNT inputPdf.NumberOfPages;         如果(完<启动||末>页页次)
         {
             结束=页页次;
         }         //加载输入文档
         文档inputDoc =
             新的文档(inputPdf.GetPageSizeWithRotation(1));         //创建文件流
         使用(的FileStream FS =新的FileStream(OUTPUTFILE,FileMode.Create))
         {
             //创建输出作家
             PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc,FS);
             inputDoc.Open();             PdfContentByte CB1 = outputWriter.DirectContent;             从输入//副本页面输出文档
             的for(int i =启动; I< =结束;我++)
             {
                 inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(1));
                 inputDoc.NewPage();                 PdfImportedPage页=
                     outputWriter.GetImportedPage(inputPdf,I);
                 INT旋转= inputPdf.GetPageRotation(ⅰ);
                 如果(旋转== || 90旋转== 270)
                 {
                     cb1.AddTemplate(页面,0,-1f,1F,0,0,
                         inputPdf.GetPageSizeWithRotation㈠.Height);                 }
                 其他
                 {
                     cb1.AddTemplate(页面,1F,0,0,1F,0,0);
                 }             }             inputDoc.Close();
         }
     }


解决方案

我想你的code和它为我工作得很好;拆分页面保留着原来的方向。

一个解决办法可能是明确旋转页面180度。

替换为:

  cb1.AddTemplate(页,1F,0,0,1F,0,0);

使用:

  cb1.AddTemplate(页,-1f,0,0,-1f,
                inputPdf.GetPageSizeWithRotation(我).WIDTH,
                inputPdf.GetPageSizeWithRotation㈠.Height);

如果您的来电 inputPdf.GetPageRotation(我)返回1​​80,那么你可以在如果语句处理这随后(采用我建议的code旋转== 180)。

I am using the below function to split the pdf into two.

Though it is spliting the pdf, the content is appearing upside down. How do I rotate it by 180 degrees.

Please help. below is the code for the same

private static void ExtractPages(string inputFile, string outputFile,
  int start, int end)
     {
         // get input document
         PdfReader inputPdf = new PdfReader(inputFile);

         // retrieve the total number of pages
         int pageCount = inputPdf.NumberOfPages;

         if (end < start || end > pageCount)
         {
             end = pageCount;
         }

         // load the input document
         Document inputDoc =
             new Document(inputPdf.GetPageSizeWithRotation(1));

         // create the filestream
         using (FileStream fs = new FileStream(outputFile, FileMode.Create))
         {
             // create the output writer
             PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs);
             inputDoc.Open();

             PdfContentByte cb1 = outputWriter.DirectContent;

             // copy pages from input to output document
             for (int i = start; i <= end; i++)
             {
                 inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(1));
                 inputDoc.NewPage();

                 PdfImportedPage page =
                     outputWriter.GetImportedPage(inputPdf, i);
                 int rotation = inputPdf.GetPageRotation(i);


                 if (rotation == 90 || rotation == 270)
                 {
                     cb1.AddTemplate(page, 0, -1f, 1f, 0, 0,
                         inputPdf.GetPageSizeWithRotation(i).Height);

                 }
                 else
                 {
                     cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                 }

             }

             inputDoc.Close();
         }
     }

解决方案

I tried your code and it worked fine for me; split pages kept their original orientation.

A workaround might be to explicitly rotate your pages 180 degrees.

Replace:

cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); 

With:

cb1.AddTemplate(page, -1f, 0, 0, -1f, 
                inputPdf.GetPageSizeWithRotation(i).Width, 
                inputPdf.GetPageSizeWithRotation(i).Height);

If your call to inputPdf.GetPageRotation(i) returns 180 then you can handle this in the if statement that follows (using my suggested code for rotation == 180).

这篇关于使用iTextSharp的在C#中旋转PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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