0

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)

  1. 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();
    
  2. 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);
    
  3. 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.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • 1
    How about using a pdf library like http://sourceforge.net/projects/itextsharp/ or http://pdfsharp.codeplex.com/ – EZI Jul 06 '15 at 20:39
  • 1
    Solution 1) and 2) can't work because you exported text only without any formatting. Standard pdf readers expect some encapsulation. – Graffito Jul 06 '15 at 20:58
  • There is no way you will be able to implement a reliable PDF writer based on answers to a question like this. There is WAY more to the PDF format than can be addressed in a single Stack Overflow question. Either use a library, or get a copy of the specification (freely available from Adobe). – Peter Duniho Jul 06 '15 at 21:19
  • So there is no short solution to turn a rtf into pdf? As microsoft office, can save as... pdf, I hoped there would be at least a API or something that executes that same procedure. :/ – user3557388 Jul 07 '15 at 17:11

1 Answers1

1

for solution 3, try to change paramExportFormat:

object paramExportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Graffito
  • 1,658
  • 1
  • 11
  • 10
  • After doing that I get a runtime error: https://www.dropbox.com/s/ujhanzidpbezlgp/RuntimeErr.png?dl=0 – user3557388 Jul 06 '15 at 23:06
  • Change the first parameter of wordApplication.Quit by Word.WdSaveOptions.wdDoNotSaveChanges. – Graffito Jul 06 '15 at 23:41
  • Doing so results in the following runtime error: – user3557388 Jul 06 '15 at 23:55
  • Does your code look like that: _object wdsaveoption = Word.WdSaveOptions.wdDoNotSaveChanges ; wordApplication.Quit(ref wdsaveoption , ref paramMissing, ref paramMissing);_ Did you try to open the generated pdf file ? – Graffito Jul 07 '15 at 00:20
  • I get a compiler error in that line: Error 2 Cannot implicitly convert type 'Microsoft.Office.Interop.Word.WdSaveFormat' to 'Microsoft.Office.Interop.Word.WdExportFormat'. An explicit conversion exists (are you missing a cast?) EDIT: Changed it to: Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF; and now it works, Graffito I could kiss you. * user3557388 starts dancing of joy in he's room* – user3557388 Jul 07 '15 at 17:15