pouët.net

Go to bottom

RTF2BMP

category: residue [glöplog]
 
Anyone has code, preferably in C#, that renders an RTF document to a Bitmap object? TIA.
added on the 2018-06-11 15:22:09 by Adok Adok
Since it's cheap to just ask but not share code, I will share my code that I am using now with you. I am not satisfied with this solution because if a document is longer than a page, it makes the screen blink, and that's ugly. That's why I'm asking for a solution that renders the RTF directly to a Bitmap instead of rendering it to the screen and capturing the screen.

Code: private void GetCompleteProjektbeschreibung_Prepare() { PnMain_ScrollToTop(); pnGebaeudestandort.Visible = false; backupHeight = pnProjektbezeichnung.Height; backupFormSize = this.Size; backupRtf = projektbezeichnung.Projektbeschreibung.Rtf; this.Size = this.MaximumSize; } private void GetCompleteProjektbeschreibung_Close() { projektbezeichnung.Projektbeschreibung.Rtf = backupRtf; pnProjektbezeichnung.Height = backupHeight; this.Size = backupFormSize; pnGebaeudestandort.Visible = true; } // 2018.05.23 // 2018.05.24 private Bitmap GetCompleteProjektbeschreibung_Page1() { int standardHeight = projektbeschreibung_page1_height; int newHeight = projektbezeichnung.Projektbeschreibung.GetPositionFromCharIndex(GetRealCharIndex (standardHeight)).Y; projektbezeichnung.DrawPanel(pnProjektbezeichnung, this.pnMain.Width, true, newHeight, true); Bitmap projektbeschreibung = newHeight == 0 ? new Bitmap(projektbezeichnung.Width, 1) : projektbezeichnung.GetProjektbeschreibung(newHeight); return projektbeschreibung; } // 2018.05.23 // 2018.05.24 private Bitmap GetCompleteProjektbeschreibung_Page2(out Bitmap projektbeschreibung_part2, int prevPageHeight = projektbeschreibung_page1_height) { int standardHeight = projektbeschreibung_page2_height / 2; projektbezeichnung.DrawPanel(pnProjektbezeichnung, this.pnMain.Width, true, standardHeight, true); projektbezeichnung.Projektbeschreibung.Text = projektbezeichnung.Projektbeschreibung.Text.Substring(GetRealCharIndex(prevPageH eight)); string backupInternalRtf_state1 = projektbezeichnung.Projektbeschreibung.Rtf; int newHeight = projektbezeichnung.Projektbeschreibung.GetPositionFromCharIndex(GetRealCharIndex (standardHeight)).Y; Bitmap projektbeschreibung = newHeight == 0 ? new Bitmap(projektbezeichnung.Width, 1) : projektbezeichnung.GetProjektbeschreibung(newHeight); projektbezeichnung.Projektbeschreibung.Text = projektbezeichnung.Projektbeschreibung.Text.Substring(GetRealCharIndex(newHeight )); string backupInternalRtf_state2 = projektbezeichnung.Projektbeschreibung.Rtf; newHeight = projektbezeichnung.Projektbeschreibung.GetPositionFromCharIndex(GetRealCharIndex (standardHeight)).Y; if (newHeight == 0) { projektbeschreibung_part2 = new Bitmap(projektbezeichnung.Width, 1); projektbezeichnung.Projektbeschreibung.Rtf = backupInternalRtf_state1; projektbeschreibung = projektbezeichnung.GetProjektbeschreibung(standardHeight); } else { projektbeschreibung_part2 = projektbezeichnung.GetProjektbeschreibung(newHeight); projektbezeichnung.Projektbeschreibung.Text = projektbezeichnung.Projektbeschreibung.Text.Substring(GetRealCharIndex(newHeight )); newHeight = projektbezeichnung.Projektbeschreibung.GetPositionFromCharIndex(GetRealCharIndex (standardHeight)).Y; if (newHeight == 0) { projektbezeichnung.Projektbeschreibung.Rtf = backupInternalRtf_state1; projektbeschreibung_part2 = projektbezeichnung.GetProjektbeschreibung(standardHeight); } } projektbezeichnung.Projektbeschreibung.Rtf = backupInternalRtf_state2; return projektbeschreibung; } // 2018.05.24 private int GetRealCharIndex(int pageHeight) { int heightDiff; int CharIndex; do { Point P = new Point(0, pageHeight); CharIndex = projektbezeichnung.Projektbeschreibung.GetCharIndexFromPosition(P); heightDiff = projektbezeichnung.Projektbeschreibung.GetPositionFromCharIndex(CharIndex).Y - pageHeight; pageHeight--; } while (heightDiff > 0); return CharIndex; } // 2018.05.23 // 2018.05.24 private int Projektbeschreibung_GetNumPages() { int count = 1; while (projektbezeichnung.Projektbeschreibung.Width != projektbezeichnung.Projektbeschreibung.ClientRectangle.Width) { count++; Bitmap tempBitmap = new Bitmap(1, 1); GetCompleteProjektbeschreibung_Page2(out tempBitmap); } projektbezeichnung.Projektbeschreibung.Rtf = backupRtf; return count; } // 2018.05.17 private void PrintStandort() { GetCompleteProjektbeschreibung_Prepare(); int numPages = Projektbeschreibung_GetNumPages(); printImage = new Bitmap[numPages]; printImage_index = 0; PrintHeaderFooter(); // *** here comes code I am not allowed to share, so I've removed it Bitmap projektbeschreibung = GetCompleteProjektbeschreibung_Page1(); int posProjektbeschreibung_Y = posAlbedo_Y + marginTabelle + marginLine * 2; gdig.DrawLine(new Pen(new SolidBrush(Color.Black)), new Point(0 + marginLeft, posProjektbeschreibung_Y - marginLine), new Point(width_a4 - marginRight, posProjektbeschreibung_Y - marginLine)); gdig.DrawImage(projektbeschreibung, new Point(marginLeft, posProjektbeschreibung_Y + marginLine)); // 2018.05.23 // 2018.05.24 while (numPages > printImage_index + 1) { printImage_index++; PrintHeaderFooter(); Bitmap projektbeschreibung_nextPage; Bitmap projektbeschreibung_nextPage_part2; if (printImage_index == 1) { projektbeschreibung_nextPage = GetCompleteProjektbeschreibung_Page2(out projektbeschreibung_nextPage_part2); } else { projektbeschreibung_nextPage = GetCompleteProjektbeschreibung_Page2(out projektbeschreibung_nextPage_part2, projektbeschreibung_page2_height / 2); } int posProjektbeschreibung_nextPage_Y = customLogoHeight + headerSpacing + print_fontHeight + marginTop; gdig = Graphics.FromImage(printImage[printImage_index]); gdig.DrawImage(projektbeschreibung_nextPage, new Point(marginLeft, posProjektbeschreibung_nextPage_Y + marginLine)); gdig.DrawImage(projektbeschreibung_nextPage_part2, new Point(marginLeft, posProjektbeschreibung_nextPage_Y + marginLine + projektbeschreibung_page2_height / 2)); } GetCompleteProjektbeschreibung_Close(); printImage_index = 0; } // 2018.05.22 private string ConvertNumber(string src) { int index = src.IndexOf(" - "); if (index == -1) return src; return src.Substring(0, index); } private void PrintHeaderFooter() { // ********** Bitmap bmp = new Bitmap(width_a4, height_a4); bmp.SetResolution(dpi, dpi); Graphics gdig = Graphics.FromImage(bmp); // *** here comes code I am not allowed to share, so I've removed it gdig.Dispose(); printImage[printImage_index] = bmp; } // 2017.10.11 private PrintDocument printDocument1 = new PrintDocument(); Bitmap[] printImage; // 2017.10.11 // 2018.05.17 // 2018.05.23 // 2018.05.24 private void BtnPrint_Click(object sender, EventArgs e) { // 2018.05.24 PrintPreviewDialog dialog = new PrintPreviewDialog(); dialog.PrintPreviewControl.BackColor = standardColor; dialog.ClientSize = new Size(width_a4 * 4/5, height_a4 * 4/5); PrintController pc1 = new StandardPrintController(); printDocument1.PrintController = pc1; switch (CurrentPage) { case Form2_Page.Standort: PrintStandort(); dialog.Document = printDocument1; dialog.ShowDialog(); break; case Form2_Page.Auswertungsdatum: // print option should not be available here return; case Form2_Page.Orientierungen: // print option should not be available here return; default: CaptureScreen(); dialog.Document = printDocument1; dialog.ShowDialog(); break; } } // 2017.10.11 // 2018.05.23 private void CaptureScreen() { Graphics myGraphics = this.CreateGraphics(); Size s = this.Size; printImage = new Bitmap[1]; printImage_index = 0; printImage[0] = new Bitmap(s.Width, s.Height, myGraphics); Graphics memoryGraphics = Graphics.FromImage(printImage[0]); memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s); } // 2017.10.11 // 2018.03.20 // 2018.05.23 private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e) { try { int newWidth = Math.Min(width_a4, printImage[printImage_index].Width); int newHeight = printImage[printImage_index].Height * newWidth / printImage[printImage_index].Width; e.Graphics.DrawImage(printImage[printImage_index], 0, 0, newWidth, newHeight); } catch { } printImage_index++; if (printImage_index >= printImage.Length) e.HasMorePages = false; else e.HasMorePages = true; }
added on the 2018-06-11 15:47:47 by Adok Adok
I don't have a solution for you, but I'm really curious about those datestamp comments. I've never seen that before. What exactly is the system there? Adding a datestamp for every change made?
added on the 2018-06-11 16:08:28 by fizzer fizzer
What's with the mix of German and English?
Jesus Christ projektbezeichnung
added on the 2018-06-11 17:59:41 by Preacher Preacher
Adding a datestamp whenever a change is made, yes.
added on the 2018-06-11 19:04:40 by Adok Adok
Preacher: have a nice cup of Projektbeschreibung
added on the 2018-06-11 19:15:59 by keops keops
Quote:
Adding a datestamp whenever a change is made, yes.


How do you remember which change it was?
You must be hiding something.

How come PrintStandort() is stamped 2018.05.17 but the inner while loop is stamped 2018.05.23 and 2018.05.24?
I keep a logfile.
added on the 2018-06-11 22:43:23 by Adok Adok
If this is how your company does version control, I'd run away very quickly.
Why have I been using git all those years while I could have just manually added datestamps?!
Can you show us an example of what the log file contains? I couldn't sleep wondering about PrintStandort() having a timestamp that doesn't reflect the last modification of the inner while loop
projektbezeichnung
projektbeschreibung_nextPage_part2
GetCompleteProjektbeschreibung_Page2
added on the 2018-06-12 12:30:03 by visy visy
BB Image
added on the 2018-06-12 15:10:55 by keops keops
I do not owe to explain you my system of keeping logfiles, I am asking for a solution to the problem described in the original posting. If you have nothing to contribute then please STFU.
added on the 2018-06-12 15:34:24 by Adok Adok
Code:private void Projektbeschreibung_STFU() { ... }
added on the 2018-06-12 15:49:38 by keops keops
ReadTheFuck2BadMoodParty();
added on the 2018-06-12 15:54:20 by skarab skarab
Quote:
I do not owe to explain you my system of keeping logfiles, I am asking for a solution to the problem described in the original posting. If you have nothing to contribute then please STFU.


Adok, I'm sorry you feel this way. You just got me very interested in that log system. I should indeed have added "please".

I have another question. What's with the methods that don't have timestamp. I understand they were never modified. But how do you know when they were written?
Is the logfile a semantic description of what you did, or is it some kind of auto-generated diff?

This is important.
added on the 2018-06-12 20:22:13 by fizzer fizzer
The former.
added on the 2018-06-12 22:01:43 by Adok Adok
Dude, srsly. Have a look at GIT. All u do here manually does it for you. And it is integrated in many IDE's and editors. With Visual-Studio-Code + its Gitlens plugin it is literally a one-click to initialize a pool.

Git in Visual Studio Code
BB Image
added on the 2018-06-13 00:18:21 by Salinga Salinga
This thread was not intended to be about GIT.
added on the 2018-06-13 04:22:12 by Adok Adok
Have you tried stackoverflow? It might be a better venue to ask a question like this. Or google maybe? In any case, I suggest you deobfuscate (=degermanize) your code before posting it somewhere.
added on the 2018-06-13 09:22:10 by Preacher Preacher

login

Go to top