Print Options(Excel Iterop) Issue with horizontal page breaks? - c#

I am having problems with setting horizontal page breaks. No matter what row I set the first page break to it ALWAYS is set the row 87
This is the code I am using. (I checked to see if I was accidentally setting the page break again below this code, but I was not) So this is all of the code I am using to setup the print settings.
int pageCount = 2;
string defprinter = null;
defprinter = xlApplication.ActivePrinter;
xlWorkSheet.ResetAllPageBreaks();
xlApplication.ActivePrinter = defprinter;
var with = xlWorkSheet.PageSetup;
with.PaperSize = Excel.XlPaperSize.xlPaperA4;
with.Orientation = Excel.XlPageOrientation.xlPortrait;
// Fit Sheet on One Page
with.Zoom = false;
with.FitToPagesWide = 1;
with.PrintArea = #"$A$1:$I$" + (pageCount * 71);
with.FitToPagesTall = pageCount;
xlWorkSheet.HPageBreaks.Add(xlWorkSheet.Range["A72"]);
xlWorkSheet.HPageBreaks.Add(xlWorkSheet.Range["A143"]);
// Normal Margins
with.LeftMargin = xlApplication.InchesToPoints(0.5);
with.RightMargin = xlApplication.InchesToPoints(0.5);
with.TopMargin = xlApplication.InchesToPoints(0.5);
with.BottomMargin = xlApplication.InchesToPoints(0.5);
I have spent the better part of two days trying all different settings with no luck. No matter what I do, the first PageBreak is always set the row 87 and not row 72.
Any suggestions would be greatly appreciated.
EDIT: Tried this code on a different PC today and it works as it should. So now the issue is identified as being something to do with what is going on in Excel on that particular machine. OS is Windows 10 BTW.
So even though it works, this is still an issue as I can't have it sporadically working on some users PC's and not on others.

Related

How can I add an image to my sheet without being prompted to replace contents of the (empty) destination cells (EPPlus)?

When I open the Excel file I generate using EPPlus (C#), I get "Do you want to replace the contents of the destination cells in [... .xlsx]PivotTable?"
As you can see in the screen shot below, there is nothing in the cell where I want the image to display. Even when I push the image beyond the outer edge of the PivotTable, I get this message (I had been placing beneath column C/3). When I select yes to the dialog quoted above, the image is finally placed on the sheet (if I select "no" it is not added), and the sheet looks like this:
This is the code I'm using to add the image:
int imageCol = _grandTotalsColumnPivotTable + 1; // -1; <= I really want it to be this
AddImage(pivotTableWorksheet, 1, imageCol);
. . .
private void AddImage(ExcelWorksheet oSheet, int rowIndex, int colIndex)
{
var excelImage = oSheet.Drawings.AddPicture("PRO*ACT Logo", _logo);
excelImage.From.Column = colIndex - 1;
excelImage.From.Row = rowIndex - 1;
excelImage.SetSize(130, 100);
excelImage.From.ColumnOff = Pixel2MTU(2);
excelImage.From.RowOff = Pixel2MTU(2);
}
public int Pixel2MTU(int pixels)
{
int mtus = pixels * 9525;
return mtus;
}
How can I either get the message to not display (programmatically, this can't be something the user needs to do) and "just do it" or let it know that there is nothing in that location, anyway?
UPDATE
This is apparently not the problem; I tried to delete this post, but can't, since it has an answer. Even when I comment out the writing of the image, I get it. And trying to prevent messages like so:
_xlApp.DisplayAlerts = false;
...does not work, either - I still get it.
Apparently the problem is elsewhere, but I don't know where.
UPDATE 2
It was a case of hidden data underneath the PivotTable; once I got rid of that, the problem went away. IOW, the problem was with my code, not with EPPlus.
I use this code to add an image to Excel. It does not asks me to replace content. But I do not use PivotTables. Maybe that would make a difference.
using (System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("test.png")))
{
var excelImage = ws.Drawings.AddPicture("My Logo", image);
excelImage.SetPosition(1, 0, 5, 0);
}

Cannot edit DataGridView cell, after choosing option I have to press Esc and lose the edit

So here is the story - I have 2 DataGridViews - one of them is used as a display, the other one is user for editing or adding new entries. I don't use bindings on the edit one. When I am creating new entry I am just extracting the cells' values and passing them to an object. When I am editing though comes the problem.
The edit consists of 2 parts - one is to select the entry to be edited and display it on the second DGV. This is done with SelectionChanged event and the code is bellow. Then when editing is done it should just publish the new task the same way as creation. The thing is that when I am doing the edit and I come to the moment when I have to select from one of my cells - a ComboBoxCell - and I make a selection everything freezes. I cannot click anything else than this ComboBox until I press Esc - which obviously reverts the choice.
Why is this freeze happening and what causes it only when the data is cloned from the first DGV, but is okay when creating a brand new row? I am using VS2012 by the way and this is a windows forms application
Here is the code for duplication of the selected task - this method is the only thing in the DGV1's SelectionChanged Event handler:
public void createTemplateTaskToBeEditted(Form1 form1, ApplicationControl appControl)
{
form1.dg_templateView.Rows.Clear();
for (int i = 0; i < form1.dg_taskView.SelectedRows.Count; i++)
{
if (form1.dg_taskView.SelectedRows[i].Cells[0].Value == null)
{
form1.dg_taskView.SelectedRows[i].Cells[0].Value = false;
}
int index = form1.dg_templateView.Rows.Add();
form1.dg_templateView.Rows[index].Cells[0].Value =
form1.dg_taskView.SelectedRows[i].Cells[0].Value.ToString();
form1.dg_templateView.Rows[index].Cells[1].Value =
form1.dg_taskView.SelectedRows[i].Cells[2].Value.ToString();
form1.dg_templateView.Rows[index].Cells[2].Value =
form1.dg_taskView.SelectedRows[i].Cells[3].Value.ToString();
form1.dg_templateView.Rows[index].Cells[3].ValueType = typeof(ComboBox);
form1.dg_templateView.Rows[index].Cells[3].Value =
form1.PopulateAssignToComboBox(appControl.GetAllowedMembers(form1));
form1.dg_templateView.Rows[index].Cells[4].Value =
form1.dg_taskView.SelectedRows[i].Cells[5].Value.ToString();
form1.dg_templateView.Rows[index].Cells[5].Value =
form1.dg_taskView.SelectedRows[i].Cells[6].Value.ToString();
form1.dg_templateView.Rows[index].Cells[6].Value =
form1.dg_taskView.SelectedRows[i].Cells[7].Value.ToString();
form1.dg_templateView.Rows[index].Cells[7].Value =
form1.dg_taskView.SelectedRows[i].Cells[8].Value.ToString();
form1.dg_templateView.Rows[index].Cells[8].Value =
form1.dg_taskView.SelectedRows[i].Cells[9].Value.ToString();
form1.dg_templateView.Rows[index].Cells[9].Value =
form1.dg_taskView.SelectedRows[i].Cells[10].Value.ToString();
form1.dg_templateView.Rows[index].Cells[10].Value =
form1.dg_taskView.SelectedRows[i].Cells[11].Value.ToString();
form1.dg_templateView.Rows[index].Cells[11].Value =
form1.dg_taskView.SelectedRows[i].Cells[12].Value.ToString();
}
}

Creating image in asp.net aspx.cs file

I am trying to create an image in asp.net then i want to change the image url in the code behind fie to display different images with different conditions. when i run the program i get an empty box with the description of the image. What should i do? thank you.
here is my code in the code behind file:
if (major2.Text == "ECE")
{
Image1.ImageUrl ="~/App_Data/Electrical_component.jpg";
Image1.AlternateText = "ECE";
Image1.Width = 250 ;
Image1.Height = 250;
}
if (major2.Text == "ENG")
{
Image1.Width = 250;
Image1.Height = 250;
Image1.ImageUrl = "~/App_Data/eng.jpg";
Image1.AlternateText = "english";
}
if (major2.Text == "PHY")
{
Image1.Width = 250;
Image1.Height = 250;
Image1.AlternateText = "physics";
Image1.ImageUrl = "~/App_Data/PhysicsBanner.jpg";
}
if (major2.Text == "MEC")
{
Image1.Width = 250;
Image1.Height = 250;
Image1.AlternateText = "mechanical";
Image1.ImageUrl = "~/App_Data/mechanical-engineers_00062336.jpg";
}
Open up your webpage in Google Chrome, and open the F12 Developer Options (I think this is the third time today I'm offering this advice to someone !!)
Go into the Network tab, then try to trigger one of your images appearing.
Does the Network tab show your webpage is trying to load the appropriate image file ?
If so, does it produce a "404" not found error, or does it successfully load that image ?
It's also worth checking that your image files have been set to "Copy to output" in Visual Studio, to make sure they are included in your site. But if you hit a "404" error above, that'll point this problem out to you.
Finally, does your C# code (shown in the question) actually get run? If you put a breakpoint on these lines, does it get hit ? And does a Postback occur after setting the Image attributes, which might also explain the images disappearing again.
Good luck!

Groupbox adjust width based on text length

I have a c# app (winform) that generates Groupboxes from an xml file and based on what's in the file, populates it with Radio Buttons or CheckBoxes. Each of these groupboxes have a name, some of them are longer and they get cut off half way through.
This is how they are generated.
int nc = groupNodes.Count;
for (int i = 0; i < nc; i++)
{
node = groupNodes[i];
GroupBox box = new GroupBox();
box.AutoSize = true;
box.AutoSizeMode = AutoSizeMode.GrowAndShrink;
box.Text = node.Attributes["name"].Value;
//......
}
I tried using the following,
Size textSize = TextRenderer.MeasureText(box.Text,box.Font);
box.Width = (int)textSize.Width;
and tried the following
box.width = (int)box.text.length;
but none of this made any difference.
I also came across This thread. But since I don't use the PaintEventArgs I'm not sure how this applies to me.
Setting the groupbox width is only one of your problems.
It probably should be done like this:
groupBox.AutoSize = true;
int oWidth = groupBox1.Width;
int tWidth = (int)groupBox.CreateGraphics().
MeasureString(groupBox.Text, groupBox.Font).Width;
if (tWidth > oWidth)
{
groupBox.AutoSize = false;
groupBox.Width = tWidth;
}
Note:
This code make use of the AutoSize property. Which will make the GB wide enough to hold its content ie. the RadioButtons&CheckBoxes with their texts. Therefore these should be set before adjusting the GB sizes.
The result will have AutoSize true for some GB and false for others..
After changing the width of the GBs they will need to be repositioned unless they sit in a FlowLayoutPanel!
Their contents (The RadioButtons etc ought to still sit correctly as you placed them before). You did Add them to the GBs, right? If some don't show, post the code you create them with!
So the order is this:
Create GBs with their Text
Add contents with their Texts
Resize GBs
Reposition GBs (shouldn't be necessary with FLP)

How to Fill effect Text Box in Word Document using c#?

I want to insert text from TextBox in a word document using c# and spire.dll tools, but when I using code FillEffects not worked same this code
textBox.Format.FillEfects.Gradient.Color1 = Color.Red;
textBox.Format.FillEfects.Gradient.Color2 = Color.Yellow;
textBox.Format.FillEfects.Gradient.ShadingStyle = GradientShadingStyle.DiagonalUp;
The code is correct and my program is build correct, but it not make any changes for textBox and if I want to fill effect with picture it not worked else Like this code
textBox.Format.FillEfects.Picture = pictureBox1.Image;
I Attach To you My small project to see why this code not worked
Okay after trying to fix this for a few hours i figured it out. I have slightly re-coded your textbox but don't worry. I added comments. I worked hard on this one. The answer though, was quite simple. I have never used Spire so I had to learn as I went.
//Creates gradient
Spire.Doc.BackgroundGradient myGradient = new BackgroundGradient();
myGradient.Color1 = Color.Red;
myGradient.Color2 = Color.Yellow;
myGradient.ShadingStyle = GradientShadingStyle.DiagonalUp;
myGradient.ShadingVariant = GradientShadingVariant.ShadingDown;
//Insert TextBox
Paragraph paragraph1 = section.AddParagraph();
Spire.Doc.Fields.TextBox textBox = paragraph1.AppendTextBox(200, 100);
textBox.Format.LineColor = Color.DeepSkyBlue;
textBox.Format.LineWidth = 3.5F;
textBox.Format.LineStyle = TextBoxLineStyle.Double;
//Sets the gradient you made
textBox.Format.FillEfects.Gradient = myGradient;
//Sets the textbox to USE the gradient
textBox.Format.FillEfects.Type = BackgroundType.Gradient;
Please add below code to your program before setting the textBox fill effect.
textBox.Format.FillEfects.Type = BackgroundType.Gradient;
E-iceblue Support team.

Categories

Resources