您有免费的 .Net 托管代码用于将 DocX 转换为 PDF 吗?

发布于 2024-08-03 05:28:34 字数 927 浏览 5 评论 0原文

在我的网络项目中,我使用 DocX 文件类型来包含报告模板。我需要将 DocX 文件类型转换为 PDF。您有任何 .Net 托管代码可以执行此操作吗?

我知道解决这个问题的几种方法。但它不是托管代码并且像以下项目一样免费。

  • Word 12.0 对象库 以编程方式将 Word 2007 文档另存为PDF 文档或 XPS 文档。但它需要在服务器上安装 Office 2007。

  • 使用一些免费的 PDF 打印机进行打印,例如 PDFCreator。但我需要一些在 Office 2007 等服务器上打开 DocX 的程序。这是一个非常糟糕的主意。

  • 通过免费转换器程序进行转换。但结果并不完美。

  • 使用XF 渲染服务器等框架。这是一个非常好的主意,但它不是免费的。

  • 在 PDF Api 中创建所有文档内容,例如 iTextSharp。但我必须努力创建漂亮的文档。

  • 或者使用其他文件格式创建模板文档,可以将其导出为 DocX 和 PDF 文件格式。此外,最终用户应该可以轻松编辑此文件。如果您知道,请告诉我。

谢谢

In my web project, I use DocX file type for containing report template. I need to convert DocX file type to PDF. Do you have any .Net managed code for doing that?

I know several ways for solving this question. But it isn't managed code and free like the following items.

  • Word 12.0 Object Library To programmatically save a Word 2007 document as either a PDF document or an XPS document. But it requires installing Office 2007 on server.

  • Print by using some free PDF printer like PDFCreator. But I need some program for open DocX on server like Office 2007. It's very bad idea.

  • Convert by free convertor program. But result isn't perfect.

  • Use framework like XF Rendering Server. It's very good idea but it isn't free.

  • Create all document content in PDF Api like iTextSharp. But I must try hard for creating nice document.

  • Or create template document with other file format that can export it to DocX and PDF file format. Moreover, end-users should easily edit this file. If you know, please tell me.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

柠檬色的秋千 2024-08-10 05:28:34

我没有将 DocX 转换为 PDF 的代码,但看来您对 DocX 的要求并不严格。你的最后一颗子弹说:

或者使用其他创建模板文档
可以将其导出到 DocX 的文件格式
和 PDF 文件格式。而且,
最终用户应该轻松编辑此内容
文件。如果你知道请告诉我。

我读到这意味着您希望能够创建一个模板文档,用数据填充它并将其转换为 PDF,但允许维护模板,对吗?

解决方案:XSL-FO

XSL-FO 与 HTML 一样是 W3C 标准,可以被许多开源和商业产品转换为 PDF、WordML、XPS、PS、PCL、SVG、TIFF 等我使用它每月交付数十万份文档,包括在线 PDF 和离线文档(例如批量支票打印)。

为了帮助您入门,这里是 XML-FO 的 W3C 页面。那里有很多有用的信息,包括左侧的软件列表(开源和商业)。我个人使用过两种商业产品,分别是 IBEX PDF CreatorRenderX 的 XEP。两者都是优秀的产品,并且有一个 100% 托管的 C# 实现来获取 PDF,名为 FO.NET up在 CodePlex 上。我没有尝试过这个,但它应该满足你的“免费”标准。

您可以通过多种方式编辑要在 XSL-FO 中创建的文档的模板。通常,此模板是应用于 XML 数据的 XSLT,但这不是必需的。我已经手工构建了这些,但这有一点学习曲线。您可以从 XSL-FO 中的文档开始,然后用代码填充其中的各个部分,就像 HTML 一样。好消息是 许多 <有一个 href="http://www.renderx.com/tools/datastyler.html" rel="noreferrer">XSL-FO 编辑器。坏消息是,据我所知,没有一个是免费的,但其中一些很便宜,您可以通过谷歌搜索找到一些符合免费标准的东西。但是,一种选择是您可以使用样式表从 Word 进行转换(商业 & < a href="http://msdn.microsoft.com/en-us/library/aa537167(office.11​​).aspx" rel="noreferrer">免费)。

I do not have code for converting DocX to PDF, but it appears your requirement for DocX is not firm. Your last bullet says:

Or create template document with other
file format that can export it to DocX
and PDF file format. Moreover,
end-users should easily edit this
file. If you know, please tell me.

I read this to mean you want to be able to create a template document, fill it with data and covert it to PDF, yet allow the template to be maintained, right?

Solution: XSL-FO

XSL-FO is a W3C standard like HTML and can be transformed by a number of open source and commercial products into PDF, WordML, XPS, PS, PCL, SVG, TIFF, etc. I have used this to deliver hundreds of thousands of documents per month, both online as PDFs and offline (things like bulk check printing).

To get you started, here is the W3C page for XML-FO. There is a lot of good information there, including a list of software (both open source and commecial) down the left side. I have personally used two commercial products called IBEX PDF Creator and XEP by RenderX. Both are excellent products, and there is a 100% managed C# implementation to get to PDF called FO.NET up on CodePlex. I have not tried this, but it should satisfy your "free" criterion.

There are a number of ways you can edit the template for documents to be created in XSL-FO. Typically this template is XSLT that you apply to your XML data, but this is not a requirement. I have built these by hand, but it is a bit of a learning curve. You can start with a document in XSL-FO and fill in sections of it with code, just as you could HTML. The good news is that there are a number of XSL-FO editors out there. The bad news is that none I know of are free, but several of them are cheap and you may find something that meets the free criterion with a bit of Googling. However, one option is that you can convert from Word using a stylesheet (commercial & free).

深府石板幽径 2024-08-10 05:28:34

安装 Office 2007 并使用 Word 12 对象库绝对是我的选择(并且在我自己的一些项目中也这样做过)。

如果您不想在生产 Web 服务器上安装 Word,为什么不在辅助服务器上安装它呢?您可以让第二个服务器与第一个服务器进行通信(使用 Web 服务或类似的东西)——它可以请求下一个需要导出的 Word 文档,进行转换,然后返回 PDF 数据。

如果您想要执行此转换的 Word 自动化的 C# 示例,请告诉我(非常简单)。

阿德里安


这是我的代码,为杰森发布。这适用于Word 2007。您需要从Office网站下载并安装PDF导出器:

using Microsoft.Office.Interop.Word;

...

object _read_only = false;
object _visible = true;
object _false = false;
object _true = true;
object _dynamic = 2;
object  _missing = System.Reflection.Missing.Value;

object _htmlFormat = 8;        
object _pdfFormat = 17;
object _xpsFormat = 18;

object fileName = "C:\\Test.docx";

ApplicationClass ac = new ApplicationClass();
//ac.Visible = true; // Uncomment to see Word as it opens and converts the document
//ac.Activate();

Document d = ac.Documents.Open(ref fileName, ref _missing, ref _true, ref _read_only, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _visible, ref _missing, ref _missing, ref _missing, ref _missing);

object newFileName = ((string)fileName).Substring(0, ((string)fileName).LastIndexOf(".")) + ".pdf";

d.SaveAs(ref newFileName, ref _pdfFormat, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing);

d.Close(ref _false, ref _missing, ref _missing);
ac.Quit(ref _false, ref _missing, ref _missing);

ac = null;

所以,Soul_Master,您的意思是您不想使用互操作(虽然您没有说为什么,我有兴趣知道),您不想向商业出口商付款,并且想要完美的结果?

恐怕我帮不了你。 Interop 每次都会给您带来完美的结果,而且您​​已经拥有了该软件。如果你不使用它,你将不得不做出牺牲——要么是成本,要么是质量。

Installing Office 2007 and using the Word 12 Object Library is definitely the option I'd go for (and have done so on some of my own projects).

If you don't want to install Word on a production web server, why not have it on a secondary server. You can get this second server to communicate with the first (using a web service or something like that) -- it could request the next Word document that needs exporting, do the conversion and then return the PDF data.

Let me know if you want a C# example of the Word automation that does this conversion (it's very trivial).

Adrian


Here's my code, posted for Jason. This works with Word 2007. You need to download and install the PDF exporter from the Office web site:

using Microsoft.Office.Interop.Word;

...

object _read_only = false;
object _visible = true;
object _false = false;
object _true = true;
object _dynamic = 2;
object  _missing = System.Reflection.Missing.Value;

object _htmlFormat = 8;        
object _pdfFormat = 17;
object _xpsFormat = 18;

object fileName = "C:\\Test.docx";

ApplicationClass ac = new ApplicationClass();
//ac.Visible = true; // Uncomment to see Word as it opens and converts the document
//ac.Activate();

Document d = ac.Documents.Open(ref fileName, ref _missing, ref _true, ref _read_only, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _visible, ref _missing, ref _missing, ref _missing, ref _missing);

object newFileName = ((string)fileName).Substring(0, ((string)fileName).LastIndexOf(".")) + ".pdf";

d.SaveAs(ref newFileName, ref _pdfFormat, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing);

d.Close(ref _false, ref _missing, ref _missing);
ac.Quit(ref _false, ref _missing, ref _missing);

ac = null;

So, Soul_Master, what you are saying is that you don't want to use interop (though you don't say why, which I'd be interested to know), you don't want to pay for a commercial exporter, and you want perfect results?

I cant help you, I'm afraid. Interop will give you perfect results, every time, and you already have the software. If you won't use that, you are going to have to make a sacrifice -- either cost or quality.

溺孤伤于心 2024-08-10 05:28:34

我已经使用名为 Free Spire.Doc 的免费组件解决了 docX 到 pdf 转换的问题。您可以在此网站上查看更多相关信息。您的使用非常简单。

I've solved my issues with docX to pdf conversion with a free component called Free Spire.Doc. You can see more about it on this site. It's quite easy your usage.

飘然心甜 2024-08-10 05:28:34

Open Office 有 API 吗?那至少是一个免费的选择?

使用 ActiveReports/Crystal Reports 等报告工具创建 PDF(比使用 iTextSharp 容易得多)

Does Open Office have an API? that would at least be a free option?

Create the PDF using a Reporting Tool like ActiveReports/Crystal Reports (much easier than using iTextSharp)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文