Creating a LaTeX Document in C#/WPF Application - c#

Has anyone ever created a .pdf document within their C#/WPF application using LaTeX? We're creating reports and displaying them to the screen and we've looked into creating the reports with HTML and LaTeX. The HTML option is easy, but creating a LaTeX document within C# seems non-trivial which I personally find a bit odd; I'd think people have done this many times before. Anyone know of any LaTeX libraries to look at or some other way of doing it?

I've been working on a C# port of the iosMath LaTeX library. As I write this, it produces good results in most cases. I'm working on the remaining ones.
https://github.com/verybadcat/CSharpMath/tree/master

I'm not aware of any LaTeX libraries and like you said it doesn't seem to be trivial to do so which I also find a bit strange. I've always written the documents myself using the TeXnic Center IDE which I've found really useful.
You could always try writing the source for the document to a file using a StreamWriter and then run a new pdflatex process through the command prompt in code to compile the document to a pdf?
Other than that I did manage to stumble across a list of open source pdf generating libraries for C# which might be worth taking a look at here (I don't think any of them are for LaTeX unfortunately):
http://en.csharp-online.net/Open_Source_PDF_Libraries
iTextSharp seems to be recommended quite a lot by people so it might be worth looking at that. Hopefully somebody else will be able to point you towards a LaTeX library but if they can't and running the pdflatex as a process through your code isn't viable then atleast these might give you a decent alternative.
Hope this helps.

This C# library 'KaTeX Sharp Runner' can parse raw LaTeX strings or expressions within markdown text. It wraps the KaTeX JavaScript library for use in .NET applications using Jint.
https://github.com/appsoftwareltd/katex-sharp-runner

I build Latex text in C#, which I then compile with a Process.Start() with pdfLatex.exe.
I built some routines to more easily (and less error-prone) build equations and tables of parameters in the style
length 2.3 m
mass 45.5 kg
Without these routines and just C#-write an equation as a long Latex string with all its comma's, (curly) braces etc. is hell.
An internationalizeable equation in my program looks like:
Equation
(
"equation_name_in_Swahili",
new List<string[]>{
new[] {$"F_{{{Str("cable")}}}", null}, eq,
new[] {"\\sqrt", "\\sqrt"},
curl1,
new[] {"(", "("},
new[] {$"F_{{{ltxGrav},X}}", $"{Fg * Math.Cos(Ag):G5}"},
plus,
new[] {$"F_{{{ltxWind},X}}", $"{Fw * Math.Cos(Aw):G5}"},
new[] {")", ")"},
new[] {"^2", "^2"},
plus,
new[] {$"F_{{{ltxGrav},Y}}^2", $"{Fg * Math.Sin(Ag):G5}^2"},
plus,
new[] {$"F_{{{ltxWind},Z}}^2", $"{Fw * Math.Cos(Aw):G5}^2"},
curl2
}
)
the first column is the equation with formal parameter names, and the second is the same, but with the values. You can guess what the "plus"and "dots"do.

Related

What is the most straightforward way of modifying PDF start-up options via C#?

I'd like to know what would be the most straightforward (i.e., preferably without adding DLLs to my solution) way to write a C# code snippet for modifying a PDF document's page layout and magnification options.
I know this is incredibly easy in LaTeX (well, it's easy to set these for a PDF to-be-generated using a single package) through hyperref options.
Modifying an already-created PDF document is a lot more work however. Also, most MS Office print-to-PDF options don't seem to include this ability while it's obviously annoying to keep opening documents up in Acrobat and manually modifying these for the proper (desired) opening settings (layout: single page, and magnification: fit page, for me).
So, I would like to write a code snippet that could do this. What is the most straightforward way of modifying PDF start-up options via C#?
Alternatively, is there a way to force-apply these options when generating PDFs from MS Office software suites?
I looked into PDFSharp and MigraDoc at one point for PDF-making from C#, but that really didn't match what I was hoping for. Plus, it added a DLL set (of ten dependencies) which was sub-optimal with respect to how many ancillary files were included with the build for managing a relatively simple function.
However, as noted in the comments, such a batch modification code is likely to be very difficult without any dependencies, so I'll also accept answers which reference dependencies.
Past topics (but not very good matches):
This is similar but for JavaScript, and actually suggests it shouldn't be done.
This is a bit more elaborate for JavaScript, but not exactly what I am looking for.
This topic seems to be this question for PHP but including some external references...

Create a workspace to work with PDF (C#) [duplicate]

I have several A4 PDF documents which I would like (two into one) "glue" together into A3 format PDF document. So I will get from 2PDFs A4 a single one sided PDF A3.
I have found the excellent utility PDFToolkit and some others but none of them can be used to "glue" side by side two documents.
I just came across a nice tool on superuser.com called PDFjam that can do all of the above in a single command:
pdfjam --nup 2x1 file1.pdf file2.pdf --outfile DONESKI.pdf
It has other standard features like page size plus a nice syntax for more sophisticated collations of pages (the tricky page re-ordering necessary for true booklet-style page imposition).
It's built on top of TeX which is, whatever it is. Installing is a breeze on Ubuntu: you can just apt-get install pdfjam. On Mac OS, I recommend getting BasicTeX (google "mactex basictex"; SO thinks I'm a spammer and won't let me post the link).
This is a lot easier and more maintanable than installing both pdftk and Multivalent (on both Mac OS for dev and Ubuntu for deploy), which wasn't going so well for me anyway...!
Found the following (free and open-source) tool for doing Imposition called Impose (thanks danio for the tip). This solved my problem perfectly.
EDIT:
Here is how it's done:
Use PDF Toolkit to joint two PDF files into one (two A4)
pdftk File1.pdf File2.pdf cat output OutputFile.pdf
Create from this a single page (one A3):
java -cp Multivalent.jar tool.pdf.Impose -dim 2x1 -verbose -paper-size "42.2x29.9cm" -layout "1,2" OutputFile.pdf
I would like to advertise my pdftools
It's written in Python so should run on any platform. It's a wrapper to Latex (the pdfpages packages) but can do lot of things with a single command line: merge pdf files, nup them (multiple input pages per output page) and number the pages of the output file (you specify the location and the format of the number)
It still needs some work but I think it's quite stable to be usable right now :)
This puts two landscape letter pages onto a single portrait letter sheet, to be "bound" (i.e., folded) along the top.
pdftops $1 - |
psbook |
pstops -w11in -h8.5in '4:1#.65(.5in,0in)+0#.65(.5in,5.5in),2U#.65(8in,5.5in)+3#.65U(8in,11in)' |
ps2pdf - $(basename $1 .pdf).psbook.pdf
By the way, I do this often, so I'll probably submit more "answers" to this question just to keep track of successful pstops pagespecs. Let me know if this is an inappropriate use of SO.
A nice, powerful, open-source imposition tool is included
in the PoDoFo package:
http://podofo.sourceforge.net/
It works for me. Some imposition plans can be found at:
http://www.av8n.com/computer/prepress/
PoDoFo can do lots of other stuff, not just imposition.
Another useful imposition tool is Bookbinder (on the
quantumelephant site). It has a GUI that appeals to non-experts.
It is not as flexible or powerful as PoDoFo, but it can do
imposition.
pdftk is more-or-less essential to have, but it will not
do imposition.
pdfjam is useless to me, because there are a wide range of
valid pdf files that it cannot handle.
I've never been able to get multivalent to work, either.
What you want to do is imposition. There are commercial tools to impose PDFs such as ARTS crackerjack and Quite imposing but they are pretty expensive (US$500), require a copy of acrobat professional and are overkill for imposing 2 A4 pages to an A3 sheet.
On the Postscript side, a tool named pstops is able to rearrange pages of a Postscript file in any way you could imagine. I've not heard of such a tool for PDF. But pdf2ps and ps2pdf exist. So a not-so-ideal solution may be a combination of pdf2ps, pstops and ps2pdf.
I would combine the two A4 pages into one 2-page PDF using pdftk. Then Print to PDF using something like PrimoPDF, and tell it to print to A3 format, two pages per side.
I just tested this printing some slides from PowerPoint. It worked great. I selected A3 as my paper size in PowerPoint, and then chose to print 2 pages per side. Printed to Primo and voila, I have two A4 slides per A3.
You can put multiple input pages on one output page using BookletImposer.
And you can change page orders and combine multiple pdf files using PDF Mod.
With these two tools, you can do almost everything you want with pdf files (except editing their content).
I had a similar problem. I tried Impose but it was giving me an
Exception in thread "main" java.lang.NoClassDefFoundError: tool/pdf/Impose
Caused by: java.lang.ClassNotFoundException: tool.pdf.Impose
(...)
Could not find the main class: tool.pdf.Impose. Program will exit.
I then tried PDF Snake which isn't free or open source, but has a completely unrestricted 30-day trial version. It worked perfectly, after tweaking the parameters to achieve what I wanted. It's a great tool. I would definitely buy it if it wasn't so expensive! Anyway, I thought I'd leave my 2 cents in case anyone had the same problem I had with Impose.
look at this
http://sourceforge.net/projects/proposition/
It needs laTex to run,
but when it does, works really fine
Regards

Parsing PCL for Text

Ok, so I know this is a crappy question but it has been driving me crazy all day...
I have a bunch of files containing raw PCL6/PCL XL code from printing jobs run to our printers. What I need to be able to do is somehow parse them so I can search for specific text.
Does anyone know if this is possible or understand PCL enough to suggest a reason why even on basic prints from say notepad the raw text doesn't seem to be visible within the code?
I suppose I should mention, I need to be able to code this into my C# app. Manual converters or the ability to print the pcl is not going to do what I want.
#mcalex is correct, PCL 6 (PCLXL) is a compiled binary. You can't read it. You need something that can decompile for you. Pagetech have some solutions for this. You could also look to convert to some other format where the data might be more readable. If the source could be generated in PCL 5 or PS you "might" have a better chance or reading the data directly (although not likely).

Translating comments and region names in source code

Does anyone know of a batch processor or a VS 2010 plugin/script that would let me translate comments and region names from Chinese into English?
The only ones I've found either process all strings or only one string at a time.
I have two large C# projects that I am trying to read through.
Thanks.
Use PrepTags to prepare your file for translation. It will allow you to select the text to be translated based on regex.
www.preptags.com
You can work file by file for free, or process the files as batch using the pro version (€39)
In your case, it's pretty simple to prepare. You just mark everything as protected, then unprotect the content of the comments & region names.
Disclosure: I develop PrepTags.
As was noted, you can use Google Translate API or alternatively Bing Translator API. You can detect comments and regions in your files using System.CodeDom.
I'm not too sure if this is possible. What you can do to help would be the following:
1) Make sure that both C# projects have the Properties > Build > Xml document file check box checked.
2.1) Write an application that reads in the generated xml file.
2.2) Parse the file, and for each value make a call to Google Translate to get the translated value.
2.3) Place the translated value within another xml file that has the same structure as the one created from building the project.
This wouldn't solve the your desire to translate the region names, but its a start. At least you would have intelligence when using the two projects.
This is actually a good idea for a small open source project. I may decide to pick it up. If I do, I'll let you know.

Free Math equation image generator

I'm looking for a component that generates math equations.
like what wikipedia has but for windows server.
if i enter x^2, it will generate a gif with proper formatting as output
It must run on IIS and .NET and not a web service from somewhere.
has anyone seen anything that does this?
Check this out. They have a LaTeX like equation editor, and apparently the can compile their code with Visual Studio. You might be able to get something working related to your question.
Well, one option would be to write your own parser - i.e. that takes "x^2" and builds a .NET 3.5 Expression to do the same. Once you have an Expression, you can use Compile() to get a delegate (Func<double,double>, for example) that you can use to generate the points. That isn't a huge amount of work (it took me about 2 hours to write a similar (though basic) parser a few months ago).
Of course, you'd still need to worry about the imaging side... maybe the .NET chart control released a few months ago? (it isn't just ASP.NET)
I am using MathJax to generate equations in websites,
basically I write HTML. I've had no problems with it.

Categories