//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;
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 (var ms = new MemoryStream())
{
skBitmap.Encode(ms, SKEncodedImageFormat.Png, 100);
return new Bitmap(ms);
}
}
2条答案
按热度按时间wz3gfoph1#
我们可以通过PdfLayoutResult获取PDF文档页面中的空白区域。请参考下面的代码片段了解更多细节,
C#:
我们已经创建了相同的样本,可以从下面的链接下载,
Sample to get the remaining page size of PDF document
xkftehaa2#
我们可以得到空白页循环像素从最后一行页到第一行页和比较颜色与白色。如果与白色不同,则返回该位置。例如: