2013-11-28 199 views
2

我研究了這一點,並試圖旋轉單頁PDF的內容。我可以旋轉頁面90,180或270度。我不想旋轉頁面,而是旋轉內容。旋轉PDF頁面的內容,而不是實際的頁面

這裏是到目前爲止,我已經適應了方法:

public static byte[] RotatePdf(byte[] fileBytes, int degreesClockwise) 
{ 
    if (degreesClockwise % 90 != 0) 
     throw new ApplicationException(string.Format("degreesClockwise must be 0, 90, 180, 360: {0}", degreesClockwise)); 

    PdfReader reader = new PdfReader(fileBytes); 
    using (var fs = new MemoryStream()) 
    { 
     PdfStamper stamper = new PdfStamper(reader, fs); 

     PdfDictionary pageDict = reader.GetPageN(1); 
     int desiredRotation = degreesClockwise; // x degrees clockwise from what it is now 
     PdfNumber rotation = pageDict.GetAsNumber(PdfName.ROTATE); 
     if (rotation != null) 
     { 
      desiredRotation += rotation.IntValue; 
      desiredRotation %= 360; // must be 0, 90, 180, or 270 
     } 
     pageDict.Put(PdfName.ROTATE, new PdfNumber(desiredRotation)); 

     stamper.Close(); 

     return fs.ToArray(); 
    } 
} 

任何建議,將不勝感激。

+0

嗯,這是怎麼代碼不完整?即它現在取得了什麼成就?這與你想要的有何不同? – millimoose

+0

*我不想旋轉頁面,而是內容* - 有什麼區別? – mkl

+0

@millimoose代碼旋轉頁面方向和內容。我想要的方向保持不變,但旋轉90度的內容。 – Seany84

回答

1

我使用PdfSharp庫完成了代碼,因爲我找不到iTextSharp的任何示例或答案。

這裏是我用來實現我想要的東西代碼:

// Create the output document 
PdfDocument outputDocument = new PdfDocument(); 

// Show single pages 
// (Note: one page contains two pages from the source document) 
outputDocument.PageLayout = PdfPageLayout.SinglePage; 

// Open the external document as XPdfForm object 
XPdfForm form = XPdfForm.FromFile(filename); 

for (int i = 0; i < form.PageCount; i++) 
{ 
    // Add a new page to the output document 
    PdfPage page = outputDocument.AddPage(); 
    page.Orientation = PageOrientation.Landscape; 
    double width = page.Width; 
    double height = page.Height; 

    int rotate = page.Elements.GetInteger("/Rotate"); 

    XGraphics gfx = XGraphics.FromPdfPage(page); 

    XRect box = new XRect(0, 0, width, height * 2); 
    // Draw the page identified by the page number like an image 
    gfx.DrawImage(form, box); 
} 

// Save the document... 
filename = "RotatedAndStretched_tempfile.pdf"; 
outputDocument.Save(filename); 
相關問題