试试添加spire.pdf.dll为引用,导入以下命名空间:
using Spire.Pdf
using System.IO
//加载PDF流
PdfDocument doc = new PdfDocument()
doc.LoadFromStream(PdfStream)
//指定打印机
doc.PrintSettings.PrinterName = "HP LaserJet P1007"
//设置文档打印页码范围
doc.PrintSettings.SelectPageRange(1, 5)
//打印PDF文档
doc.Print()
添加spire.pdf.dll为引用,使用下面的代码即可提取pdf中所有表格数据到txt文档:
using System.IO
using System.Text
using Spire.Pdf
using Spire.Pdf.Utilities
namespace ExtractPdfTable
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument()
//Load the sample PDF file
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Table.pdf")
//Create a StringBuilder object
StringBuilder builder = new StringBuilder()
//Initialize an instance of PdfTableExtractor class
PdfTableExtractor extractor = new PdfTableExtractor(doc)
//Declare a PdfTable array
PdfTable[] tableList = null
int tableNum = 1
//Loop through the pages
for (int pageIndex = 0pageIndex <doc.Pages.CountpageIndex++)
{
//Extract tables from a specific page
tableList = extractor.ExtractTable(pageIndex)
//Determine if the table list is null
if (tableList != null &&tableList.Length >0)
{
//Loop through the table in the list
foreach (PdfTable table in tableList)
{
builder.Append("Table " + tableNum)
builder.Append("\r\n")
//Get row number and column number of a certain table
int row = table.GetRowCount()
int column = table.GetColumnCount()
//Loop though the row and colunm
for (int i = 0i <rowi++)
{
for (int j = 0j <columnj++)
{
//Get text from the specific cell
string text = table.GetText(i, j)
//Add text to the string builder
builder.Append(text + " ")
}
builder.Append("\r\n")
}
builder.Append("\r\n")
tableNum += 1
}
}
}
//Write to a .txt file
File.WriteAllText("Table.txt", builder.ToString())
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)