在将Office文档转换为PDF时需要输入 [英] Input Desired on Converting Office Documents to PDF
本文介绍了在将Office文档转换为PDF时需要输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试[从命令行或C#中的类库]将文档,excel文件等转换为pdf.无需使用Interop或在计算机上安装任何办公产品.
I am trying to convert [from a command line, or a class library in c#] a document, excel file, etc. to a pdf. Without using Interop or having any office products installed on the machine.
有人做过这样的事情吗?或有什么想法?
Has anyone done such a thing? Or have any ideas?
我们遇到的所有解决方案似乎都需要互操作
All of the solutions we have come across seem to require interop
谢谢
推荐答案
如果有帮助,请参阅此文章: 转换并在C#中将Office文件合并为一个PDF文件
See this article if it helps: Convert and Merge Office Files to One PDF File in C#
using System.Drawing;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Doc;
using Spire.Xls;
using Spire.Presentation;
using System.IO;
using Spire.Pdf.Graphics;
namespace ConvertAndMerge
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PdfFiles = new List();
}
public List PdfFiles { get; set;}
//Add files to listbox
private void btnAdd_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All files (*.docx, *.pdf, *.pptx, *.pdf)|*.docx;*.pdf;*.pptx;*.xlsx";
ofd.Multiselect=true;
if (DialogResult.OK == ofd.ShowDialog())
{
string[] files = ofd.FileNames;
listBox1.Items.AddRange(files);
}
}
private void btnMerge_Click(object sender, EventArgs e)
{
//Convert other file formats to PDF file
string ext=string.Empty;
foreach (object item in listBox1.Items)
{
ext=Path.GetExtension(item.ToString());
switch (ext)
{
case ".docx":
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document(item.ToString());
doc.SaveToStream(ms, Spire.Doc.FileFormat.PDF);
PdfFiles.Add(new PdfDocument(ms));
}
break;
case ".pdf":
PdfFiles.Add(new PdfDocument(item.ToString()));
break;
case ".pptx":
using (MemoryStream ms = new MemoryStream())
{
Presentation ppt = new Presentation(item.ToString(),Spire.Presentation.FileFormat.Auto);
ppt.SaveToFile(ms,Spire.Presentation.FileFormat.PDF);
PdfFiles.Add(new PdfDocument(ms));
}
break;
case ".xlsx":
using (MemoryStream ms = new MemoryStream())
{
Workbook xls = new Workbook();
xls.LoadFromFile(item.ToString());
xls.SaveToStream(ms, Spire.Xls.FileFormat.PDF);
PdfFiles.Add(new PdfDocument(ms));
}
break;
default:
break;
}
}
//Merge the PDF files into one PDF
PdfDocument newPdf1 = new PdfDocument();
foreach (PdfDocument doc in PdfFiles)
{
newPdf1.AppendPage(doc);
}
//Create a new PDF with specified page size, copy the content of merged file to new PDF file
PdfDocument newPdf2 = new PdfDocument();
foreach (PdfPageBase page in newPdf1.Pages)
{
PdfPageBase newPage = newPdf2.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
PdfTextLayout loLayout = new PdfTextLayout();
loLayout.Layout = PdfLayoutType.OnePage;
page.CreateTemplate().Draw(newPage, new PointF(0, 0), loLayout);
}
//Save the destination PDF file
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Pdf files(*.pdf)|*.pdf";
if (DialogResult.OK == sfd.ShowDialog())
{
newPdf2.SaveToFile(sfd.FileName);
}
}
}
}
这篇关于在将Office文档转换为PDF时需要输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文