【问题标题】:Rotate PDF without truncating contents using PDFsharp使用 PDFsharp 旋转 PDF 而不截断内容
【发布时间】:2015-03-01 22:46:35
【问题描述】:

旋转到纵向视图后,如何确保所有内容都可见(在页面范围内)?

PDFsharp 在旋转 +90 度时截断我的页面,但在旋转 -90 度时不会截断。

PdfDocument outputDocument = new PdfDocument();
XPdfForm old = XPdfForm.FromFile(in_filename);
PdfPage newP = outputDocument.AddPage();

if (old.Page.Orientation == PageOrientation.Landscape)
{
    old.Page.Rotate= (old.Page.Rotate - 90) % 360;
    old.Page.Orientation = PageOrientation.Portrait;
}

newP.Height = old.Page.Height;
newP.Width = old.Page.Width;

// Get a graphics object for page1
XGraphics gfx = XGraphics.FromPdfPage(newP);

// Draw the page identified by the page number like an image
gfx.DrawImage(old, new XRect(0, 0, old.PointWidth, old.PointHeight));

以上适用于一些 pdf 测试用例,但我怀疑这是巧合/运气

我正在使用 PDFsharp 1.50 测试版。

【问题讨论】:

    标签: c# pdf rotation pdfsharp portrait


    【解决方案1】:

    PDFsharp 1.50 beta 存在一个关于导入旋转 PDF 的已知问题。该问题仍在调查中。

    PDF 文件有许多不同的变体,因此很难确保代码在所有情况下都能正常工作。

    【讨论】:

    【解决方案2】:

    我最终做了以下事情: (注意,这只适用于横向到纵向)

            var output = new PdfDocument();
            var outputPage = output.AddPage();            
    
            using (var stream = new MemoryStream(Convert.FromBase64String(pdfBase64String)))
            {
                using (var input = XPdfForm.FromStream(stream))
                {
                    outputPage.Height = input.PointWidth;
                    outputPage.Width = input.PointHeight;
    
                    using (var graphics = XGraphics.FromPdfPage(outputPage))
                    {
                        graphics.RotateAtTransform(90, new XPoint(input.PointHeight / 2, input.PointHeight / 2));
                        graphics.DrawImage(input, new XRect(0, 0, input.PointWidth, input.PointHeight));
    
                    }
    
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      相关资源
      最近更新 更多