如何使用syncfusion pdf获取剩余页面大小

问题描述 投票:0回答:2

我正在使用Xamarin表单的synfusion pdf插件创建pdf,如果我打印pdf,那么页面上有很多剩余空间,那么我如何使用syncfusion pdf获取pdf页面的剩余空间会发生什么

c# xamarin xamarin.forms syncfusion
2个回答
0
投票

我们可以使用PdfLayoutResult获取PDF文档页面中的空白区域。请参阅下面的代码片段了解更多详细信息,

C#:

//Create a text element with the text and font
PdfTextElement textElement = new PdfTextElement(text, font);
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Layout = PdfLayoutType.Paginate;

//Draw the paragraph
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0, 
page.GetClientSize().Width, page.GetClientSize().Height), layoutFormat);

//Get the blank space of PDF using PdfLayoutResult and page height 
float blankSpaceHeight = page.GetClientSize().Height - result.Bounds.Bottom;

我们已经创建了相同的示例,可以从下面的链接下载,

获取PDF文档剩余页面大小的示例


0
投票

我们可以从最后一页到第一页逐个循环像素获取空白页,并将颜色与白色进行比较。如果与白色不同,那么我们返回这个位置 例如:

static float GetHeightFromPdfPage(Stream streamData, int pageIndex)
{
    // Convert last page to image
    var image = ConvertPageToImage(streamData, pageIndex);
    // Loop from last positionx to first position of image
    // Return if detect any color in (A, B, G) is different from than white color
    for (int y = image.Height - 1; y >= 0; y --)
    {
        for (int x = 0; x < image.Width; x++)
        {
            var colorPixel = image.GetPixel(x, y);
            if (colorPixel.A != System.Drawing.Color.White.A || colorPixel.B != System.Drawing.Color.White.B || colorPixel.G != System.Drawing.Color.White.G)
            {
                return y;
            }
        }
    }
    return 0;
}

static Bitmap ConvertPageToImage(Stream streamData, int pageIndex)
{
    var loadedDocument = new PdfLoadedDocument(streamData);
    var page = loadedDocument.Pages[pageIndex];

    PdfRenderer pdfExportImage = new PdfRenderer();
    pdfExportImage.Load(streamData);
    var skBitmap = pdfExportImage.ExportAsImage(pageIndex, page.Size, true);

    using (SKImage skImage = SKImage.FromBitmap(skBitmap))
    using (SKPixmap skPixmap = skImage.PeekPixels())
    using (var ms = new MemoryStream())
    {
        skPixmap.Encode(ms, SKEncodedImageFormat.Png, 100);
        return new Bitmap(ms);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.