RTF ファイルを PDF ファイルに変換するにはどうすればよいですか? Adobe PDF プリンターを持っていますが、それを使用する必要がありますか? もしそうなら、どうすればプログラムでアクセスできますか?
4 に答える
PDFプリンターを使用することはできますが、それでも解決すべき問題がいくつかあります。
複数のページにまたがるテキストを処理するには、EM_FORMATRANGEメッセージを処理するRichTextboxの子孫を作成するために この記事が必要です。
そこにはたくさんの(無料の)PDFプリンターがありますが、出力のファイル名を制御できるのはBioPdfだけであることがわかりました。また、ライセンスバージョンに対してもリーズナブルな料金が適用されます。
これを使用して、電子メールの添付ファイルとして複雑なレポート(複数のRTFセグメントとカスタムグラフィックの組み合わせ)を作成しました。
仮想印刷ドライバー doPdf http://www.dopdf.com/を実稼働マシンで許可されている場合は使用できます。これにより、ほぼすべてのファイルタイプが rtf だけでなく pdf 形式に変換されます。インストールすると、Print Manager 内に別のプリンターとして表示されます。
それをwinformsコードで使用するために、msdnの印刷例http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspxにあるコードを適応させました
private void button1_Click(object sender, EventArgs e)
{
try
{
streamToPrint = new System.IO.StreamReader
(@"F:\temp\labTest.txt");
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "doPDF v6";//<-------added
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
私が追加する必要があったコードの唯一の部分は、上でマークされた部分でした。
よりエレガントで堅牢なプリンター列挙メソッドが存在する可能性があり、これに対して、おそらく構成ファイル設定に対してプリンタードライバーが存在するかどうかをテストできます。
更新: 複数のページの処理は、msdn サンプルに従って this.pd_PrintPage というメソッドで処理されます。PrintDocument は、ページからの印刷とページへの印刷をサポートします。DoPdf は fileSaveAsDialog ボックスを自動的にポップアップ表示するため、ファイルを PDF ドキュメントとして保存できます。
しかし、rtfはどうですか?Microsoft フォーマットはあまりサポートされていないようです。この記事http://msdn.microsoft.com/en-us/library/ms996492.aspxのデモ コードでは、RichTextBox を開始点として使用し、P/Invoke を使用して Win32 の機能を利用して RTF を WYSIWG として出力します。このコントロールは、上記のコード スニペットで使用されているものを置き換える独自のページ長メソッドを定義し、引き続き PrintDocument を使用するため、使いやすいはずです。Rtb.rtf メソッドを使用して任意の rtf を割り当てることができます。
RTF ドキュメントは、その形式を理解できる何らかのアプリによって読み取られ、解釈される必要があります。そのアプリをプログラムで起動し、RTF ファイルを読み込んで、PDF プリンターに送信する必要があります。Word は .NET インターフェイスが優れているため、これには適しています。手順の概要は次のとおりです。
ApplicationClass word = new ApplicationClass();
Document doc = word.Documents.Open(ref filename, ...);
doc.PrintOut(...);
名前空間を使用して、アセンブリMicrosoft.Office.Interop.Word
への参照を追加する必要があります。Microsoft.Office.Interop.Word.dll
実際、これらのどれもひどく信頼できるものではなく、私が望むこともありません。解決策は簡単です。Adobe Acrobat をインストールし、Process クラスを使用して RTF ファイルを開くだけです。
また、より合理的なアプローチを見つけました。ファイルを RTF として保存し、Word で開き、PDF として保存します (Word の Print As PDF プラグインをインストールする必要があります)。
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Personal Document File (*.pdf)|*.pdf";
if (sfd.ShowDialog() == DialogResult.OK) {
String filename = Path.GetTempFileName() + ".rtf";
using (StreamWriter sw = new StreamWriter(filename)) {
sw.Write(previous);
}
Object oMissing = System.Reflection.Missing.Value; //null for VB
Object oTrue = true;
Object oFalse = false;
Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
oWord.Visible = false;
Object rtfFile = filename;
Object saveLoc = sfd.FileName;
Object wdFormatPDF = 17; //WdSaveFormat Enumeration
oWordDoc = oWord.Documents.Add(ref rtfFile, ref oMissing, ref oMissing, ref oMissing);
oWordDoc.SaveAs(ref saveLoc, ref wdFormatPDF, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);
oWord.Quit(ref oFalse, ref oMissing, ref oMissing);
//Get the MD5 hash and save it with it
FileStream file = new FileStream(sfd.FileName, FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
using (StreamWriter sw = new StreamWriter(sfd.FileName + ".md5")) {
sw.WriteLine(sfd.FileName + " - " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString() + " md5: " + BinaryToHexConverter.To64CharChunks(retVal)[0]);
}
}