admin管理员组文章数量:1605188
整体命名空间:
using Spire.Pdf;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
Word转Pdf
需要NuGet的包:Microsoft.Office.Interop.Word
public string WordToPdf(string wordPath, string pdfPath = null)
{
Word.Application application = new Word.Application();
Word.Document document = null;
application.Visible = false;
document = application.Documents.Open(wordPath);
pdfPath = pdfPath ?? wordPath.Replace(".docx", ".doc").Replace(".doc", ".pdf");
int t = 1;
while (File.Exists(pdfPath))
{
if (t == 1)
{
pdfPath = string.Concat(Path.GetDirectoryName(pdfPath), "\\", Path.GetFileNameWithoutExtension(pdfPath), "(", t.ToString(), ").pdf");
}
else
{
pdfPath = pdfPath.Replace("(" + (t - 1) + ").pdf", "(" + t + ").pdf");
}
t++;
}
if (!File.Exists(pdfPath))
{
document.ExportAsFixedFormat(pdfPath, Word.WdExportFormat.wdExportFormatPDF);
}
document.Close();
return pdfPath;
}
Pdf转Word
需要NuGet的包:Spire.Pdf
public string PdfToWord(string pdfPath, string wordPath = null)
{
PdfDocument document = new PdfDocument();
document.LoadFromFile(pdfPath);
wordPath = wordPath ?? pdfPath.Replace(".pdf", ".doc");
int t = 1;
while (File.Exists(pdfPath))
{
if (t == 1)
{
pdfPath = string.Concat(Path.GetDirectoryName(pdfPath), "\\", Path.GetFileNameWithoutExtension(pdfPath), "(", t.ToString(), ").pdf");
}
else
{
pdfPath = pdfPath.Replace("(" + (t - 1) + ").pdf", "(" + t + ").pdf");
}
t++;
}
if (!File.Exists(wordPath))
{
document.SaveToFile(wordPath, FileFormat.DOC);
}
document.Close();
return wordPath;
}
版权声明:本文标题:C# Word和Pdf互相转换 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1728480272a1160029.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论