指定输出PDF和图像中如何跨越字符串

可能的使用场景

当单元格包含文本或字符串但大于单元格宽度时,如果下一列的单元格为空,则字符串会溢出。当您将Excel文件保存为PDF / 图像时,您可以通过使用TextCrossType枚举指定交叉类型来控制此溢出。它具有以下值

  • TextCrossType.Default: 显示与MS Excel类似的文本,具体取决于下一个单元格。 如果下一个单元格为空,则字符串将交叉或被截断。

  • TextCrossType.CrossKeep: 显示与MS Excel导出PDF/Image类似的字符串。

  • TextCrossType.CrossOverride: 显示所有文本,通过跨越其他单元格,并覆盖经过交叉处理的单元格上的文本

  • TextCrossType.StrictInCell: 仅在单元格宽度内显示字符串。

使用TextCrossType指定输出PDF/图像中如何跨越字符串

以下示例代码加载示例Excel文件,并通过指定不同的TextCrossType将其保存为PDF/图像格式。可以从以下链接下载示例Excel文件和输出文件:

sampleCrossType.xlsx

outputCrossType.pdf

outputCrossType.png

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Load template Excel file
Workbook wb = new Workbook(sourceDir + "sampleCrosssType.xlsx");
// Create file streams for saving the PDF and PNG files
using (FileStream outputStream = new FileStream(outputDir + "outputCrosssType.pdf", FileMode.Create))
using (FileStream outputStream2 = new FileStream(outputDir + "outputCrosssType.png", FileMode.Create))
{
// Initialize PDF save options
PdfSaveOptions saveOptions = new PdfSaveOptions();
// Set text cross type
saveOptions.TextCrossType = TextCrossType.StrictInCell;
// Save PDF file
wb.Save(outputStream, saveOptions);
// Initialize image or print options
ImageOrPrintOptions imageSaveOptions = new ImageOrPrintOptions();
// Set text cross type
imageSaveOptions.TextCrossType = TextCrossType.StrictInCell;
// Initialize sheet renderer object
SheetRender sheetRenderer = new SheetRender(wb.Worksheets[0], imageSaveOptions);
// Create bitmap image from sheet renderer
System.Drawing.Bitmap bitmap = sheetRenderer.ToImage(0);
// Save PNG image
bitmap.Save(outputStream2, ImageFormat.Png);
}