I've got a WPF control with several text boxes on it. The number of controls is variable depending on what the user does in the program. What I need to do is take the text from the text boxes and write it to a XPS doc. What the question really boils down to is "How do I write lines of text to a XPS doc? Does anybody know of a library I can incorporate or what the best way to do this is?
Here is simplest sample I saved to my OneNote some time ago (sorry I dont remember the source):
PrintDocumentImageableArea area = null;
XpsDocumentWriter wr = PrintQueue.CreateXpsDocumentWriter(ref area);
var text = new TextBlock() {Text = "Hello there"};
text.Margin = new Thickness(area.OriginWidth, area.OriginHeight, 0, 0);
Size outputSize = new Size(area.MediaSizeWidth, area.MediaSizeHeight);
text.Measure(outputSize);
text.Arrange(new Rect(outputSize));
text.UpdateLayout();
wr.Write(text);
Related
I have a RichTextBox in which I write text and I give it color and format and what I want is that when I press a button that I have programmed, I open the Word application and pass the text that I have in the RichTextBox to the Word document, along with the color and format that I have given in my application.
I have the following code that opens Word and it passes the text that I had in the RichTextBox but the problem is that it does not show me the color and format that I had in the text in my application.
colorLetra = new ColorDialog();
objWord = new Word.Application();
objWord.Visible = true;
objDocumento = objWord.Documents.Add(Missing.Value);
objWord.Selection.Font.Color = objWord.Selection.Font.Color;
objWord.Selection.TypeText(richTextBox.Text);
Could you tell me why it does not show me the color and format in Word?
Your question is:
Could you tell me why it does not show me the color and format in Word?
The reason is because you only enter/type the text. You don't apply any formatting. You're simply transferring the string value of the Windows Forms control to the Word document, as a string.
Your implied question is: How do I pass formatted RichTextBox content to Word...
There is no way to directly pass formatted information from a Windows Form to a Word document. You must go over the clipboard, as was suggested in a comment. The code that comments points to, however, is incorrect for formatted text. The following works for me:
if (richTextBox.Text.Length > 0)
{
// Copy the formatted content to the clipboard
Clipboard.SetText(richTextBox.Rtf, TextDataFormat.Rtf);
objWord.Selection.Paste();
}
Question Revised and clarified (Thanks to Bruno for point me in the right direction)-
This post was originally made under great sleep deprivation and after reading, I see how it could be confusing. I want to make sure that others have this solution in the future and not have to spend loads of time trying to figure it out.
Here is my question and there is an answer below that solves it.
I have a pdf form and I need to add an image to a specific location where I have put a place holder. How can I do this?
Just for future people who may be looking for the answer on how to best add an image to a pre-defined specific location into a pre-existing .pdf form, here is the answer.
/* This codes works for those who have a pre-created pdf form with a button in the location and size you want your image.
I reccomend acrobat pro, thought it has a monthly cost. There are additional free PDF editors
instantiate a new PdfStamper that will create a new file from my existing form. The Reader reads in your template and the stamper makes a copy
FileToPath and FileFromPath are strings i created above that hold the path
*/
using (PdfStamper stamper = new PdfStamper(new PdfReader(fileFromPath), File.Create(FileToPath)))
{
//get my buttons positions current location and height btn1 is the name of my button that exists in the pdf already
AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions("btn1")[0];
//create a new button utilze the field position to set it's location. FYI this is a rectangle. I reccomend you read about those
//it will save you tons of time to understand them.
PushbuttonField imageField = new
//btn1Replaced is what I named the new button that will overwrite the old place holder button
PushbuttonField(stamper.Writer, fieldPosition.position, "btn1Replaced");
//Here I set they layout from my old button to my new one
imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
//grab the image you want in your pdf imgPath is a string I wrone above to grab the image
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("imgPath");
//set your buttons image property to be your image you just grabbed
imageField.Image = img;
//always scale to the size of the button
imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
imageField.ProportionalIcon = false;
//make sure your button is read only and then it will not act like a button, it will act like an image.
imageField.Options = BaseField.READ_ONLY;
//Get rid of the old button
stamper.AcroFields.RemoveField("btn1");
//add my button and make sure it is on the correct page
stamper.AddAnnotation(imageField.Field, fieldPosition.page);
stamper.Close();
}
This question already has answers here:
Bold a single word within a sentence with iTextSharp
(3 answers)
Closed 7 years ago.
I am writing a simple program on CSharp. What it does, is that you write a text(in a richTextBox), and it saves it's content on a .pdf in a specific path. Now, i want the user to target a specific word/phrase, and by clicking a button, the specific word/phrase will go bold. Is this possible ?? And if yes, how ??
First of all check this out : http://www.mikesdotnetting.com/article/81/itextsharp-working-with-fonts
What basically you need to do is to call the SetFieldProperty function
You take your old pdf, edit it and out put a new PDF with a bolded textbox.
MemoryStream stream = new MemoryStream(); //Memory stream to with new pdf with changed bold text
PdfReader pdfReader = new PdfReader("file.pdf"); //The original PDF
PdfStamper stamper = new PdfStamper(pdfReader, stream); //A stamper to create the pdf
SetFieldProperty("fieldName","textfont",BaseFont.COURIER_BOLD,null); //Change textbox properties
SetField("fieldName","TEXT"); //Change field text
stamper.Close(); //Save and close
byte[] newFile = stream.ToArray(); //Here you have your new file with the bolded text
This code will give you a new file with the bolded text
I think you can input an html to itextsharp. This way you can control many styling attributes using html. for ex: This is bold text. can be input as :
This is <b>bold</b> text.
You can use Html to customize the styling and color of the text like this. Although I haven't tried using external stylesheets using itextsharp, but the inline styling works fine.
Afternoon all;
So, I have been developing an application using C# and WPF. I am using a Rich Text Box to display a fair amount of static text, say roughly 1000 lines (lines being between 10 and 30 words)
I am inserting the text by saving it inside a text file. Then reading that text file and saving it to a string variable. That string variable is then put inside a FlowDoc, like so:
public void fillTimeline(RichTextBox timeline)
{
FlowDocument myFlowDoc = new FlowDocument();
string x = System.IO.File.ReadAllText(#"path");
myFlowDoc.Blocks.Add(new Paragraph(new Run(x)));
timeline.Document = myFlowDoc;
}
Once I built and ran the application, I see that the text is extremely fuzzy, and almost unreadable.
Like this
However this issue does not occur if I reduce the lines to about 500. Is there an issue displaying that much text within a RichTextBox?
I have tried a few things to resolve the issue
Setting FormattingMode to all available options.
Enabling ClearType
Trying all 3 TextRenderingModes
Is this a known issue? Is there anything I may be able to try. I am out of ideas now.
Thanks.
Try reading the text a line at a time
List<String> Lines = new List<String>();
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
myFlowDoc.Blocks.Add(new Paragraph(new Run(x)));
Lines.Add(line);
}
file.Close();
Try using a FlowDocumentScrollViewer rather than RichTextBox
Try just using Lines with a ListBox with TextBlock
With virtualization it performs nicely
I am working on a WPF Windows application using C# code. This is an application I inherited. I only have limited experience using WPF.
I have a Rich Text Box control which is used to build an Email using html. There is code to read the Rich Text Box contents and store the results as html in a database. This code is working. I need to write the reverse that is writing the html to the rich text box so it appears as text.
Below is the code I have to read the rich text box.
TextRange xaml = new TextRange(EmailBodyRichTextBox.Document.ContentStart, EmailBodyRichTextBox.Document.ContentEnd);
MemoryStream ms = new MemoryStream();
xaml.Save(ms, DataFormats.Xaml);
string xamlString = ASCIIEncoding.Default.GetString(ms.ToArray());
string html = HTMLConverter.HtmlFromXamlConverter.ConvertXamlToHtml("<FlowDocument>" + xamlString + "</FlowDocument>");
html = HttpUtility.HtmlEncode(html);
From the code you post above , it seems , you are saving the text in the rich text box to xaml, and then translate the xaml to HTML.
if you want to reverse, then translate your HTML to xaml and then load it to the rich text box.
To save the RichTextBox's content to a regular string that you can save in a database:
string formattedEmail = XamlWriter.Save(EmailBodyRichTextBox.Document);
To reload the RichTextBox from the string:
EmailBodyRichTextBox.Document = XamlReader.Parse(formattedEmail);
If you only want to store the emails in the database and then reload them to RichTextBoxes, there is no point in converting to HTML, it can only introduce conversion format mismatches.