Anonymous
Преобразование строки/*.rtf в PDF [закрыто]
Сообщение
Anonymous » 02 авг 2024, 19:47
Я пытался преобразовать и строку, и документ .rtf в документ PDF.
Пока что у меня был неоднозначный успех. Я попробовал 3 разных способа (код ниже), чтобы превратить текст в PDF-документ. Все они компилируют, запускают и создают PDF-файл, но во всех случаях документ оказывается поврежденным.
Ошибка: Ссылка
(
https://www.dropbox.com /s/j7sckgwm10gf5ml/PdfError.jpg?dl=0)
Написание потока с обычным текстом для его преобразования в документ 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();
Аналогично способу 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:
Код: Выделить всё
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: В случаях 2 и 3 я загружаю документ RTF, который содержит специальные (немецкие) символы, в случае 1.
Это простая строка, которую вы видите в коде, но заканчивается она одинаково (см. изображение с ошибкой выше)
Надеюсь, кто-нибудь сможет мне помочь или указать правильное направление.
Подробнее здесь:
https://stackoverflow.com/questions/312 ... rtf-to-pdf
1722617233
Anonymous
Я пытался преобразовать и строку, и документ .rtf в документ PDF. Пока что у меня был неоднозначный успех. Я попробовал 3 разных способа (код ниже), чтобы превратить текст в PDF-документ. Все они компилируют, запускают и создают PDF-файл, но во всех случаях документ оказывается поврежденным. Ошибка: Ссылка (https://www.dropbox.com /s/j7sckgwm10gf5ml/PdfError.jpg?dl=0) [list] [*]Написание потока с обычным текстом для его преобразования в документ PDF . [code] 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(); [/code] [*]Аналогично способу 1.: [code] //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); [/code] [*]И более сложная попытка 3: [code] 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(); } } [/code] [/list] PS: В случаях 2 и 3 я загружаю документ RTF, который содержит специальные (немецкие) символы, в случае 1. Это простая строка, которую вы видите в коде, но заканчивается она одинаково (см. изображение с ошибкой выше) Надеюсь, кто-нибудь сможет мне помочь или указать правильное направление. Подробнее здесь: [url]https://stackoverflow.com/questions/31255141/converting-string-rtf-to-pdf[/url]