all I am new to VSTO and c#,
I'm trying to add a watermark to word document through VSTO automation.
I'm able to add watermark but after adding some text watermark is getting distorted (height is decreasing)
This is a distorted image
it is a bit blur, sorry for it.
Let me know if you need any information
Thanks in advance
below is my code
foreach (Word.Section section in doc.Sections)
{
Object oMissing = System.Reflection.Missing.Value;
Word.Shape shape =
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes
.AddTextEffect(MsoPresetTextEffect.msoTextEffect2, "confidential", "Calibri", 5,
MsoTriState.msoTrue, MsoTriState.msoFalse, 0, 0, ref oMissing);
//shape.Select(ref oMissing);
shape.Name = "WaterMark";
shape.Line.Visible = MsoTriState.msoFalse;
shape.TextEffect.NormalizedHeight = MsoTriState.msoFalse;
shape.TextEffect.FontItalic = MsoTriState.msoFalse;
shape.TextEffect.FontBold = MsoTriState.msoFalse;
shape.Fill.Visible = MsoTriState.msoTrue;
shape.Fill.Solid();
shape.Fill.ForeColor.RGB = 12632256;
shape.Fill.Transparency = (float)0.5;
shape.Rotation = 315;
shape.LockAspectRatio = MsoTriState.msoTrue;
shape.Left = (float)Word.WdShapePosition.wdShapeCenter;
shape.LeftRelative = (float)-999999;
shape.Top = (float)Word.WdShapePosition.wdShapeCenter;
shape.TopRelative = (float)-999999;
shape.Height = Globals.ThisAddIn.Application.CentimetersToPoints((float)3.65);
shape.Width = Globals.ThisAddIn.Application.CentimetersToPoints((float)12.62);
shape.WrapFormat.Type = Word.WdWrapType.wdWrapBehind;
shape.WrapFormat.AllowOverlap = -1;
shape.WrapFormat.DistanceBottom = 0;
shape.WrapFormat.DistanceLeft = 9;
shape.WrapFormat.DistanceRight = 9;
shape.WrapFormat.DistanceTop = 0;
shape.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
shape.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
shape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
shape.WidthRelative = (float)-999999;
doc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
}
Related
I Used spire.xls to create a sheet and a chart but my problem is when I save a chart as a PDF it would be so inappropriate just need to justify the chart but don't know how I think it would be the problem with scale
Chart chart = sheet.Charts.Add(ExcelChartType.ScatterSmoothedLineMarkers);
//Set region of chart data
chart.DataRange = sheet.Range["A2:A" + (_state.NumberOfReads + 2).ToString()].AddCombinedRange(sheet.Range["F2:F" + (_state.NumberOfReads + 2).ToString()]);
chart.SeriesDataFromRange = false;
//Set position of the chart
chart.LeftColumn = 22;
chart.TopRow = 6;
chart.RightColumn = 32;
chart.BottomRow = 29;
//Set and format category axis title
chart.PrimaryCategoryAxis.Title = "Applied Pressure DUT.";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
//Set and format value axis title
chart.PrimaryValueAxis.Title = "Average of REF.";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = -90;
chart.PrimaryValueAxis.MinValue = 0;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
chart.Legend.Delete();
//Loop through the data series of the chart
foreach (ChartSerie cs in chart.Series)
{
cs.Format.Options.IsVaryColor = true;
//Show data labels for data points
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
}
chart.PrimaryValueAxis.HasMajorGridLines = true;
IChartTrendLine trendLine = chart.Series[0].TrendLines.Add(TrendLineType.Linear);
chart.Series[0].TrendLines[0].DisplayEquation = true;
chart.Series[0].TrendLines[0].DisplayRSquared = true;
//chart.HasDataTable = true;
Image[] imgs = workbook.SaveChartAsImage(sheet);
Bitmap img = new Bitmap(imgs[0]);
img.Save(path + filePic, ImageFormat.Png);
and also look at the outputs
I try to print a pdf with IronPdf from html but the result leaves a border.
Is there a way to set Fit-To-Page in my PrintDocument?
Here my code:
public static void PrintDocument(string printer, bool landscape, PdfDocument pdfDocument, Duplexing duplex)
{
var printDocument = pdfDocument.GetPrintDocument();
printDocument.PrinterSettings.PrinterName = printer;
printDocument.DefaultPageSettings.Landscape = landscape;
printDocument.PrinterSettings.Duplex = DuplexMapping(duplex);
printDocument.PrinterSettings.DefaultPageSettings.PaperSize.RawKind = (int)PaperKind.A4;
printDocument.Print();
}
Thanks to IronPdf-Support I found a solution:
// enable javascript
var renderer = new IronPdf.HtmlToPdf();
renderer.PrintOptions.EnableJavaScript = true;
renderer.PrintOptions.RenderDelay = 500; //milliseconds
renderer.PrintOptions.CssMediaType = IronPdf.PdfPrintOptions.PdfCssMediaType.Screen;
renderer.PrintOptions.MarginTop = 0;
renderer.PrintOptions.MarginBottom = 0;
renderer.PrintOptions.MarginLeft = 0;
renderer.PrintOptions.MarginRight = 0;
Set Margin to 0.
i build document with Aspose.Word. I try add page number in right margin. Image better explain it:
My result pdf preview
How to do it?
My current code:
var document = new Document();
_builder = new DocumentBuilder(document)
{
PageSetup =
{
Orientation = Orientation.Portrait,
PaperSize = PaperSize.A4,
RightMargin = ConvertUtil.MillimeterToPoint(20),
BottomMargin = ConvertUtil.MillimeterToPoint(35),
LeftMargin = ConvertUtil.MillimeterToPoint(35),
TopMargin = ConvertUtil.MillimeterToPoint(35)
}
};
_builder.StartTable();
_builder.InsertCell();
_builder.Write("Test test test");
_builder.EndTable();
_builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
_builder.Write("Pages: ");
_builder.InsertField("PAGE", "");
_builder.Write("/");
_builder.InsertField("NUMPAGES");
document.Save(stream, SaveFormat.Pdf);
In your case, you need to add text box in the footer of document, insert page number field in it, and set its position. Please use following modified code example to get the desired output.
var document = new Document();
DocumentBuilder _builder = new DocumentBuilder(document)
{
PageSetup =
{
Orientation = Orientation.Portrait,
PaperSize = Aspose.Words.PaperSize.A4,
RightMargin = ConvertUtil.MillimeterToPoint(20),
BottomMargin = ConvertUtil.MillimeterToPoint(35),
LeftMargin = ConvertUtil.MillimeterToPoint(35),
TopMargin = ConvertUtil.MillimeterToPoint(35)
}
};
_builder.StartTable();
_builder.InsertCell();
_builder.Write("Test test test");
_builder.EndTable();
_builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
Shape shape = new Shape(document, ShapeType.TextBox);
shape.Stroked = false;
shape.Width = _builder.CurrentSection.PageSetup.PageWidth;
shape.Height = 50;
shape.Left = 0;
shape.Left = - _builder.CurrentSection.PageSetup.LeftMargin;
shape.Top = 0;
Paragraph paragraph = new Paragraph(document);
shape.AppendChild(paragraph);
_builder.InsertNode(shape);
_builder.MoveTo(paragraph);
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
_builder.Write("Pages: ");
_builder.InsertField("PAGE", "");
_builder.Write("/");
_builder.InsertField("NUMPAGES");
document.Save(MyDir + "output.pdf", SaveFormat.Pdf);
I work with Aspose as Developer evangelist.
Can someone explain me how I can create chart like this one using itextsharp library.
As you can see here, this chart has attached table with legends to its bottom side. And each bar has year attached to it. I want to create something like that.
Second picture. This is what i have so far. I don't have year attached to each bar, and my legends are not beneath each bar like in top graph. If someone could guide me how to create identical graph like one on top i would be grateful. Thanks in advance!
How can I turn those labels to be displayed horizontally, and put those legends beneath years in table like one in picture number 1.
// Chart Centers By Year
var chartCentersByYear = new Chart
{
Width = 1000,
Height = 450,
RenderType = RenderType.ImageTag,
AntiAliasing = AntiAliasingStyles.Graphics,
TextAntiAliasingQuality = TextAntiAliasingQuality.High
};
chartCentersByYear.Titles.Add("Centers By Year");
chartCentersByYear.Titles[0].Font = new Font("Arial", 16f);
chartCentersByYear.Titles[0].Alignment = System.Drawing.ContentAlignment.TopLeft;
chartCentersByYear.ChartAreas.Add("");
chartCentersByYear.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
chartCentersByYear.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
chartCentersByYear.Series.Add("Count");
chartCentersByYear.Series.Add("Cases");
chartCentersByYear.Series[0].ChartType = SeriesChartType.Column; //Pie
chartCentersByYear.Series[1].ChartType = SeriesChartType.StepLine; //StepLine
chartCentersByYear.Series[1].BorderDashStyle = ChartDashStyle.DashDot;
chartCentersByYear.Series[1].BorderWidth = 3;
chartCentersByYear.Series[0].Color = Color.Brown;
chartCentersByYear.Legends.Add("1");
chartCentersByYear.Legends.Add("2");
chartCentersByYear.Legends[0].HeaderSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[0].HeaderSeparatorColor = Color.Black;
chartCentersByYear.Legends[0].ItemColumnSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[0].ItemColumnSeparatorColor = Color.Black;
chartCentersByYear.Legends[1].HeaderSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[1].HeaderSeparatorColor = Color.Black;
chartCentersByYear.Legends[1].ItemColumnSeparator = LegendSeparatorStyle.Line;
chartCentersByYear.Legends[1].ItemColumnSeparatorColor = Color.Black;
//For the Legend
LegendCellColumn firstColumn = new LegendCellColumn();
firstColumn.ColumnType = LegendCellColumnType.SeriesSymbol;
firstColumn.HeaderBackColor = Color.WhiteSmoke;
chartCentersByYear.Legends[0].CellColumns.Add(firstColumn);
chartCentersByYear.Legends[1].CellColumns.Add(firstColumn);
LegendCellColumn secondColumn = new LegendCellColumn();
secondColumn.ColumnType = LegendCellColumnType.Text;
secondColumn.Text = "#LEGENDTEXT";
secondColumn.HeaderBackColor = Color.WhiteSmoke;
LegendItem newItemCount = new LegendItem();
newItemCount.Cells.Add(LegendCellType.Text, "Count", System.Drawing.ContentAlignment.MiddleCenter);
newItemCount.BorderWidth = 1;
newItemCount.BorderDashStyle = ChartDashStyle.Solid;
LegendItem newItemCases = new LegendItem();
newItemCases.Cells.Add(LegendCellType.Text, "Cases", System.Drawing.ContentAlignment.MiddleCenter);
newItemCases.BorderWidth = 1;
newItemCases.BorderDashStyle = ChartDashStyle.Solid;
// Getting data from a stored procedure
var totalCentersByYearResult = new Repository().GetTotalCentersByYear();
foreach (IGD_spInternationalReportCenterWithTots1_Result item in totalCentersByYearResult)
{
// For Series
chartCentersByYear.Series[0].Points.AddXY(item.YearEcmo, item.Count);
chartCentersByYear.Series[1].Points.AddY(item.Cases);
// For Legend
newItemCount.Cells.Add(LegendCellType.Text, item.Count.ToString(), System.Drawing.ContentAlignment.MiddleCenter);
newItemCases.Cells.Add(LegendCellType.Text, item.Cases.ToString(), System.Drawing.ContentAlignment.MiddleCenter);
}
chartCentersByYear.Legends[0].CustomItems.Add(newItemCount);
chartCentersByYear.Legends[0].CustomItems.Add(newItemCases);
chartCentersByYear.Legends[0].Docking = Docking.Bottom;
chartCentersByYear.Legends[1].Docking = Docking.Bottom; //Top
chartCentersByYear.Series[0].YAxisType = AxisType.Primary;
chartCentersByYear.Series[1].YAxisType = AxisType.Secondary;
//For two coordinate systems
chartCentersByYear.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
chartCentersByYear.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
chartCentersByYear.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
chartCentersByYear.ChartAreas[0].AxisY2.IsStartedFromZero = chartCentersByYear.ChartAreas[0].AxisY.IsStartedFromZero;
using (var chartimage = new MemoryStream())
{
chartCentersByYear.SaveImage(chartimage, ChartImageFormat.Png);
Byte[] newChart = chartimage.GetBuffer(); //return chartimage.GetBuffer();
var image = Image.GetInstance(newChart); //Image.GetInstance(Chart());
image.ScalePercent(50f);
image.SetAbsolutePosition(document.LeftMargin + 40, document.BottomMargin + 100);
document.Add(image);
}
I create a line chart, but I want to display the chart begin from 0 in X-axis.
How can I do this.
I try some method but still did not get what I want.
Chart1.ChartAreas[0].AxisX.Interval = 0;
Chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
Chart1.ChartAreas[0].AxisX.Minimum = 0;
Chart1.ChartAreas[0].AxisX.Crossing = 0;
This is what I do now
This is what I want
And one more, how can I set major and minor unit in the chart..?
my code here
protected void Page_Load(object sender, EventArgs e)
{
System.Drawing.Font axisFont = new System.Drawing.Font("Arial", 8, System.Drawing.FontStyle.Bold);
System.Drawing.Font titleFont = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Chart1.Width = 600;
Chart1.Height = 400;
Chart1.BorderlineColor = System.Drawing.Color.Black;
Chart1.BorderlineWidth = 1;
Chart1.BorderlineDashStyle = ChartDashStyle.Solid;
double[] min = { 60.9, 0, 28.81, 7.27 };
double[] ave = { 60.9, 0, 28.81, 7.27 };
double[] max = { 5167.72, 1.27, 4176.16, 2566.59 };
Chart1.Series["Series1"].ChartArea = "ChartArea1";
Chart1.Series["Series2"].ChartArea = "ChartArea1";
Chart1.Series["Series3"].ChartArea = "ChartArea1";
Chart1.Series["Series1"].Points.AddXY("Step 1-2", max[0]);
Chart1.Series["Series2"].Points.AddXY("Step 1-2", ave[0]);
Chart1.Series["Series3"].Points.AddXY("Step 1-2", min[0]);
Chart1.Series["Series1"].Points.AddXY("Step 2-3", max[1]);
Chart1.Series["Series2"].Points.AddXY("Step 2-3", ave[1]);
Chart1.Series["Series3"].Points.AddXY("Step 2-3", min[1]);
Chart1.Series["Series1"].Points.AddXY("Step 3-4", max[2]);
Chart1.Series["Series2"].Points.AddXY("Step 3-4", ave[2]);
Chart1.Series["Series3"].Points.AddXY("Step 3-4", min[2]);
Chart1.Series["Series1"].Points.AddXY("Step 4-5", max[3]);
Chart1.Series["Series2"].Points.AddXY("Step 4-5", ave[3]);
Chart1.Series["Series3"].Points.AddXY("Step 4-5", min[3]);
String hour1 = "hh";
Chart1.Titles.Add("Cycle Time : "+hour1);
Chart1.Titles[0].Font = titleFont;
Chart1.Series["Series1"].MarkerStyle = System.Web.UI.DataVisualization.Charting.MarkerStyle.Triangle;
Chart1.Series["Series2"].MarkerStyle = System.Web.UI.DataVisualization.Charting.MarkerStyle.Square;
Chart1.Series["Series3"].MarkerStyle = System.Web.UI.DataVisualization.Charting.MarkerStyle.Diamond;
Chart1.Series["Series1"].MarkerSize = 15;
Chart1.Series["Series2"].MarkerSize = 15;
Chart1.Series["Series3"].MarkerSize = 15;
Chart1.Legends.Add("Legend1");
Chart1.Series["Series1"].LegendText = "Max";
Chart1.Series["Series2"].LegendText = "Ave";
Chart1.Series["Series3"].LegendText = "Min";
Chart1.Series["Series1"].Legend = "Legend1";
Chart1.Series["Series2"].Legend = "Legend1";
Chart1.Series["Series3"].Legend = "Legend1";
Chart1.Series["Series1"].IsVisibleInLegend = true;
Chart1.Series["Series2"].IsVisibleInLegend = true;
Chart1.Series["Series3"].IsVisibleInLegend = true;
//This part I try to make the graph start from 0 in X-axis but not work
//Chart1.ChartAreas[0].AxisX.Interval = 0;
//Chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
//Chart1.ChartAreas[0].AxisX.Minimum = 0;
//Chart1.ChartAreas[0].AxisX.Crossing = 0;
//Chart1.ChartAreas[0].AxisX.Minimum = 0;
//Chart1.ChartAreas[0].Position.Auto = false;
Chart1.ChartAreas[0].AxisX.TitleFont = axisFont;
Chart1.ChartAreas[0].AxisY.TitleFont = axisFont;
Chart1.ChartAreas[0].AxisX.Title = "Step";
Chart1.ChartAreas[0].AxisY.Title = "Time (" + hour1 + ")";
Chart1.ChartAreas[0].BackColor = System.Drawing.Color.AliceBlue;
Chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
Chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.ColorTranslator.FromHtml("#D5E8F5");
}
I had a similar issue, and found that the solution was to set the IsMarginVisible flag to false:
chart1.ChartAreas[0].AxisX.IsMarginVisible = false;
Hope this helps.
The Chart control decides by his own where to start the Axes, and more importantly where to end them, because it could create problems in displaying the points.
Say that you have a point in (-1,0), if you decided to start the X-Axis from 0 what should the chart control do? Display the series starting from unknown? Erase the point?
In your chart every point has 2 values, a string value for the X-Axis and a double for the Y-Axis.
The strings are stored in the chart in alphabetical order, and it also checks whether some are equals or not (so you don't have 3 *Step 1-3*s).
It also give the strings a position value, starting obviously from 0.
But what is the 0 position value string?
Answer: there is no 0 position value string possible.
In fact, if you try something like
Chart1.Series["Series1"].Points.AddXY(string.Empty, 1.0);
Chart1.Series["Series2"].Points.AddXY(string.Empty, 2.0);
Chart1.Series["Series3"].Points.AddXY(string.Empty, 3.0);
The chart control will automatically add a label for the empty string called 1, showing the position value.
The only way for setting major and minor unit in the chart is by adding or removing data from the chart.
Another workaround could be this:
Chart1.Series["SeriesMin"].Points.AddXY(0, max[0]);
Chart1.Series["SeriesAve"].Points.AddXY(0, ave[0]);
Chart1.Series["SeriesMax"].Points.AddXY(0, min[0]);
Chart1.Series["SeriesMin"].Points[0].AxisLabel = "Step 1-2";
Chart1.Series["SeriesAve"].Points[0].AxisLabel = "Step 1-2";
Chart1.Series["SeriesMax"].Points[0].AxisLabel = "Step 1-2";
Then you could add all your so far not working code
Chart1.ChartAreas[0].AxisX.Interval = 0;
Chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
Chart1.ChartAreas[0].AxisX.Minimum = 0;
Chart1.ChartAreas[0].AxisX.Crossing = 0;
Chart1.ChartAreas[0].AxisX.Minimum = 0;
//Chart1.ChartAreas[0].Position.Auto = false; //EXCEPT THIS ONE!!
Worked like a charm, exceedingly boring if you had to add more data not from a db.