C# PDF library for headless Linux server - c#

I'm currently trying to find a PDF library which will run without a running X server. I have already tried the following...
Migradoc/PDFSharp (requires X)
ITextSharp (requires X)
SharpPDF (might work, but I am looking for something with a bit more features)
The library does not have to be opensource or free.
My solution runs on Apache2.2 mod_mono.
Does anyone know of such library?
--- edit ---
The test code used for itextsharp, which produces errors on my testserver is listed below (the code for Migradoc and SharpPDF is just as simple):
using System;
using sharp=iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.IO;
namespace pdftester
{
public static class ITextSharpTest
{
public static void HelloWorld(string filename)
{
Stream stream = new FileStream(filename, FileMode.Create);
sharp.Document document = new sharp.Document();
PdfWriter.GetInstance(document, stream);
document.Open();
document.Add(new sharp.Paragraph("Hello world"));
document.Close();
}
}
}

Since no one has given a definitive answer to the thread, i'm closing it.
I've chosen to go the sharpPDF way, as it's the only one supported on my server. I'll simply have to implement what's needed for my project.
Thanks for the help received so far :)

Related

C#: StreamWriter program is not writing to text file

I know this question has probably been answered multiple times across this site, but even after looking at those solutions I don't have an answer to why my program isn't writing to the text file I have assigned. Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Main
{
class Program
{
public static void Main (string[] args)
{
using (StreamWriter writer = new StreamWriter("test.txt"))
{
writer.WriteLine("Hello, World!");
}
}
}
}
My program throws no exceptions and exits with 0, so I am not understanding how this is still not functioning properly. If someone could please provide an answer along with an explanation as to why this doesn't work, I would really appreciate it.
EDIT: Okay, I fixed the code after playing around a bit. Turns out that upon reading the file the text that I wrote is there. Thus, a clarification of this problem would be:
The text writes to the file, but it is not visible to me when I open up the file from my project in Visual Studio. I am not sure as to why, and this is leading to confusion
Your sample is correct. The file is saved into build path. (where is "yourApp.exe").
You can try with a abosule path to define where file will be save, for example StreamWriter writer = new StreamWriter(#"c:\test\test.txt")

How to convert ppt to HTML in C#?

In my website, admin can upload a PPT & on submission, I am in need to convert to html.
I was using OpenXML library for the Word document. I thought the same lib can be used for PPT also. But not finding the method for the same.
namespace OpenXML_Sample
{
class Program
{
static void Main(string[] args)
{
ExportHTML.GenerateHTML(#"D:\test.pptx");
Console.ReadKey();
}
}
public class ExportHTML
{
public static XElement GenerateHTML(string filePath)
{
try
{
byte[] byteArray = File.ReadAllBytes(filePath);
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (PresentationDocument pptDoc=
PresentationDocument.Open(memoryStream, true))
{
HtmlConverterSettings settings = new HtmlConverterSettings()
{
PageTitle = "My Page Title"
};
//not accepting pptDoc as parameter,throws compile time error.
XElement xHtml = HtmlConverter.ConvertToHtml(pptDoc, settings);
var html = xHtml.ToString();
File.WriteAllText(#"D:\sample.html", html,Encoding.UTF8);
return xHtml;
}
}
}
catch (Exception ex)
{
throw new FileLoadException(ex.InnerException.Message.ToString());
}
}
}
}
How do I pass the ppt document to the method to generate the html document of the uploaded ppt file.
Would welcome for any other(free) api as well.
I have used the Aspose library before and I believe it supports what you are wanting to achieve.
A quick search on their forums revealed this post which might suit your needs;
web,
I like to share that Aspose.Slides for .NET supports exporting presentation file to HTML and you don't even need to install MS Office for this on your machine. All you need to do is to use the appropriate functionality in API. Please visit this documentation link for your kind reference. If you still have an issue then please contact us in Aspose.Slides support forum.
I am working as Support developer/ Evangelist at Aspose.
There are some examples of converting in C# with iSpring Platform http://www.ispringsolutions.com/ispring-platform. It isn’t tailored for a certain programming language, but it’s easy to use it with C#. First of all, there are some examples, and secondly, there’s a Code Builder app, so you can set the necessary conversion configuration and use the generated C# code in your app.

Convert .doc to .docx using C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I convert PDF file to the word file using PDFFocus.net dll. But for my system I want .docx file. I tried different ways. There some libraries available. But those are not free. This is my pdf to doc convert code.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using iTextSharp.text;
Using iTextSharp.text.pdf;
namespace ConsoleApplication
{
class Program
{
static void main(String[] args)
{
SautinSoft.PdfFocus f=new SautinSoft.PdfFocus();
f.OpenPdf(#"E:\input.pdf");
t.ToWord(#"E:\input.doc");
}
}
}
This work successfully.
Then I tried with below code to convert .doc to .docx. But it gives me error.
//Open a Document.
Document doc=new Document("input.doc");
//Save Document.
doc.save("output.docx");
Can anyone help me please.
Yes like Erop said. You can use the Microsoft Word 14.0 Object Library. Then it's really easy to convert from doc to docx. E.g with a function like this:
public void ConvertDocToDocx(string path)
{
Application word = new Application();
if (path.ToLower().EndsWith(".doc"))
{
var sourceFile = new FileInfo(path);
var document = word.Documents.Open(sourceFile.FullName);
string newFileName = sourceFile.FullName.Replace(".doc", ".docx");
document.SaveAs2(newFileName,WdSaveFormat.wdFormatXMLDocument,
CompatibilityMode: WdCompatibilityMode.wdWord2010);
word.ActiveDocument.Close();
word.Quit();
File.Delete(path);
}
}
Make sure to add CompatibilityMode: WdCompatibilityMode.wdWord2010 otherwise the file will stay in compatibility mode. And also make sure that Microsoft Office is installed on the machine where you want to run the application.
Another thing, I don't know PDFFocus.net but have you tried converting directly from pdf to docx. Like this:
static void main(String[] args)
{
SautinSoft.PdfFocus f=new SautinSoft.PdfFocus();
f.OpenPdf(#"E:\input.pdf");
t.ToWord(#"E:\input.docx");
}
I would assume that this is working, but it's only an assumption.
Try to use Microsoft.Office.Interop.Word assembly.
An MSDN article can be found Here
Include references in your project, and enable their use in a code module via an example from the above link that shows
using System.Collections.Generic;
using Word = Microsoft.Office.Interop.Word;

PowerTools for OpenXml. Null reference when reading header/footer

i've got a task to convert docx document to Pdf. I've decided to take this approach:
convert docx to html, then pass html to ItextSharp. Few week i've been looking all over google, codeplex, sourceforge and stackoverflow and so on for a solution thow to make this conversion, until I found Eric White blog. At firs impression he made great tool for working with OpenXml documents. But when I tried to test it I've got an error about null reference. Error occurs when reading header (RevisionAccepter class)
public static void AcceptRevisions(WordprocessingDocument doc)
{
AcceptRevisionsForPart(doc.MainDocumentPart);
foreach (var part in doc.MainDocumentPart.HeaderParts) //part is null
AcceptRevisionsForPart(part); //null ref exception here
foreach (var part in doc.MainDocumentPart.FooterParts)
AcceptRevisionsForPart(part);
if (doc.MainDocumentPart.EndnotesPart != null)
AcceptRevisionsForPart(doc.MainDocumentPart.EndnotesPart);
if (doc.MainDocumentPart.FootnotesPart != null)
AcceptRevisionsForPart(doc.MainDocumentPart.FootnotesPart);
}
code I use for conversion (same as in example)
private void conv()
{
byte[] byteArray = File.ReadAllBytes(textBox1.Text);
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc =
WordprocessingDocument.Open(memoryStream, true))
{
HtmlConverterSettings settings = new HtmlConverterSettings()
{
PageTitle = "My Page Title"
};
XElement html = HtmlConverter.ConvertToHtml(doc, settings);
File.WriteAllText("Test.html", html.ToStringNewLineOnAttributes());
}
}
}
nameSpaces:
using System.Xml;
using System.Xml.Xsl;
using OpenXmlPowerTools;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
I tried to pass documents created by word 2010, with header and without and still getting errors in the same place. Maybe I'm passing document incorrectly or something with the document itself.
Maybe there is another way to convert docx to pdf without using comercial components, like Apose.
found the problem. Errors was occuring because of different references between power tools project and main project.
DocumentFormat.OpenXml version on my project was 2.5.5513.0 and on power tools - 2.0.5022.0
After making references to the same resource everything went fine.

Using embedded resources in C# console application

I'm trying to embed an XML file into a C# console application via Right clicking on file -> Build Action -> Embedded Resource.
How do I then access this embedded resource?
XDocument XMLDoc = XDocument.Load(???);
Edit: Hi all, despite all the bashing this question received, here's an update.
I managed to get it working by using
XDocument.Load(new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Namespace.FolderName.FileName.Extension")))
It didn't work previously because the folder name containing the resource file within the project was not included (none of the examples I found seemed to have that).
Thanks everyone who tried to help.
Something along these lines
using System.IO;
using System.Reflection;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApplication1.XMLFile1.xml");
StreamReader reader = new StreamReader(stream);
XmlDocument doc = new XmlDocument();
doc.LoadXml(reader.ReadToEnd());
}
}
}
Here is a link to the Microsoft doc that describes how to do it. http://support.microsoft.com/kb/319292

Categories