将字符串/ * .rtf转换为PDF

时间:2015-07-06 20:36:35

标签: c# pdf rtf

我一直在尝试将字符串和.rtf文档转换为PDF文档。到目前为止,我取得了成功。我尝试了3种不同的方式(下面的代码)将文本转换为PDF。所有的人都编译,运行和创建PDF,但在所有casses中,文档最终都被破坏了。 错误:Linkhttps://www.dropbox.com/s/j7sckgwm10gf5ml/PdfError.jpg?dl=0

  1. 将简单字符串转换为PDF文档

    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. 与方法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. 更复杂的尝试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();
         }        
     }
    
  4. PS:在案例2和3中,我加载了一个RTF文档,其中包含特殊(德语)字符,如果是1,它只是您在代码中看到的简单字符串,但它们都以相同的方式结束(请参阅带上述错误的图片)

    我希望有人可以帮助我或指出我正确的方向。

1 个答案:

答案 0 :(得分:1)

对于解决方案3,尝试更改paramExportFormat:

object paramExportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;