How to add text on desired coordinates on new word file using openxml - coordinates

I want to create .docx file using openxml and add text on desired coordinates(location) on each page of the file. Is there any way in openxml to adjust the text. I am using the following snippet:
WordprocessingDocument doc = WordprocessingDocument.Create("E:\\test11.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
{
MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
ParagraphProperties oParagraphProperties = para.AppendChild(new ParagraphProperties());
Run run = para.AppendChild(new Run());
Text tt = new Text(str);
run.AppendChild(tt);
RunProperties runProp = new RunProperties(); // Create run properties.
RunFonts runFont = new RunFonts() { Ascii = "Cambria(Headings)", HighAnsi = "Cambria(Headings)" };
Bold bold = new Bold();
DocumentFormat.OpenXml.Wordprocessing.Color Color1 = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "0EBFE9" };
Italic ita = new Italic();
runProp.Append(bold);
runProp.Append(Color1);
runProp.Append(ita);
FontSize size = new FontSize();
size.Val = new StringValue((fontSize * 2).ToString()); // 48 half-point font size
runProp.Append(runFont);
runProp.Append(size);
run.PrependChild<RunProperties>(runProp);
}
Using this I was able to add text on .docx file, but not on desired coordinate location. Can someone help with this?
Thanks.

I found a way to add text to a coordinate on the page of a Word file. I started with your generated Word file and using Word, I added a simple TextBox (Insert->Text->TextBox). I generated the code for the added TextBox using the Productivity Tool. (Note: as of this writing, the latest version of the SDK is now 2.5, which is recommended for this to work).
Add the following method to your class above:
private static void PlaceTextAtCoordinate(Paragraph para, string text, double xCoordinate, double uCoordinate)
{
var picRun = para.AppendChild(new Run());
Picture picture1 = picRun.AppendChild(new Picture());
Shapetype shapetype1 = new Shapetype() { Id = "_x0000_t202", CoordinateSize = "21600,21600", OptionalNumber = 202, EdgePath = "m,l,21600r21600,l21600,xe" };
Stroke stroke1 = new Stroke() { JoinStyle = StrokeJoinStyleValues.Miter };
Path path1 = new Path() { AllowGradientShape = true, ConnectionPointType = ConnectValues.Rectangle };
shapetype1.Append(stroke1);
shapetype1.Append(path1);
Shape shape1 = new Shape() { Id = "Text Box 2", Style = string.Format("position:absolute;margin-left:{0:F1}pt;margin-top:{1:F1}pt;width:187.1pt;height:29.7pt;z-index:251657216;visibility:visible;mso-wrap-style:square;mso-width-percent:400;mso-height-percent:200;mso-wrap-distance-left:9pt;mso-wrap-distance-top:3.6pt;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:3.6pt;mso-position-horizontal-relative:text;mso-position-vertical-relative:text;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:top", xCoordinate, uCoordinate), Stroked = false };
TextBox textBox1 = new TextBox() { Style = "mso-fit-shape-to-text:t" };
TextBoxContent textBoxContent1 = new TextBoxContent();
Paragraph paragraph2 = new Paragraph();
Run run2 = new Run();
Text text2 = new Text();
text2.Text = text;
run2.Append(text2);
paragraph2.Append(run2);
textBoxContent1.Append(paragraph2);
textBox1.Append(textBoxContent1);
TextWrap textWrap1 = new TextWrap() { Type = WrapValues.Square };
shape1.Append(textBox1);
shape1.Append(textWrap1);
picture1.Append(shapetype1);
picture1.Append(shape1);
}
The following usings were found in my class - your list may be different - but I wanted to detail them here just in case.
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Vml;
using DocumentFormat.OpenXml.Vml.Office;
using DocumentFormat.OpenXml.Vml.Wordprocessing;
using DocumentFormat.OpenXml.Wordprocessing;
Finally, add the following 2 calls to the very end of your method above:
PlaceTextAtCoordinate(para, "Text at 90.1,90.1", 90.1, 90.1);
PlaceTextAtCoordinate(para, "Text at 120.5,120.5", 120.1, 120.1);
and your Word Doc will look like the following:

Related

saving modified data in pdf itextsharp

I have created a pdf using itextsharp which contains some editable fields, when the service is called the pdf is created. But the problem what I am facing is, if i am changing anything in the pdf and downloading it then the changes are not saved. Also i want to open the pdf in a new tab through server side.
Code what I am using is:
public static String[] LANGUAGES_gc = { "English", "Math", "Science" };
[HttpGet]
[ODataRoute("GetPdf")]
public void DownloadPDF()
{
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "inline;filename=Example.pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
Document doc = new Document(iTextSharp.text.PageSize.A4, 10f, 10f, 100f, 0f);
string pdfFilePath = HttpContext.Current.Server.MapPath(".") + "/PDFFiles";
PdfWriter wri = PdfWriter.GetInstance(doc, HttpContext.Current.Response.OutputStream);
doc.Open();
doc.AddAuthor("Test author");
doc.AddCreationDate();
PdfContentByte cb = wri.DirectContent;
Font _bf = new Font(Font.FontFamily.HELVETICA, 6);
PdfFormField _radioGroup = PdfFormField.CreateRadioButton(wri, true);
_radioGroup.FieldName = "language_gc";
Rectangle _rect;
RadioCheckField _radioG;
PdfFormField _radioField1;
PdfFormField field;
for (int i = 0; i < LANGUAGES_gc.Length; i++)
{
_rect = new Rectangle(46, 806 - i * 40, 60, 788 - i * 40);
_radioG = new RadioCheckField(wri, _rect, null, LANGUAGES_gc[i]);
_radioG.BackgroundColor = new GrayColor(0.8f);
_radioG.BorderColor = GrayColor.BLACK;
_radioG.CheckType = RadioCheckField.TYPE_CIRCLE;
_radioField1 = _radioG.RadioField;
_radioGroup.AddKid(_radioField1);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(LANGUAGES_gc[i], new Font(Font.FontFamily.HELVETICA, 18)), 70, 790 - i * 40, 0);
}
/* Button */
_rect = new Rectangle(300, 806, 370, 788);
PushbuttonField button = new PushbuttonField(wri, _rect, "Buttons");
button.BackgroundColor = new GrayColor(0.75f);
button.BorderColor = GrayColor.GRAYBLACK;
button.BorderWidth = 1;
button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED;
button.TextColor = GrayColor.GRAYBLACK ;
button.FontSize = 12;
button.Text = "Submit";
//button.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT;
button.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
button.ProportionalIcon = true;
button.IconHorizontalAdjustment = 0;
field = button.Field;
field.Action = PdfAction.JavaScript("this.showButtonState()", wri);
wri.AddAnnotation(field);
//}
//return ms.ToArray();
/*----------------------------------------------------*/
wri.AddAnnotation(_radioGroup);
wri.AddAnnotation(button.Field);
cb = wri.DirectContent;
doc.Close();
HttpContext.Current.Response.Write(doc);
HttpContext.Current.Response.End();
}
Can someone suggest me the solution??
Let's make an analogy here, with a simpler file-type. Suppose you're showing the user a textfile. They open it in their native .txt app (notepad) and make some changes.
Do you expect their changes to be communicated automatically back to your server?
Or that their changes are magically propagated? Of course not.
And even if it would, this seems like functionality notepad would have to provide, rather than something the creator of the file should do.
Now, as it so happens, there is a particular standard of pdf documents that exists for precisely your use-case. Essentially, the document sets up a connection to a server, and synchronizes. However, this standard is rather obscure, not a lot of viewers support it.
To the best of my knowledge, no pdf library (including iText) supports making documents like that.

syncfusion chart background line will be dotted line

I want my chart backgroud line will be dotted. Which property can i use for dotted in syncfusion? I tried but i coudn't do this. I don't know exactly which propertly will use for dotted line.
Here is my code:
control.AutoTempFileCleanUp = true;
control.OutputFormat = ImageProviderOutputFormat.DiskFile;
control.Model.Series.Clear();
ChartModel chartModel = new ChartModel();
ChartSeries chart = new ChartSeries(yAxisBar1LegendName, ChartSeriesType.Column);
chart.Text = yAxisBar1LegendName;
control.ChartArea.PrimaryXAxis.TickLabelsDrawingMode = ChartAxisTickLabelDrawingMode.UserMode;
// = string.Format("");
control.ChartArea.PrimaryXAxis.Labels.Add(new ChartAxisLabel("", Color.Black, new Font("Arial", 10), 0, "", ChartValueType.Custom));
int counter = 1;
DoubleRange dr = new DoubleRange(1, 100);
foreach (DataRow row in ds.Tables[0].Rows)
{
double bar1Value = Convert.ToDouble(row[yAxisValueColumn1]);
chart.Points.Add(counter, bar1Value);
control.ChartArea.PrimaryXAxis.Labels.Add(new ChartAxisLabel(row["ModuleCode"].ToString(), Color.Black, new Font("Arial", 10), counter, "", ChartValueType.Custom));
counter++;
}
chart.PrepareStyle += new ChartPrepareStyleInfoHandler(series_PrepareStyle);
control.ChartArea.PrimaryXAxis.DrawGrid = false;
control.PrimaryXAxis.GridLineType.ForeColor = Color.DarkGray;
control.PrimaryYAxis.GridLineType.ForeColor = Color.DarkGray;
control.PrimaryXAxis.LineType.ForeColor = Color.DarkGray;
control.PrimaryYAxis.LineType.ForeColor = Color.DarkGray;
control.Text = chartHeader;
control.ChartArea.PrimaryYAxis.Title = yAxisText;
control.ChartArea.PrimaryXAxis.Title = xAxisText;
control.ChartArea.PrimaryXAxis.TitleAlignment = StringAlignment.Center;
control.ChartArea.PrimaryXAxis.IsVisible = true;
control.ChartArea.PrimaryXAxis.LabelAlignment = StringAlignment.Center;
control.ChartArea.PrimaryXAxis.VisibleRange.Min = 0;
control.ChartArea.PrimaryXAxis.VisibleRange.Max = counter;
control.ChartArea.PrimaryXAxis.VisibleRange.Interval = 1;
control.ChartArea.PrimaryYAxis.EdgeLabelsDrawingMode = ChartAxisEdgeLabelsDrawingMode.Center;
control.ChartArea.PrimaryYAxis.GridDrawMode = ChartAxisGridDrawingMode.Default;
//control.PrimaryXAxis.EdgeLabelsDrawingMode = ChartAxisEdgeLabelsDrawingMode.Center;
control.PrimaryXAxis.Font = new Font("Arial", 10F);
control.PrimaryYAxis.Font = new Font("Arial", 10F);
counter = 0;
foreach (ChartSeries series in control.Series)
{
Color color;
if (counter == 0)
{
color = Color.Green;
else
{
color = Color.Red;
}
series.Style.Interior = new BrushInfo(color);
series.Style.Border.Color = Color.DarkGray;
series.Style.Font.Bold = true;
series.Style.TextColor = Color.Black;
series.Style.TextOrientation = ChartTextOrientation.Left;
series.Style.TextFormat = "{0}";
counter++;
}
control.Width = 650;
control.Series3D = false;
control.ShowLegend = false;
control.BorderStyle = BorderStyle.None;
control.BorderAppearance.SkinStyle = ChartBorderSkinStyle.None;
//control.Legend.Alignment = ChartAlignment.Far;
}
Here is my chart image:
Thanks for using syncfusion products.
We have analyzed your query. If you want to customize the grid lines in axis, then you can use the “DashStyle” property in GridLineType which property is used to change the line style. And you can also specify the grid line style as Dash, DashDot, DashDotDot, Dot, Solid in axis.
And please find the below code snippet
this.ChartWebControl1.PrimaryYAxis.GridLineType.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
And we have also prepared a sample for your reference in ASP.NET classic platform and attached in the below location.
Sample Link : http://www.syncfusion.com/downloads/support/directtrac/160606/ze/Sample1273790903
Please find the output of the sample below:
And also we wish to let you know that the above mentioned property is also applicable for chart control in windows forms, ASP.NET classic and ASP.NET MVC classic platforms.
Please contact us via syncfusion support, if you have any queries related to using syncfusion products.

How do I change the weight of a simulated bold font using itext

I am using the iText library to generate text. I am loading the Arial Unicode MS font which does not contain a bold style so iText is simulating the bold. This works fine, but the weight of the bold font appears too heavy compared with text generated using the Java API or even using Microsoft Word.
I tried to get the weight from the FontDescriptor, but the value returned is always 0.0
float weight = font.getBaseFont().getFontDescriptor(BaseFont.FONT_WEIGHT, fontSize);
Is there a way I can change the weight of a simulated bold font?
As an addendum to #Chris' answer: You do not need to construct those Object[]s as there is a Chunk convenience method:
BaseFont arialUnicodeMs = BaseFont.createFont("c:\\Windows\\Fonts\\ARIALUNI.TTF", BaseFont.WINANSI, BaseFont.EMBEDDED);
Font arial12 = new Font(arialUnicodeMs, 12);
Paragraph p = new Paragraph();
for (int i = 1; i < 100; i++)
{
Chunk chunk = new Chunk(String.valueOf(i) + " ", arial12);
chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, i/100f, null);
p.add(chunk);
}
document.add(p);
results in
EDIT
Sorry, I just realized after posting this that you're using iText but my answer is for iTextSharp. You should, however, be able to use most of the code below. I've updated the source code link to reference the appropriate Java source.
Bold simulation (faux bold) is done by drawing the text with a stroke. When iText is asked to draw bold text with a non-bold font it defaults to applying a stroke with a width of of the font's size divided by 30. You can see this in the current source code here. The magic part is setting the chunk's text rendering mode to a stroke of your choice:
//.Net code
myChunk.Attributes[Chunk.TEXTRENDERMODE] = new Object[] { PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, MAGIC_NUMBER_HERE, null };
//Java code
myChunk.attributes.put(Chunk.TEXTRENDERMODE, new Object[]{Integer.valueOf(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE), MAGIC_NUMBER_HERE, null});
Knowing that you can just apply the same logic but using your weight preference. The sample below creates four chunks, the first normal, the second faux-bold, the third ultra-heavy faux-bold and the fourth ultra-lite faux-bold.
//.Net code below but should be fairly easy to convert to Java
//Path to our PDF
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
//Path to our font
var ff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
//Normal document setup, nothing special here
using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
//Register our font
FontFactory.Register(ff, "Arial Unicode MS");
//Declare a size to use throughout the demo
var size = 20;
//Get a normal and a faux-bold version of the font
var f = FontFactory.GetFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, size, iTextSharp.text.Font.NORMAL);
var fb = FontFactory.GetFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, size, iTextSharp.text.Font.BOLD);
//Create a normal chunk
var cNormal = new Chunk("Hello ", f);
//Create a faux-bold chunk
var cFauxBold = new Chunk("Hello ", fb);
//Create an ultra heavy faux-bold
var cHeavy = new Chunk("Hello ", f);
cHeavy.Attributes = new Dictionary<string, object>();
cHeavy.Attributes[Chunk.TEXTRENDERMODE] = new Object[] { PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, size / 10f, null };
//Create a lite faux-bold
var cLite = new Chunk("Hello ", f);
cLite.Attributes = new Dictionary<string, object>();
cLite.Attributes[Chunk.TEXTRENDERMODE] = new Object[] { PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, size / 50f, null };
//Add to document
var p = new Paragraph();
p.Add(cNormal);
p.Add(cFauxBold);
p.Add(cHeavy);
p.Add(cLite);
doc.Add(p);
doc.Close();
}
}
}

iTextSharp does not render header/footer when using element generated by XmlWorkerHelper

I am trying to add header/footer to a PDF whose content is otherwise generated by XMLWorkerHelper. Not sure if it's a placement issue but I can't see the header/footer.
This is in an ASP.NET MVC app using iTextSharp and XmlWorker packages ver 5.4.4 from Nuget.
Code to generate PDF is as follows:
private byte[] ParseXmlToPdf(string html)
{
XhtmlToListHelper xhtmlHelper = new XhtmlToListHelper();
Document document = new Document(PageSize.A4, 30, 30, 90, 90);
MemoryStream msOutput = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
writer.PageEvent = new TextSharpPageEventHelper();
document.Open();
var htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(HttpContext.Server.MapPath("~/Content/themes/mytheme.css"), true);
var pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
var worker = new XMLWorker(pipeline, true);
var parser = new XMLParser();
parser.AddListener(worker);
using (TextReader sr = new StringReader(html))
{
parser.Parse(sr);
}
//string text = "Some Random Text";
//for (int k = 0; k < 8; ++k)
//{
// text += " " + text;
// Paragraph p = new Paragraph(text);
// p.SpacingBefore = 8f;
// document.Add(p);
//}
worker.Close();
document.Close();
return msOutput.ToArray();
}
Now instead of using these three lines
using (TextReader sr = new StringReader(html))
{
parser.Parse(sr);
}
if I comment them out and uncomment the code to add a random Paragraph of text (commented in above sample), I see the header/footer along with the random text.
What am I doing wrong?
The EventHandler is as follows:
public class TextSharpPageEventHelper : PdfPageEventHelper
{
public Image ImageHeader { get; set; }
public override void OnEndPage(PdfWriter writer, Document document)
{
float cellHeight = document.TopMargin;
Rectangle page = document.PageSize;
PdfPTable head = new PdfPTable(2);
head.TotalWidth = page.Width;
PdfPCell c = new PdfPCell(ImageHeader, true);
c.HorizontalAlignment = Element.ALIGN_RIGHT;
c.FixedHeight = cellHeight;
c.Border = PdfPCell.NO_BORDER;
head.AddCell(c);
c = new PdfPCell(new Phrase(
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " GMT",
new Font(Font.FontFamily.COURIER, 8)
));
c.Border = PdfPCell.TOP_BORDER | PdfPCell.RIGHT_BORDER | PdfPCell.BOTTOM_BORDER | PdfPCell.LEFT_BORDER;
c.VerticalAlignment = Element.ALIGN_BOTTOM;
c.FixedHeight = cellHeight;
head.AddCell(c);
head.WriteSelectedRows(
0, -1, // first/last row; -1 flags all write all rows
0, // left offset
// ** bottom** yPos of the table
page.Height - cellHeight + head.TotalHeight,
writer.DirectContent
);
}
}
Feeling angry and stupid for having lost more than two hours of my life to Windows 8.1! Apparently the built in PDF Reader app isn't competent enough to show 'Headers'/'Footers'. Installed Foxit Reader and opened the output and it shows the Headers allright!
I guess I should try with Adobe Reader too!!!
UPDATES:
I tried opening the generated PDF in Adobe Acrobat reader and finally saw some errors. This made me look at the HTML that I was sending to the XMLWorkerHelper more closely. The structure of the HTML had <div>s inside <table>s and that was tripping up the generated PDF. I cleaned up the HTML to ensure <table> inside <table> and thereafter the PDF came out clean in all readers.
Long story short, the code above is fine, but if you are testing for PDF's correctness, have Adobe Acrobat Reader handy at a minimum. Other readers behave unpredictably as in either not reporting errors or incorrectly rendering the PDF.

WordProcessingML. How do I assign background color to text?

I have code that creates document with several paragraphs with different text-color for some words. Something like:
using (var doc = WordprocessingDocument.Create("some-file-name", WordprocessingDocumentType.Document))
{
// Add a new main document part.
var mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
var body = new Body();
var paragraph = new Paragraph();
var run = new Run();
...
// append bold text
run.AppendChild(new RunProperties {Bold = new Bold(), });
run.AppendChild(new Text("some-text"));
...
// append red text
run.AppendChild(new RunProperties
{ Color = new Color {Val = "FF0000"}});
run.AppendChild(new Text("some-text"));
But I haven't found a way how to add text with colored background. How can I do that?
Let me answer myself:
Background is Highlight property:
// yellow background sample
run.AppendChild(new RunProperties { Highlight = new Highlight { Val = HighlightColorValues.Yellow } });
run.AppendChild(new Text("some-text"));
I found that I needed to set the w:shd property in the run properties. I was using docx4j, but the principal is the same.