111111111.pdf (39.6 KB)
测试插入表格到PDF.pdf (4.3 KB)
微信截图_20220402185757.png (31.4 KB)
/// <summary>
/// 插入表格到PDF
/// </summary>
public static void TestPdfToTable()
{
// The path to the documents directory.
string dataDir = @"C:\Users\Administrator\Desktop\新建文件夹\插入前\测试插入表格到PDF.pdf";
// Load source PDF document
APDF.Document doc = new APDF.Document(dataDir);
MarginInfo marginInfo = new MarginInfo()
{
Top = 0,
Left = 0,
Right = 0,
Bottom = 0,
};
// Initializes a new instance of the Table
APDF.Table table = new APDF.Table();
//table.Top = 50;
//table.Left = 200;
//table.VerticalAlignment = APDF.VerticalAlignment.Center;
//table.HorizontalAlignment = APDF.HorizontalAlignment.Center;
// Set the table border color as LightGray
table.Border = new APDF.BorderInfo(APDF.BorderSide.All, 1.2f, APDF.Color.FromRgb(System.Drawing.Color.Red));
// Set the border for table cells
table.DefaultCellBorder = new APDF.BorderInfo(APDF.BorderSide.All, 1.2f, APDF.Color.FromRgb(System.Drawing.Color.Red));
table.ColumnWidths = "50 50 50";
//table.DefaultColumnWidth = "50";
table.DefaultCellTextState = new TextState()
{
FontSize = 10F,
Font = FontRepository.FindFont("Arial"),
HorizontalAlignment = APDF.HorizontalAlignment.Center,
};
table.DefaultCellPadding = new MarginInfo(5, 5, 5, 5);
// Create a loop to add 10 rows
for (int row_count = 1; row_count < 3; row_count++)
{ //Add row to table
Aspose.Pdf.Row row = table.Rows.Add();
row.FixedRowHeight = 25;
//row.VerticalAlignment = APDF.VerticalAlignment.Center;
row.DefaultCellTextState.HorizontalAlignment = APDF.HorizontalAlignment.Center;
// Add table cells
row.Cells.Add(" (" + row_count + ", 1)");
row.Cells.Add(" (" + row_count + ", 2)");
row.Cells.Add(" (" + row_count + ", 3)");
}
// Add table object to first page of input document
double pageWidth = doc.Pages[1].GetPageRect(false).Width;
double pageHeight = doc.Pages[1].GetPageRect(false).Height;
double tableWidth = table.GetWidth();
table.Top = 10;
table.Left = (float)(pageWidth - tableWidth - 10);
doc.Pages[1].PageInfo = new PageInfo()
{
Margin = marginInfo,
Height = pageHeight,
Width = pageWidth,
};
Console.WriteLine("pageWidth:" + pageWidth+ "pageHeight:" + pageHeight);
doc.Pages[1].Paragraphs.Add(table);
string dataoDir = @"C:\Users\Administrator\Desktop\新建文件夹\插入后\111111111.pdf";
// Save updated document containing table object
doc.Save(dataoDir);
}
以上是我写的测试代码,我只是想把表格插入到右上角的固定位置横向显示,但不知道为什么总是会在左侧,并且是竖着的,有什么好办法解决吗?
我写的是C#代码,麻烦帮我用C#代码测试一下,看看是哪里出的问题,非常感谢!