When I'm appending the text in pdf using pdfbox is working fine but the problem is that text append inversely.
Example:
I'm adding some text in (x,y)=(0,0)
The text added in last of the page.
The text should be added in the first of the page.
PDDocument pDDocument = PDDocument.load(new java.io.File(filePath));
PDPage page = new PDPage();
page = pDDocument.getPages().get(0);
PDPageContentStream pDPageContentStream = new PDPageContentStream(pDDocument,page,PDPageContentStream.AppendMode.APPEND,true,true);
pDPageContentStream.setFont(org.apache.pdfbox.pdmodel.font.PDType1Font.TIMES_BOLD_ITALIC, 12);
pDPageContentStream.beginText();
pDPageContentStream.newLineAtOffset(0, 0);
pDPageContentStream.showText("Welcome!");
pDPageContentStream.endText();
pDPageContentStream.close();
pDDocument.save(newFilePath);
pDDocument.close();
When I'm set the position of the newLineAtOffset(0,0).
Expected result, text should be added in the first of the page.
Related
I created a richtextbox in wpf where i can select text and link it to a file or resource.
Let's say i want to add text something into textbox and add hyperlink to D:\cdrréper
I attach a part of the code:
_link.CommandParameter = path;
_link.NavigateUri = new Uri(path, UriKind.Absolute);
_link.IsEnabled = true;
using (MemoryStream ms = new MemoryStream()) {
TextRange tr = new TextRange(_link.ContentStart, _link.ContentEnd);
tr.Save(ms, DataFormats.Rtf);
richTextBox.Selection.Load(ms, DataFormats.Rtf);
}
The current text value in richtext box after load is
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Arial;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red0\green102\blue204;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ul\cf3\ltrch {\field{\*\fldinst { HYPERLINK "D:\\\\cdrr'e9per" }}{\fldrslt {something}}}}\li0\ri0\sa75\sb75\fi0\ql\par}
}
}
Hyperlink value from RTF is { HYPERLINK "D:\\cdrr'e9per" }.
When i'm trying to access the path is not working because cannot be founded. é character is not encoded correctly. Do you have any suggestions about how i can fix this? Thanks for any help.
In the Hyperlink section is needed to have a \ before 'e9. When we load the rtf string into a TextRange we need to Load it as Xaml.
tr.Load(ms, DataFormats.Xaml);
I would like to add rich text to a TextBox on a PowerPoint Slide.
I am using .Net 4.7.2 with Microsoft.Office.Interop.PowerPoint
Overview
// Initialization of the Application
public PresentationGenerator () {
pptApplication = new Application ();
}
// Creating a new document based on a teplate.
Presentation pptPresentation = generator.pptApplication.Presentations.Open (templatePath, MsoTriState.msoFalse, MsoTriState.msoTrue, showWindow);
// Setting a slide and editing the text works perfectly fine with this
Slide currentSlide;
currentSlide = pptPresentation.Slides[1];
currentSlide.Shapes.Title.TextFrame.TextRange.Text = "Wonderful Title";
currentSlide.Shapes[3].TextFrame.TextRange.Text = "Great TextBox";
This is an mwe of my setup to set text on slides.
I would however like to add text to one of my shapes using a loop and setting the layout depending on a property.
Imagine the following Array
customParagraphs = [
{
text:"example heading",
type:"title"
},{
text:"example normal text",
type:"text"
}]
I can loop over this list and add the text to the end of the TextRange2 using .insertAfter(text) and try setting the font-size for the text portion that i added.
TextRange2 textrange = currentSlide.Shapes[3].TextFrame2.TextRange
foreach(var paragraph in customParagraphs){
TextRange2 paragraphRange = textrange.Paragraphs.insertAfter(paragraph.text)
if(paragraph.type == "title"){
paragraphRange.Font.Size = 24.0F;
}
}
This will successfully add the text and change the font-size, if type is title. However it will change the font-size for the whole text-range!
The reference returned by .insertAfter() seems to refer to the instance of TextRange2 and not my newly added paragraph.
My Questions
Is there a way to change the font-size and other attributes of a line, paragraph or word inside a TextRange or TextRange2 element?
Is there a better way to add text to a TextRange or TextRange2 element than .insertAfter that preferably returns a reference to only the text i added?
Using cete Dynamic PDFtool I want to remove the footer template from the first page of the document. How to achieve this?
Page.Elements.Add(tblcontent);
Document.Pages.Add(Page);
The page class has an ApplyDocumentTemplate property that you can set to false, if you do not want a the template to be applied to a particular page.
Document document = new Document();
Template template = new Template();
// Add elements to template
document.Template = template;
Page page1 = new Page(PageSize.Letter);
// Add elements to page
page1.ApplyDocumentTemplate = false;
// Add additional pages leaving ApplyDocumentTemplate as true
// Save the PDF
document.Draw("output.pdf");
You can also accomplish this by using document sectioning. When a document is broken up into sections, each section can have its own template, or not have a template at all. In this following example, the first section doesn't have a template so the page numbers are not shown for the first two pages, and the second section does so page numbers are shown for the last 3 pages.
Document document = new Document();
// Create a template object and add a page numbering label
Template template = new Template();
template.Elements.Add(new PageNumberingLabel("%%SP%% of %%ST%%", 0, 680, 512, 12, Font.Helvetica, 12, TextAlign.Center));
// Begin the first section
document.Sections.Begin(NumberingStyle.RomanLowerCase);
// Add two pages
document.Pages.Add(new Page()); //Page 1
document.Pages.Add(new Page()); //Page 2
// Begin the second section
document.Sections.Begin(NumberingStyle.Numeric, template);
// Add three pages
document.Pages.Add(new Page()); //Page 3
document.Pages.Add(new Page()); //page 4
document.Pages.Add(new Page()); //page 5
// Save the PDF
document.Draw("output.pdf");
Here is a link to a topic on document sectioning:
http://docs.dynamicpdf.com/NET_Help_Library_19_08/Document%20Sectioning.html
I have a text with hyperlinks in it.
To not irritate the reader I want to have the normal text and the links on the same line. Right now every Inline element starts a new line.
It displays:
Please visit
http://google.com
to continue.
I want:
Please visit http://google.com to continue.
I've also noticed, that the hyperlink Hit area cover the hole inline element and not just the text.
My problem is identical than described and solved here:
Add clickable hyperlinks to a RichTextBox without new paragraph
The problem is, that it seems than something like a flowdocument for wp8 doesn't exist.
I need to create the inline elements programatically.
EDIT 1:
Here my code how I add the inline elements:
int index = 0;
rt = new RichTextBox() { };
while (true)
{
Paragraph para = new Paragraph();
if (item.text.Substring(index).IndexOf("<") == 0)
{
//TRUE when link
//I extract the URL and the linktext, and also update the index
Hyperlink hyper = new Hyperlink();
hyper.Click += new RoutedEventHandler((sender,e) => Hyperlink_Click(sender,e,URL));
hyper.Inlines.Add(linktext);
para.Inlines.Add(hyper);
}
else if (item.text.Substring(index).Contains("<"))
{
//TRUE when text, item.text contains a link
// I extract the text and update index
Run run = new Run() { Text = text };
para.Inlines.Add(run);
}
else
{
//TRUE when only text is left
Run run = new Run() { Text = item.text.Substring(index) };
para.Inlines.Add(run);
rt.Blocks.Add(para);
break;
}
// REMOVE: rt.Blocks.Add(para);
}
rt.SetValue(Grid.RowProperty, MainViewer.RowDefinitions.Count - 1);
MainViewer.Children.Add(rt);
EDIT 2
I still couldn't solve this Problem, does no one know a solution? I saw what I want in an App before, so it must be possible.
EDIT 3
I've created for every inline element a new paragraph. I've fixed my code above, it is working now
Paragraph p = new Paragraph();
p.Inlines.Add("Plase visit ");
var link = new Hyperlink();
link.Inlines.Add("google.com ");
p.Inlines.Add(link);
p.Inlines.Add("to continue");
rtb.Blocks.Add(p);
this works fine for me.
PS
If you want to show some html in you app, you can use HTMLTextBox or HTMLViewer from http://msptoolkit.codeplex.com/
I am trying to replace the temporary text in a word document with new text from a list. It works if the text is not in a shape, but once it tries to find the text in a textbox it throws an error. Here is what I have so far:
public void FindReplace(List<repvals> replaceVals, string docLocation, int listLen)
{
//Opens a new Word application
var app = new Microsoft.Office.Interop.Word.Application();
//Opens the .docx
var doc = app.Documents.Open(docLocation, true, false);
//Selects the document
var range = doc.Range();
for (int i = 0; i < listLen; i++)
{
//Finds the parameter, then replaces
range.Find.Execute(FindText: Convert.ToString(replaceVals[i].tempVal), Replace: WdReplace.wdReplaceAll, ReplaceWith: Convert.ToString(replaceVals[i].Boxes));
var shapes = doc.Shapes;
//Finds text within textboxes, then changes them
foreach (Microsoft.Office.Interop.Word.Shape shape in shapes)
{
var initialText = shape.TextFrame.TextRange.Text;
var resultingText = initialText.Replace(Convert.ToString(replaceVals[i].tempVal), Convert.ToString(replaceVals[i].Boxes));
shape.TextFrame.TextRange.Text = resultingText;
}
}
//prints document
doc.Save();
doc.Close();
//fully closes Word
Marshal.ReleaseComObject(app);
}
The problem occurs when it hits
var initialText = shape.TextFrame.TextRange.Text;
And throws an error saying: "This object does not support attached text."
The text in the shapes are nothing special. (e.g. tDATE, tNAME, etc.)
Any ideas?
I found the answer. Turns out my code was fine, however the document I was using (which I didn't write), had another shape on the second to last page to form a place to sign your name. I replaced that with an underscore, ran the code, and everything changed perfectly.
For those who also experience this problem, try checking how many shapes your foreach loop has counted:
http://i.imgur.com/1yNrL4p.png
Thank you Andrew and varocarbas for the help
*"In 2003 the AltText default for a standard textbox WAS the contained text BUT since you can change the Alt Text to NOT match it was never a good idea to read it this way. In 2010 the default for Alt Text is blank
If the textbox is named "Text Box 2" (substitute the correct name if not)
MsgBox ActiveDocument.Shapes("Text Box 2").TextFrame.TextRange should work."*
--
John SR Wilson
http://answers.microsoft.com/en-us/office/forum/office_2010-customize/shapesalternativetext-is-blank-for-the-docx/7671c746-2c2b-41d9-b7de-389a766587a7?page=2&msgId=31041d67-e62b-4ce0-b283-57fd6a4ff6b2