PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 12:50 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jul 05, 2018 2:32 pm 
Offline

Joined: Thu Jul 05, 2018 1:52 pm
Posts: 2
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=0
rtf 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=0

And 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
      }
   }
}


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 05, 2018 2:56 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!

Do the pictures show correctly in Word?

SteveKirchmer wrote:
Is there something I can do to creat Wordpad compatible RTF's in MigraDoc that include a Image?
Can you create RTF files with images that show correctly in WordPad?
We just have to know which RTF elements used by MigraDoc are not compatible with WordPad and which RTF elements should be used instead (without changing the appearance of images in Word).

WordPad has its limitations and maybe this task cannot be solved.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 06, 2018 10:19 am 
Offline

Joined: Thu Jul 05, 2018 1:52 pm
Posts: 2
Hello Thomas,

Thomas Hoevel wrote:
Do the pictures show correctly in Word?

I Tested with MS Office - Word 2003 and MS Office - Word 2010, as well as Libre Office - Writer (V6),
all three do display the embedded Image. Windows 7 WordPad and Windows 10 WordPad do on the other hand not display the image. (All 5 programms loaded the same rtf file, with is the one I linked in #1)

Thomas Hoevel wrote:
Can you create RTF files with images that show correctly in WordPad?

Not with MigraDoc assembly. (At last the code I use in post #1 does not create a rtf, where the Image is displayed from WordPad)

I can create RTFs where WordPad shows a Image, by the following methode:
1. Put a RichTextBox .NET Control on my WinForm form.
2. Use the Windows Clipboard to copy & past a loaded image into the RichTextBox.
3. Use the RichTextBox save function to save the RichText as a RTF file.
(This is not a elegant way of doing it, and also reason why I seek for alternatives like MigraDoc)

A familar approach can also be done by using the .NET WPF RichTextBox, but my application is based on WinForm, whit why I mention that.

If you like I can post a download link for a RTF document I create with the WinForm RichTextBox, in case that helps any.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 138 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group