在打印时,我们需根据pdf的页面大小来选择相应的纸张大小。使用spire.pdf, 我们可以。该文将介绍如何使用spire.pdf组件获取pdf文档的页面大小。
c#
using spire.pdf;
using spire.pdf.actions;
using spire.pdf.general;
using spire.pdf.graphics;
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.io;
using system.linq;
using system.text;
using system.threading.tasks;
namespace getpdfpagesize
{
class program
{
static void main(string[] args)
{
//加载pdf文档
pdfdocument doc = new pdfdocument();
doc.loadfromfile("sample.pdf");
//获取第一页
pdfpagebase page = doc.pages[0];
//获取页面宽度和高度
float pointwidth = page.size.width;
float pointheight = page.size.height;
//将度量单位转换为厘米
pdfunitconvertor unitcvtr = new pdfunitconvertor();
float centimeterwidth = unitcvtr.convertunits(pointwidth, pdfgraphicsunit.point, pdfgraphicsunit.centimeter);
float centimeterheight = unitcvtr.convertunits(pointheight, pdfgraphicsunit.point, pdfgraphicsunit.centimeter);
//输出文本文档
stringbuilder content = new stringbuilder();
content.appendline("该pdf的页面大小为(宽: " pointwidth "pt, 高: " pointheight "pt).");
content.appendline("该pdf的页面大小为(宽: " centimeterwidth "cm, 高: " centimeterheight "cm.)");
string output = "getpagesize_out.txt";
//save them to a txt file
file.writealltext(output, content.tostring());
}
}
}
vb.net
imports spire.pdf
imports spire.pdf.actions
imports spire.pdf.general
imports spire.pdf.graphics
imports system.collections.generic
imports system.componentmodel
imports system.data
imports system.drawing
imports system.io
imports system.linq
imports system.text
imports system.threading.tasks
namespace getpdfpagesize
class program
private shared sub main(args as string())
'加载pdf文档
dim doc as new pdfdocument()
doc.loadfromfile("sample.pdf")
'获取第一页
dim page as pdfpagebase = doc.pages(0)
'获取页面宽度和高度
dim pointwidth as single = page.size.width
dim pointheight as single = page.size.height
'将度量单位转换为厘米
dim unitcvtr as new pdfunitconvertor()
dim centimeterwidth as single = unitcvtr.convertunits(pointwidth, pdfgraphicsunit.point, pdfgraphicsunit.centimeter)
dim centimeterheight as single = unitcvtr.convertunits(pointheight, pdfgraphicsunit.point, pdfgraphicsunit.centimeter)
'输出文本文档
dim content as new stringbuilder()
content.appendline("该pdf的页面大小为(宽: " pointwidth "pt, 高: " pointheight "pt).")
content.appendline("该pdf的页面大小为(宽: " centimeterwidth "cm, 高: " centimeterheight "cm.)")
dim output as [string] = "getpagesize_out.txt"
'save them to a txt file
file.writealltext(output, content.tostring())
end sub
end class
end namespace
效果图: