Is it at all possible to convert a document to PDF or edit a PDF in C# using only free software?

Deadly 提交于 2019-12-22 16:43:40

问题


I had this stupid idea of creating a template as a .docx or .rtf or .pdf and then replacing the text in that document to generate reports. This seemed like a better way of doing it than using paid reporting software.

Well, I believe I've tried just about everything now and I'm amazed at how impossible it is to do anything with pdfs.

Try 1

HTML -> PDF

A lot harder to design the template. It doesn't look the same when you print it. Never got it working outside of a command line example (not sure how well, say, iTextSharp-LGPL would even work or if it could handle base64 strings as I'm not sure how else you are going to tell it about images). In any case, doing it this way makes it too hard to design the template.

Try 2

OpenXml -> PDF

I stupidly assumed that because Word could save as PDF that OpenXml could to. I was wrong. It cannot save as a PDF.

Try 3

OpenOffice/LibreOffice (docX -> PDF)

It can't read OpenXml which is a problem because I was editing the template as OpenXml and then saving that result (as a .docx) but it can't read that saved document.

Try 4

iTextSharp LGPL

This one just doesn't work, lol. And apparently even though when you google "convert rtf to pdf" the ONLY thing that comes up is iText and its derivatives it doesn't convert rtf documents to pdf documents. I verified this myself (it only saves the text not the formatting) and later found this post to convince me I wasn't doing something wrong.

Try 5

PDF -> PDF

Since converting ANYTHING to a PDF seems to be impossible maybe I can save the template as a PDF and just do a text replace on that. Nope, lol, that is apparently a very difficult thing to do.

Try 6

Pandoc (.odt/.docx -> pdf), (.rtf -> .pdf not supported)

pandoc mockup2.odt -s -o mockup2.pdf

link to the files in the picture. *note, it messes up in the same way if you try converting .odt/.docx to .tex.

What do I do here? Buy software so that I can save a file as PDF? Is that the only option?


回答1:


I have a solution. I'm not saying it's the best solution. LibreOffice (or possibly OpenOffice if you are so inclined) accepts command line arguments that will do the switch.

soffice.exe --headless --convert-to pdf mockup.odt

*note - this is after I added libreoffice to my path (C:\Program Files\LibreOffice\program). idk why it's called soffice.exe instead of libreoffice.exe.

  • where i found the answer
  • relevant documentation



回答2:


I might have a working solution for you, if you have stuck with the docx-file for the template. I found one free solution for docx to pdf converting, without using microsoft.interop, etc.: See first answer in this stack overflow post

It uses two tools: The open xml power tools and DinkToPdf (Which is essentially a wkhtmltopdf wrapper). The html to pdf part works just fine, but the docx to html part looks like a catastrophe at first. You can fix this with custom css (There are some resources online).

Powertools-.NetStandard

DinkToPdf-GitHub

There are more possibilities for proprietary software, like Asposes.Words and Syncfusion file-formats. Most of the proprietary solutions are pretty expensive...

If you are just working on a Windows Environment, where MS-Office is installed, you can use Microsoft.Interop. It is by far the easiest solution (In this post, Interop is mentioned several time Stackoverflow Word to PDF

If you found another (better) working solution, please let me know. I am still have not decided if I will use a proprietary or the free one. :-)



来源:https://stackoverflow.com/questions/55066373/is-it-at-all-possible-to-convert-a-document-to-pdf-or-edit-a-pdf-in-c-sharp-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!