2010-08-26 38 views
9

Tôi đang sử dụng chức năng dưới đây để chia pdf thành hai.Xoay PDF trong C# bằng iTextSharp

Mặc dù nó đang chia nhỏ pdf, nhưng nội dung sẽ xuất hiện lộn ngược. Làm thế nào để xoay nó 180 độ.

Vui lòng trợ giúp. bên dưới là mã cho cùng một số

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

Trả lời

2

Tôi đã thử mã của bạn và nó hoạt động tốt cho tôi; các trang phân chia giữ nguyên định hướng ban đầu của chúng.

Giải pháp thay thế có thể xoay vòng 180 độ một cách rõ ràng.

Thay thế:

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

Với:

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

Nếu cuộc gọi của bạn để inputPdf.GetPageRotation(i) lợi nhuận 180 sau đó bạn có thể xử lý này trong báo cáo if rằng sau (sử dụng mã đề nghị của tôi cho xoay == 180).

6

Bạn nên thử điều này. Nó làm việc cho tôi:

   if (rotation == 90 || rotation == 270) 
       { 

        if (rotation == 90) 
        { 
         cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagenumber).Height); 
        } 
        if (rotation == 270) 
        { 
         cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(pagenumber).Width, 0); 

        } 

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

Tôi đã tìm thấy câu trả lời ở trên không xoay đúng cho cả 4 vòng xoay chính.

Dưới đây là mã của tôi để xử lý chính xác 0, 90, 180 và 270. Điều này đã được thử nghiệm với một PDF xoay theo từng hướng.

var pageRotation = reader.GetPageRotation(currentPageIndex); 
var pageWidth = reader.GetPageSizeWithRotation(currentPageIndex).Width; 
var pageHeight = reader.GetPageSizeWithRotation(currentPageIndex).Height; 
switch (pageRotation) 
{ 
    case 0: 
     writer.DirectContent.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0); 
     break; 

    case 90: 
     writer.DirectContent.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, pageHeight); 
     break; 

    case 180: 
     writer.DirectContent.AddTemplate(importedPage, -1f, 0, 0, -1f, pageWidth, pageHeight); 
     break; 

    case 270: 
     writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0); 
     break; 

    default: 
     throw new InvalidOperationException(string.Format("Unexpected page rotation: [{0}].", pageRotation)); 
} 
+2

Tôi có thể chia tỷ lệ bằng cách sử dụng văn bản đó với cùng một mã –

+2

+1: Rõ ràng 'trường hợp 0: 'có thể được rút ngắn thành' writer.DirectContent.AddTemplate (importedPage, 0, 0); 'Câu trả lời của bạn vừa giúp tôi giải quyết vấn đề quay một tệp PDF hiện có thêm 90, 180 hoặc 270 độ :) Sẽ rất tuyệt nếu các tham số đó được ghi lại ở đâu đó, vì tôi thu thập chúng là ma trận biến đổi (?). Cảm ơn nhiều. –

+0

Có một vấn đề khác với xoay vòng các trang đã xoay. Tôi đã sử dụng câu trả lời của bạn và thêm một câu trả lời với các thao tác kích thước trang/xoay bổ sung để cho phép xoay vòng các trang đã xoay. –

0

Một thay đổi nhỏ trong mã trên mã cũ

case 270: 
    writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0); 

mã mới

case 270: 
    writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageHeight, 0); 

Điều này sẽ khắc phục vấn đề với vòng quay 270 độ

+0

ở trên mã là hoàn toàn tốt với iTextSharp v 5.5. – Matthieu

2

@TimS 'Câu trả lời là rất gần hoàn hảo, và cung cấp các thông số chính xác cho AddTemplate, nhưng tôi cần phải thực hiện một vài bổ sung để cho phép 90, 180, 270 vòng quay của một PDF nơi trang đã có xoay từ 0, 90, 180 hoặc 270:

Giả sử một tham số của RotateFlipType rotateFlipType được truyền đến chức năng để xác định vòng quay (enum ích từ cuộc gọi GDI + RotateFlip):

iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent; 
iTextSharp.text.pdf.PdfImportedPage page; 
int rotation; 
int i = 0; 
while (i < pageCount) 
{ 
    i++; 
    var pageSize = reader.GetPageSizeWithRotation(i); 

    // Pull in the page from the reader 
    page = writer.GetImportedPage(reader, i); 

    // Get current page rotation in degrees 
    rotation = pageSize.Rotation; 

    // Default to the current page size 
    iTextSharp.text.Rectangle newPageSize = null; 

    // Apply our additional requested rotation (switch height and width as required) 
    switch (rotateFlipType) 
    { 
     case RotateFlipType.RotateNoneFlipNone: 
      newPageSize = new iTextSharp.text.Rectangle(pageSize); 
      break; 
     case RotateFlipType.Rotate90FlipNone: 
      rotation += 90; 
      newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation); 
      break; 
     case RotateFlipType.Rotate180FlipNone: 
      rotation += 180; 
      newPageSize = new iTextSharp.text.Rectangle(pageSize.Width, pageSize.Height, rotation); 
      break; 
     case RotateFlipType.Rotate270FlipNone: 
      rotation += 270; 
      newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation); 
      break; 
    } 

    // Cap rotation into the 0-359 range for subsequent check 
    rotation %= 360; 

    document.SetPageSize(newPageSize); 
    document.NewPage(); 

    // based on the rotation write out the page dimensions 
    switch (rotation) 
    { 
     case 0: 
      cb.AddTemplate(page, 0, 0); 
      break; 
     case 90: 
      cb.AddTemplate(page, 0, -1f, 1f, 0, 0, newPageSize.Height); 
      break; 
     case 180: 
      cb.AddTemplate(page, -1f, 0, 0, -1f, newPageSize.Width, newPageSize.Height); 
      break; 
     case 270: 
      cb.AddTemplate(page, 0, 1f, -1f, 0, newPageSize.Width, 0); 
      break; 
     default: 
      throw new System.Exception(string.Format("Unexpected rotation of {0} degrees", rotation)); 
      break; 
    } 
} 

Hy vọng rằng điều này sẽ giúp người khác có nhu cầu để sửa chữa sự quay của PDF đến. Đã cho tôi 2 ngày để hoàn thiện nó.

+1

Mã của bạn hoạt động như một sự quyến rũ. Cảm ơn nhiều. Bạn đã lưu lại ngày của tôi. – RolandDeschain

+0

@RolandDeschain: Vui vì nó đã giúp :) –

Các vấn đề liên quan