I have been trying to convert, both, a string and a .rtf document to a PDF document. So far I had mixed success. I have tried 3 different ways (code below) to turn the text into PDF. All of em compile, run and create a PDF, but in all casses the document ends up corrupted. The error: Link (https://www.dropbox.com/s/j7sckgwm10gf5ml/PdfError.jpg?dl=0)
Turning a simple string into a PDF document
String msg = "dfsgfdsgdfgfdf"; byte[] messageBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(msg); System.IO.FileStream stream = new System.IO.FileStream(@"C:\\file.pdf", System.IO.FileMode.CreateNew); System.IO.BinaryWriter writer = new BinaryWriter(stream); writer.Write(messageBytes, 0, messageBytes.Length); writer.Close();
Similar to method to method 1.:
//String myString: Contains the rft document loaded from the RTF file byte[] messageBytes = Encoding.UTF8.GetBytes(myString); System.IO.File.WriteAllBytes(@"C:\foo.pdf", messageBytes);
And the more complicated attempt 3:
public void wordToPDF() { ApplicationClass wordApplication = new ApplicationClass(); Document wordDocument = null; object paramSourceDocPath = @"C:\rtfdoc.rtf"; object paramMissing = Type.Missing; string paramExportFilePath = @"C:\pdfdoc.pdf"; WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatXPS; bool paramOpenAfterExport = false; WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint; WdExportRange paramExportRange = WdExportRange.wdExportAllDocument; int paramStartPage = 0; int paramEndPage = 0; WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent; bool paramIncludeDocProps = true; bool paramKeepIRM = true; WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks; bool paramDocStructureTags = true; bool paramBitmapMissingFonts = true; bool paramUseISO19005_1 = false; try { // Open the source document. wordDocument = wordApplication.Documents.Open( ref paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); // Export it in the specified format. if (wordDocument != null) wordDocument.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing); } catch (Exception e) { MessageBox.Show(e.Message); } finally { // Close and release the Document object. if (wordDocument != null) { wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null; } // Quit Word and release the ApplicationClass object. if (wordApplication != null) { wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
PS: In cases 2 and 3 I load a RTF document, which contains special (german) characters, in case 1. it is just the simple string you see in the code, yet it all ends up the same way (see image with the error above)
I hope someone can help me or point me into the right direction.