简体   繁体   English

使用iTextSharp合并不同方向的PDF

[英]Merging PDFs with different orientations with iTextSharp

I have two PDF files with different orientations (first document is A4 format and the second A4 landscape). 我有两个方向不同的PDF文件(第一个文档是A4格式,第二个文档是A4横向)。 I want to merge them but I need to preserve the original orientation of each page. 我想合并它们,但我需要保留每个页面的原始方向。

I tried with the rotation with this code: 我尝试使用以下代码进行旋转:

  float width = pdfImportedPage.Width;
  float height = pdfImportedPage.Height;

  if (width > height)
  {
      PdfDictionary pageDict = reader.GetPageN(documentPage);
      pageDict.Put(PdfName.ROTATE, new PdfNumber(270));
  }

After the rotation I call the AddPage method like this: 旋转后,我像这样调用AddPage方法:

copy.AddPage(pdfImportedPage);

But the result is an A4 format document with the second part with the text that goes out of the page. 但是结果是A4格式的文档,其第二部分的文字超出了页面。 For me is good if the text in the second part is horizontal but I need that also the orientation of the page will be as the original document (horizontal). 对我来说,如果第二部分中的文本是水平的,则对我来说是很好的,但我还需要页面的方向也应与原始文档一样(水平)。

I'm using iTextSharp version 5.5.13. 我正在使用iTextSharp 5.5.13版本。

I've just discovered that the problem was in another part of the code, after that, when I add the page number. 我刚刚发现问题出在代码的另一部分,之后,当我添加页码时。 By the way, a good way to preserve the page orientation is to use the SetPageSize and the NewPage methods, like this piece of code: 顺便说一句,保留页面方向的一种好方法是使用SetPageSize和NewPage方法,如这段代码:

for (int page = 1; page <= reader.NumberOfPages; page++)
{   
    copy.RotateContents = true;
    doc.SetPageSize(reader.GetPageSizeWithRotation(page));
    doc.NewPage();
    importedPage = copy.GetImportedPage(reader, page);  
    copy.AddPage(importedPage);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM