Hello all, I'm new to MigraDoc,
I use the GDI Version with the following Assembly Versions:
Code:
MigraDoc.DocumentObjectModel-gdi.dll v.1.50.4740.0
MigraDoc.Rendering-gdi.dll v.1.50.4740.0
MigraDoc.RtfRendering-gdi.dll v.1.50.4740.0
PdfSharp.Charting-gdi.dll v.1.50.4740.0
PdfSharp-gdi.dll v.1.50.4740.0
and
MigraDocPrinting.dll v.1.0.0.0
with contains the MigraDocPrintDocument.cs from MigraDoc 1.32 with some minor modification.
My Goal: The project (WinForm in .NET 3.5) should create a document, that can be previewed on the forum, can be printed, and can be saved as PDF and RTF Fileformat. The document has to support the insert of images (currently jpg format).
My Problem: I can create the document correct, for WinForm Preview, Printing and PDF. But RTF gives a problem.
When opening the RTF file with LibreOffice Writer (v.6.0.4) the embedded image can be displayed.
But when I open the same RTF file with Windows (7 & 10) Wordpad, the image is not loaded/displayed.
What I guess is wrong:I currently presume that the RTF Renderer writes the Image in a MetaFile Format into the RTF that Wordpad does not support or can not read correctly.
My Question:
Is there something I can do to creat Wordpad compatible RTF's in MigraDoc that include a Image?
If so, how?
Dropbox links to my output files:mdddl File:
https://www.dropbox.com/s/alznguf962tkqqh/Migra_Test_Mdddl.mdddl?dl=0rtf document:
https://www.dropbox.com/s/0lim0zwg52qyn5w/Migra_Test_Rtf.rtf?dl=0(Wordpad does not display the embbeded image, but libre office does)
The used image:
https://www.dropbox.com/s/8x5lhsbmvjftm9p/test_image.JPG?dl=0And here the Source Code class I wrote to create and render a MigraDoc Document:
Code:
using System;
using MigraDoc;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using MigraDoc.RtfRendering;
using MigraDoc.DocumentObjectModel.IO;
using MigraDoc.Rendering.Printing;
using System.Windows.Forms; //For PrintPreview Dialog
namespace mdTestNs
{
internal class Cls_MigraDoc
{
//Variabeln
private const String MY_TESTDIR = @"D:\1MigraTest\"; //Test Directory for writing the files
private const String MY_MDDDL = @"Migra_Test_Mdddl.mdddl"; //MDDDL File to Save
private const String MY_PDF = @"Migra_Test_Pdf.pdf"; //PDF File to Save
private const String MY_RTF = @"Migra_Test_Rtf.rtf"; //RTF File to Save
private Frm_MainForm frm_Main; //Referenz zur Hauptform
private Document document; //MigraDoc Document
/// <summary>Klassen Konstruktor</summary>
/// <param name="frm_Stimmbrief"></param>
internal Cls_MigraDoc(Frm_MainForm p_MainForm)
{
frm_Main = p_MainForm;
}
/// <summary>Create a MigraDoc Test-Document</summary>
internal void Migra_Create_Document_Content(String fileImage)
{
document = new Document(); //Create a new MigraDoc
Section section = document.AddSection(); //Add a Section
Paragraph paragraph = section.AddParagraph(); //Add a Paragraph
//Add a Image using a help methode
Migra_Image_Add(ref section, ref paragraph, fileImage, 175);
paragraph.AddLineBreak(); //Add a LineBreak
//Add some Formated Text so Document is not so Empty when viewed
paragraph.Format.Font.Color = Color.FromRgb(255,0,0); //Migra Color Objekt
FormattedText ft = paragraph.AddFormattedText("Hello World. #1", TextFormat.Underline);
ft.Font.Size = 16;
paragraph.AddLineBreak();
//Save to Mdddl using a help methode
Migra_Save_File_Mdddl(document, MY_TESTDIR+MY_MDDDL);
//Save to Rtf using a help methode
Migra_Save_File_RTF(document, MY_TESTDIR+MY_RTF);
//Save to Pdf using a help methode
Migra_Save_File_PDF(document, MY_TESTDIR+MY_PDF);
//Update Form Preview using a help methode
Migra_Preview_Document();
}
/// <summary>Add a Image into the Document</summary>
/// <param name="paragraph">(Ref) Paragraph in with the Image should be added.</param>
/// <param name="imageFile">Image File to that should be added.</param>
/// <param name="wanted_Weight">Prefered Width of Image in [point | pt]</param>
private void Migra_Image_Add(ref Section section, ref Paragraph paragraph, String imageFile, Int32 wanted_Weight)
{
if ((imageFile != "") && (System.IO.File.Exists(imageFile)))
{
//MigraDoc.DocumentObjectModel.Shapes.Image img = paragraph.AddImage(imageFile); //Create Image in Paragraph directly
MigraDoc.DocumentObjectModel.Shapes.Image img = section.AddImage(imageFile); //Create Section with Image
img.LockAspectRatio = true; //keep Image Aspect Ratio
img.Width = Unit.FromPoint(wanted_Weight); //Wanted Image Width in [point/ pt]
paragraph = section.AddParagraph(); //Add Section with Image to Paragraph
}
}
/// <summary>Update WinForm Preview Document Viewer with Document</summary>
private void Migra_Preview_Document()
{
//Update Viewer via Ddl String
//String ddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document); //Create String
//frm_Main.migra_Doc_Preview.Ddl = ddl; //Display String
//Update Viewer via Migra Document
frm_Main.migra_Doc_Preview.Document = document.Clone();
}
/// <summary>Save Document as MDDDL</summary>
/// <param name="document">Document to Save</param>
/// <param name="fileName">Filename to use for saving</param>
private void Migra_Save_File_Mdddl(Document document, String fileName)
{
DdlWriter dw = new DdlWriter(fileName);
dw.WriteDocument(document.Clone());
dw.Close();
}
/// <summary>Save Document as RTF</summary>
/// <param name="document">Document to Save</param>
/// <param name="fileName">Filename to use for saving</param>
private void Migra_Save_File_RTF(Document document, String fileName)
{
//Create Renderer
RtfDocumentRenderer rtf_Renderer = new RtfDocumentRenderer();
try
{ rtf_Renderer.Render(document.Clone(), fileName, null); } //Render and Save to RTF
catch (Exception ex)
{ MessageBox.Show($"{ex}"); } //Show Exception
}
/// <summary>Save Document as PDF</summary>
/// <param name="document">Document to Save</param>
/// <param name="fileName">Filename to use for saving</param>
private void Migra_Save_File_PDF(Document document, String fileName)
{
//Create Renderer
PdfDocumentRenderer pdf_Renderer = new PdfDocumentRenderer(true);
pdf_Renderer.Document = document.Clone(); //Hand renderer the docuemnt
pdf_Renderer.RenderDocument(); //Render Document
try
{ pdf_Renderer.Save(fileName); } //Save PDF file
catch (Exception ex)
{ MessageBox.Show($"{ex}"); } //Show Exception
}
/// <summary>Print Preview the Document</summary>
internal void Migra_Print_PrintPreview()
{
// Creates a PrintDocument that simplyfies printing of MigraDoc documents
// Code that uses the GDI build of PDFsharp/MigraDoc can use the MigraDocPrintDocument
// class and pass the object of Document class directly.
// Using an MDDDL will work with all builds, but will lose a few nanoseconds.
frm_Main.migraDocPrintDocument1 = new MigraDocPrintDocument(document);
frm_Main.printPreviewDialog2.Document = frm_Main.migraDocPrintDocument1; //Hand the PrintDocument to the Print Preview Dialog
frm_Main.printPreviewDialog2.ShowDialog(); //Show the Print Preview Dialog
}
}
}