技术问题 | 快速查询常见技术问题及解答-凯发线上登陆下载网址

技术问题 | 快速查询常见技术问题及解答-凯发线上登陆下载网址

   |    028-81705109    |       |    微信扫一扫    |       |   

  1. 简介
  2. spire.doc
  3. spire.xls
  4. spire.pdf
  5. spire.presentation

作为e-iceblue 的客户,你可能想知道“它可以做什么”或者“怎么使用它”,为了回答大部分这种问题可以浏览每个产品页包含的功能描述或者参考相关的教程。下面列出的是一些你在使用中可能经常遇到的问题的答案。

如果你没有发现你问题的答案,请联系凯发线上登陆下载网址(),并提供我们下面的详细信息。

如何从word文档中获取文本?
请使用document.gettext()方法。完整代码:
document document = new document();
document.loadfromfile(@"..\..\test.docx");
using (streamwriter sw = file.createtext("output.txt"))
 {
 sw.write(document.gettext());
 }
如何插入一张有指定宽高的图片?
您可以设置docpicture的高度和宽度的属性来调整图像的大小。完整代码:
document document = new document();
document.loadfromfile("sample.docx", fileformat.docx);
image image = image.fromfile("image.jpg");
//指定段落
paragraph paragraph = document.sections[0].paragraphs[2];
docpicture picture = paragraph.appendpicture(image);
//调整图片大小
picture.height = picture.height * 0.8f;
picture.width = picture.width * 0.8f;
document.savetofile("result.docx", fileformat.docx);
怎样对齐word文档中的文本?
请设置段落的horizontalalignment属性来对齐文本。全部代码:
document document = new document();
document.loadfromfile("sample.docx");
//左对齐
paragraph paragraph1 = document.sections[0].paragraphs[0];
paragraph1.format.horizontalalignment = horizontalalignment.left;
//居中对齐
paragraph paragraph2 = document.sections[0].paragraphs[1];
paragraph2.format.horizontalalignment = horizontalalignment.center;
//右对齐
paragraph paragraph3 = document.sections[0].paragraphs[2];
paragraph3.format.horizontalalignment = horizontalalignment.right;
document.savetofile("result.docx");
如何更改现有书签里的文本?
您可以使用bookmarksnavigator找到指定的书签。 然后使用replacebookmarkcontent方法去替换书签中的文本。全部代码:
document document = new document();
document.loadfromfile("sample.doc");
bookmarksnavigator bookmarknavigator = new bookmarksnavigator(document);
bookmarknavigator.movetobookmark("mybookmark");
//替换书签中的内容
bookmarknavigator.replacebookmarkcontent("new context", false);
document.savetofile("result.doc", fileformat.doc);
如何将word转换成html?
您可以使用savetofile方法并指定文件格式为html将word文档转换为html。全部代码:
document document = new document();
document.loadfromfile("sample.doc");
//将word保存为html
document.savetofile("result.html", fileformat.html);
document.close();
如何将html转换成word文档?
请调用loadfromfile方法加载html文件。 然后调用savetofile方法将html转换为word文档。 全部代码:
document document = new document();
document.loadfromfile("sample.html", fileformat.html, xhtmlvalidationtype.none);
//将html保存为word文档
document.savetofile("result.doc");
document.close();
如何将word2007转换为word2003?
只需调用savetofile方法并指定的文件格式为doc即可。全部代码:
document document = new document("word2007.docx");
                    
//将word2007 转换为 word2003
document.savetofile("word2003.doc", fileformat.doc);
document.close();
如何合并word文件?
请使用clone方法克隆一个章节。然后调用add方法添加复制的章节到指定的文档。 全部代码:
document document1 = new document();
document1.loadfromfile("merge1.docx");
document document2 = new document();
document2.loadfromfile("merge2.docx");
//把document2中的章节添加到document1
 foreach (section sec in document2.sections)
{
    document1.sections.add(sec.clone());
}
document1.savetofile("result.docx");
如何遍历word文档中表的单元格?
rows是表中行的集合,cells是一行中单元格的集合。所以你可以使用两个循环遍历表的单元格。全部代码:
document document = new document();
document.loadfromfile("sample.docx");
spire.doc.interface.itable table = document.sections[0].tables[0];
int i=0;
//遍历单元格
foreach (tablerow row in table.rows)
{
    foreach (tablecell cell in row.cells)
    {
        i  ;
    }
}
如何用设置文字的阴影效果?
您只需要设置textrange的isshadow属性。 全部代码:
document document = new document();
section section = document.addsection();
paragraph paragraph = section.addparagraph();
textrange htext = paragraph.appendtext("this is a test!");
//设置isshadow属性
htext.characterformat.isshadow = true;
htext.characterformat.fontsize = 80;
document.savetofile("result.doc");
如何在word中插入行号?
您需要设置该段的linenumberingrestartmode,linenumberingstep,linenumberingstartvalue属性插入行号。全部代码:
document document = new document();
section section = document.addsection();
//插入行号
section.pagesetup.linenumberingrestartmode = linenumberingrestartmode.restartpage;
section.pagesetup.linenumberingstep = 1;
section.pagesetup.linenumberingstartvalue = 1;
paragraph paragraph = section.addparagraph();
paragraph.appendtext("as an independent word .net component, spire.doc for .net doesn't need microsoft word to be installed on the machine. however, it can incorporate microsoft word document creation capabilities into any developers .net applications.");
document.savetofile("result.doc");
如何设置文字环绕图片?
请设置图片的textwrappingstyle和shapehorizontalalignment属性。全部代码:
document document = new document();
section section = document.addsection();
paragraph paragraph = section.addparagraph();
string str = "as an independent word .net component, spire.doc for .net doesn't need microsoft word to be installed on the machine. however, it can incorporate microsoft word document creation capabilities into any developers.net applications.as an independent word .net component, spire.doc for .net doesn't need microsoft word to be installed on the machine. however, it can incorporate microsoft word document creation capabilities into any developers’.net applications.";
paragraph.appendtext(str);
docpicture picture = paragraph.appendpicture(image.fromfile("logo.png"));
picture.textwrappingstyle = textwrappingstyle.tight;
picture.horizontalalignment = shapehorizontalalignment.center;
document.savetofile("result.doc");
如何编辑word文档中现有的表?
首先获取表格,然后可以编辑单元格中的文本,还可以在表格中插入新行。全部代码:
document doc = new document("sample.docx");
section section = doc.sections[0];
itable table = section.tables[0];
//编辑单元格中的文本
tablecell cell1 = table.rows[1].cells[1];
paragraph p1 = cell1.paragraphs[0];
p1.text = "abc";
tablecell cell2 = table.rows[1].cells[2];
paragraph p2 = cell2.paragraphs[0];
p2.items.clear();
p2.appendtext("def");
tablecell cell3 = table.rows[1].cells[3];
paragraph p3 = cell3.paragraphs[0];
(p3.items[0] as textrange).text = "hij";
//插入新行
tablerow newrow = table.addrow(true, true);
foreach (tablecell cell in newrow.cells)
{
    cell.addparagraph().appendtext("new row");
}
doc.savetofile("result.doc");
如何设置无下划线格式的超链接?
请将超链接的textrange的undderlinestyle设置成none。全部代码:
document document = new document();
section section = document.addsection();
paragraph paragraph = section.addparagraph();
field hyperlink = paragraph.appendhyperlink("www.e-iceblue.com", "www.e-iceblue.com", hyperlinktype.weblink);
textrange text = hyperlink.nextsibling.nextsibling as textrange;
text.characterformat.bold = true;
text.characterformat.underlinestyle = underlinestyle.none;
document.savetofile("result.doc");
如何将word文档设置为只读?
请使用protect方法并设置protectiontype为allowonlyreading。全部代码:
document document = new document();
document.loadfromfile("sample.docx");
document.protect(protectiontype.allowonlyreading);
document.savetofile("result.doc");
如何将图片添加到excel?
请使用add方法添加图片到excel文件的工作表中。全部代码:
workbook workbook = new workbook();
worksheet sheet = workbook.worksheets[0];
sheet.pictures.add(3, 2, "day.jpg");
workbook.savetofile("result.xlsx");
如何在excel文件的工作表中插入新行?
请使用insertrow方法在excel文件的工作表中插入新行。全部代码:
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
worksheet sheet = workbook.worksheets[0];
//在第三行插入新行
sheet.insertrow(3);
workbook.savetofile("result.xlsx");
如何设置打印区域?
您可以设置工作表的printarea属性来实现。 全部代码:
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
worksheet sheet = workbook.worksheets[0];
//设置打印区域为b2到f8
sheet.pagesetup.printarea = "b2:f8";
workbook.savetofile("result.xlsx");
如何复制带格式的单元格?
spire.xls为您提供了copy方法去复制带格式的单元格。全部代码:
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
worksheet sheet1 = workbook.worksheets[0];
worksheet sheet3 = workbook.worksheets[2];
//复制sheet1的单元格“b2”到sheet3的单元格“c6”
sheet1.range[3, 2].copy(sheet3.range[6,3]);
workbook.savetofile("result.xlsx");
如何将excel转换成pdf?
您只需要简单地加载文档,然后保存成pdf。全部代码:
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx", excelversion.version2010);
workbook.savetofile("result.pdf", spire.xls.fileformat.pdf);
怎样合并工作表中的单元格?
spire.xls为您提供了名为merge的方法来合并xls中的单元格。全部代码:
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
worksheet sheet = workbook.worksheets[0];
//合并"b3"到"b4"的单元格
sheet.range["b3:b4"].merge();
workbook.savetofile("result.xlsx");
如何重新排列excel中的工作表?
spire.xls为您提供了moveworksheet方法来重新排列excel中的工作表。全部代码:
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
worksheet sheet = workbook.worksheets[3];
//将第4张工作表移到第1张工作表的位置
sheet.moveworksheet(0);
workbook.savetofile("result.xlsx");
如何删除excel工作表中的列?
请使用deletecolumn方法来删除列。此方法即刻影响工作表的收集顺序。 全部代码:
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
worksheet sheet = workbook.worksheets[1];
//删除第2列
sheet.deletecolumn(2);
//删除第4列
sheet.deletecolumn(3);
workbook.savetofile("result.xlsx");
如何在excel中设置指定范围的数字格式?
请使用numberformat属性去设置指定范围的数字格式。全部代码:
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
worksheet sheet = workbook.worksheets[0];
//设置指定范围的数字格式
sheet.range[2, 2, 6, 6].numberformat = "$#,##0.00";
sheet.range["c3"].numbervalue = 3240.689;
sheet.range["d4"].numbervalue = 5230.123;
workbook.savetofile("result.xlsx");
如何在单元格中添加公式?
只需设置单元格的formula属性即可添加公式。全部代码:
workbook workbook = new workbook();
worksheet worksheet = workbook.worksheets[0];
//添加if公式到单元格j7中
string formula = @"=if(h7>0,(if(f7 > 0,(h7-f7)/f7,"""")),"""")";
worksheet.range["j7"].formula = formula;
worksheet.range["f7"].numbervalue = 5;
worksheet.range["h7"].numbervalue = 4;
workbook.savetofile("result.xlsx", excelversion.version2007);
如何合并多个excel?
spire.xls为您提供了addcopy方法来合并excel文件。全部代码:
workbook workbook = new workbook();
workbook.loadfromfile("sample1.xlsx");
workbook workbookdest = new workbook();
workbookdest.loadfromfile("sample2.xlsx");
							
//将workbook中的单元格拷贝到workbookdest中
workbookdest.worksheets.addcopy(workbook.worksheets);
workbookdest.savetofile("result.xlsx");
如何将html代码转换为pdf?
请在线程中使用loadfromhtml加载html,然后保存为pdf。全部代码:
pdfdocument pdf = new pdfdocument();
pdfhtmllayoutformat htmllayoutformat = new pdfhtmllayoutformat();
htmllayoutformat.iswaiting = false;
pdfpagesettings setting = new pdfpagesettings();
setting.size = pdfpagesize.a4;
string htmlcode = file.readalltext("..\\..\\2.html");
thread thread = new thread(() =>
{ pdf.loadfromhtml(htmlcode, false, setting, htmllayoutformat);});
thread.setapartmentstate(apartmentstate.sta);
thread.start();
thread.join();
pdf.savetofile("output.pdf");

另外,我们还有一个插件的方法转换html到pdf,参考文档:
如何在表的单元格中嵌入另一个表?
表格是一个更简单的网格。在网格中,您可以操纵每个单元格,为每个单元格设置不同的样式并嵌入另一个网格。所以您可以用网格来做这个工作。 全部代码:
pdfdocument document = new pdfdocument();
pdfpagebase page = document.pages.add(pdfpagesize.a4);
//新建一个网格
pdfgrid grid = new pdfgrid();
grid.columns.add(1);
grid.columns[0].width = page.canvas.clientsize.width;
pdfgridrow row0 = grid.rows.add();
row0.cells[0].value = "this is the first row.";
row0.cells[0].stringformat = new pdfstringformat(pdftextalignment.center, pdfverticalalignment.middle);
pdfgridrow row1 = grid.rows.add();
pdflayoutresult result=grid.draw(page, new pointf(0, 20));
pdfgrid grid2 = new pdfgrid();
grid2.columns.add(2);
pdfgridrow newrow = grid2.rows.add();
grid2.columns[0].width = grid.columns[0].width / 2;
grid2.columns[1].width = grid.columns[0].width / 2;
newrow.cells[0].value = "this is row two column one.";
newrow.cells[0].stringformat = new pdfstringformat(pdftextalignment.center, pdfverticalalignment.middle);
newrow.cells[1].value = "this is row two column two.";
newrow.cells[1].stringformat = new pdfstringformat(pdftextalignment.center, pdfverticalalignment.middle);
//将grid2嵌入到grid的row1的第一个单元格中
row1.cells[0].value = grid2;
result = grid2.draw(page, new pointf(0, result.bounds.location.y   result.bounds.height));
document.savetofile("result.pdf");
如何合并网格中的单元格?
请使用rowspan或columnspan属性去合并网格中的单元格。全部代码:
pdfdocument doc = new pdfdocument();
pdfpagebase page = doc.pages.add();
pdfgrid grid = new pdfgrid();
grid.columns.add(5);
float width = page.canvas.clientsize.width - (grid.columns.count   1);
for (int i = 0; i < grid.columns.count; i  )
{
    grid.columns[i].width = width * 0.20f;
}
pdfgridrow row0 = grid.rows.add();
pdfgridrow row1 = grid.rows.add();
row0.style.font = new pdftruetypefont(new font("arial", 16f, fontstyle.bold), true);
row1.style.font = new pdftruetypefont(new font("arial", 16f, fontstyle.italic), true);
row0.cells[0].value = "corporation";
//将row0的第一个单元格与下面的单元格合并
row0.cells[0].rowspan = 2;
row0.cells[1].value = "b&k undersea photo";
row0.cells[1].stringformat = new pdfstringformat(pdftextalignment.center, pdfverticalalignment.middle);
//将row0的第二个单元格与右边的单元格合并
row0.cells[1].columnspan = 3;
row0.cells[4].value = "world";
row0.cells[4].style.font = new pdftruetypefont(new font("arial", 10f, fontstyle.bold | fontstyle.italic), true);
row0.cells[4].stringformat = new pdfstringformat(pdftextalignment.center, pdfverticalalignment.middle);
row0.cells[4].style.backgroundbrush = pdfbrushes.lightgreen;
row1.cells[1].value = "diving international unlimited";
row1.cells[1].stringformat = new pdfstringformat(pdftextalignment.center, pdfverticalalignment.middle);
row1.cells[1].columnspan = 4;
grid.draw(page, new pointf(0, 0));
doc.savetofile("result.pdf");
如何给pdf文件添加签名?
首先,使用pdfcertificate类创建一个证书实例,用于创建pdfsignature实例。然后设置pdfsignature实例的相关属性。全部代码:
pdfdocument doc = new pdfdocument();
doc.loadfromfile("sample.pdf");
pdfcertificate cert = new pdfcertificate("demo.pfx", "e-iceblue");
//给每一页都添加一个signature
foreach (pdfpagebase page in doc.pages)
{
    pdfsignature signature = new pdfsignature(page.document, page, cert, "demo");
    signature.contactinfo = "harry";
    signature.certificated = true;
    signature.documentpermissions = pdfcertificationflags.allowformfill;
}
doc.savetofile("result.pdf");
如何将word转换成html?
您可以使用savetofile方法并指定文件格式为html将word文档转换为html。全部代码:
document document = new document();
document.loadfromfile("sample.doc");
//将word保存为html
document.savetofile("result.html", fileformat.html);
document.close();
如何重新排列pdf页面顺序?
请使用rearrange方法。该方法以int数组为参数。 int数组表示页面的新顺序。全部代码:
pdfdocument document = new pdfdocument();
//sample.pdf has four pages
document.loadfromfile("sample.pdf");
//重新调整页面顺序
int[] range = new int[] { 0, 2, 1, 3 };
document.pages.rearrange(range);
document.savetofile("result.pdf");
如何添加图片水印?
请使用backgroundimage方法,并且可以使用backgroundregion调整图片的大小以及位置。
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile("sample.pdf");
pdfpagebase page = pdf.pages[0];
image img = image.fromfile("logo.png");
page.backgroundimage = img;
page.backgroundregion = new rectanglef(300, 200, 80, 81);
pdf.savetofile("result.pdf");
如何在所有页面上重复页眉?
pdfdocumenttemplate中的内容将应用于pdf文件的每一页。你需要做的是创建一个方法,添加页眉到pdfdocumenttemplate中。然后页眉将会被添加到每个页面。全部代码:
static void main(string[] args)
{
    pdfdocument doc = new pdfdocument();
    pdfunitconvertor unitcvtr = new pdfunitconvertor();
    pdfmargins margin = new pdfmargins();
    margin.top = unitcvtr.convertunits(3.0f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point);
    margin.bottom = margin.top;
    margin.left = unitcvtr.convertunits(3.17f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point);
    margin.right = margin.left;
    //添加3页
    pdfpagebase page = doc.pages.add(pdfpagesize.a4, margin);
    page = doc.pages.add(pdfpagesize.a4, margin);
    page = doc.pages.add(pdfpagesize.a4, margin);
    //应用模板
    setdocumenttemplate(doc, pdfpagesize.a4, margin);
    doc.savetofile("result.pdf");
}
//method to add header to every page
private static void setdocumenttemplate(pdfdocument doc, sizef pagesize, pdfmargins margin)
{
    pdfpagetemplateelement leftspace
        = new pdfpagetemplateelement(margin.left, pagesize.height);
    doc.template.left = leftspace;
    pdfpagetemplateelement topspace
        = new pdfpagetemplateelement(pagesize.width, margin.top);
    topspace.foreground = true;
    doc.template.top = topspace;
    //draw header label
    pdftruetypefont font = new pdftruetypefont(new font("arial", 9f, fontstyle.italic));
    pdfstringformat format = new pdfstringformat(pdftextalignment.right);
    string label = "demo about header repeating";
    //set the header style
    sizef size = font.measurestring(label, format);
    float y = topspace.height - font.height - 40;
    pdfpen pen = new pdfpen(color.black, 0.75f);
    topspace.graphics.settransparency(0.5f);
    topspace.graphics.drawline(pen, margin.left - 30, y, pagesize.width - margin.right   30, y);
    y = y - 1 - size.height;
    topspace.graphics.drawstring(label, font, pdfbrushes.black, pagesize.width - margin.right, y, format);
    pdfpagetemplateelement rightspace
        = new pdfpagetemplateelement(margin.right, pagesize.height);
    doc.template.right = rightspace;
    pdfpagetemplateelement bottomspace
        = new pdfpagetemplateelement(pagesize.width, margin.bottom);
    bottomspace.foreground = true;
    doc.template.bottom = bottomspace;
}
如何给powerpoint文档添加密码保护?
请使用encrypt方法给文档加密。全部代码:
presentation presentation = new presentation();
presentation.loadfromfile("sample.pptx");
presentation.encrypt("test");
presentation.savetofile("encrypt.pptx", fileformat.pptx2010);
如何转换powerpoint文件到pdf?
只需要加载文件,然后保存成pdf即可。全部代码:
presentation presentation = new presentation();
//加载文件
presentation.loadfromfile("ppt.ppt");
//把文件保存成pdf
presentation.savetofile("topdf.pdf", fileformat.pdf);
如何将幻灯片保存成图片?
请使用saveasimage方法将幻灯片保存到image中,然后将image对象保存成图片。全部代码:
presentation ppt = new presentation();
ppt.loadfromfile(@"f:\testing\sample.pptx");
for (int i = 0; i < ppt.slides.count; i  )
{
  //将幻灯片保存到image中
  image image = ppt.slides[i].saveasimage();
  string filename = string.format("5614-img-{0}.png", i);
  //将image保存成文件
  image.save(filename, system.drawing.imaging.imageformat.png);
}
如何设置文本框中文本的对齐方式?
请使用alignment设置文本的对齐方式。全部代码:
presentation presentation = new presentation();
//添加一个shape
iautoshape shape = presentation.slides[0].shapes.appendshape(shapetype.rectangle, new rectanglef(50, 70, 600, 400));
//设置shape里面第一段的对齐方式为左对齐
shape.textframe.paragraphs[0].alignment = textalignmenttype.left;
shape.fill.filltype = fillformattype.none;
//添加文本
shape.textframe.text = "demo about alignment";
presentation.savetofile("alignment.pptx", fileformat.pptx2010);
如何提取powerpoint中的文本?
请您使用textparagraph的text属性。全部代码:
stringbuilder sb = new stringbuilder();
for (int i = 0; i < ppt.slides.count;i   )
       {
           for (int j = 0; j < ppt.slides[i].shapes.count;j   )
           {
               if (ppt.slides[i].shapes[j] is iautoshape)
               {
                   iautoshape shape=ppt.slides[i].shapes[j] as iautoshape;
                   if (shape.textframe != null)
                   {
                       foreach (textparagraph tp in shape.textframe.paragraphs)
                       {
                           sb.append(tp.text   environment.newline);
                       }
                   }
               }
           }
       }
如何用图片填充形状?
请使用pictureshape的url属性。全部代码:
presentation ppt = new presentation();
iautoshape shape = (iautoshape)ppt.slides[0].shapes.appendshape(shapetype.doublewave, new rectanglef(100, 100, 400, 200));
string picurl = @"c:\users\administrator\desktop\image.jpg";
shape.fill.filltype = fillformattype.picture;
shape.fill.picturefill.picture.url = picurl;
shape.fill.picturefill.filltype = picturefilltype.stretch;
shape.shapestyle.linecolor.color = color.transparent;
ppt.savetofile("shape.pptx", fileformat.pptx2010);
网站地图