2017-03-13 16 views
0

鑑於任何現有的PDF具有許多不同的頁面大小和方向,我的程序必須在頁面的四個邊緣中的每一個的中心放置一行文本,併爲該頁面邊緣旋轉。我在C#中使用PDFsharp。PDFsharp垂直文本在頁面的一面

對於頁面的底部,我不需要任何文字旋轉和它的偉大工程:

// Font for added margin text. 
XFont xf = new XFont("Arial", 10, XFontStyle.Bold); 

// String format to place at bottom center. 
XStringFormat sf = new XStringFormat(); 
sf.Alignment = XStringAlignment.Center; 
sf.LineAlignment = XLineAlignment.Far; 

using (PdfDocument pdfdOut = new PdfDocument()) 
{ 
using (PdfDocument pdfdIn = PdfReader.Open(sInPdfPath, PdfDocumentOpenMode.Import)) 
    for (int iInPageNo = 0; iInPageNo < pdfdIn.PageCount; iInPageNo++) 
    { 
    PdfPage pdfpOut = pdfdOut.AddPage(pdfdIn.Pages[iInPageNo]); 
    using (XGraphics xg = XGraphics.FromPdfPage(pdfpOut, XGraphicsPdfPageOptions.Append)) 
    { 
    XRect xr = pdfpOut.MediaBox.ToXRect(); 
    xg.DrawString("Bottom Centered Text", xf, XBrushes.Red, xr, sf); 
    } 
    } // for iInPageNo, using pdfdIn 
// ...save the output PDF here... 
} // using pdfdOut 

當我試圖將集中在頁面的左側豎排文字,指示燈熄滅肖像網頁頁面,太遠從邊緣景觀網頁:

using (XGraphics xg = XGraphics.FromPdfPage(pdfpOut, XGraphicsPdfPageOptions.Append)) 
{ 
// Rotate graphics 90 degrees around the center of the page. 
xg.RotateAtTransform(90, new XPoint(xg.PageSize.Width/2d, xg.PageSize.Height/2d)); 
XRect xr = pdfpOut.MediaBox.ToXRect(); 
xg.DrawString("Left Centered Text", xf, XBrushes.Red, xr, sf); 
} 

我的理由是,XRect需要有其寬度和高度交換,但我從來沒有看到我的左文本居中,當我做到這一點:

XRect xr = New XRect(pdfpOut.MediaBox.ToXRect().Height, pdfpOut.MediaBox.ToXRect().Width); 

MediaBox與PageSize具有相同的尺寸。

對於測試,我已經顯示了XRect,知道發生了什麼。但我就是不能讓它看起來與旋轉一樣,不旋轉:

xg.DrawRectangle(XPens.Red, xr); 

回答

0

隨着PDFsharp高達1.50版本公測3B開有橫向格式頁面的PDF文件時,有一個已知的問題。我建議刪除PDFsharp論壇上此帖中顯示的三行:
http://forum.pdfsharp.net/viewtopic.php?p=9591#p9591
只需在internal PdfPage(PdfDictionary dict)內不做任何事情。我建議下載源代碼包,在您的解決方案中引用C#項目,並對代碼進行更改。

當交換矩形的寬度和高度時,請確保您還在RotateAtTransform的調用中更新了旋轉中心。