Print multiple pages using PrintDocument - c#

I have an issue regarding printing multiple pages using the code below. It always prints only the last page. Can you guys please help me?
using (var rasterizer = new PdfRasterizer(pdfInputPdf))
{
// Create a JpegImageFormat object.
var jpegImageFormat = new JpegImageFormat(100);
// Create a FixedImageSize object with required width and height.
var imageSize = new PercentageImageSize(400);
// Save the image.
var imageData = rasterizer.Draw(jpegImageFormat, imageSize);
using (var pd = new PrintDocument())
{
var margins = new Margins(0, 40, 0, 40);
pd.DefaultPageSettings.Margins = margins;
pd.DefaultPageSettings.Color = true;
pd.DefaultPageSettings.Landscape = false;
pd.PrintPage += (sender, args) =>
{
PrintPage(text1, text2, imageData, pd, args);
};
pd.Print();
}
}
And this class:
private static int counter = 0;
private static void PrintPage(string text1, string text2, byte[][] imageData, PrintPageEventArgs args)
{
foreach (var b in imageData)
{
using (var stream = new MemoryStream(b))
{
var i = Image.FromStream(stream);
CreateNotApprovedWatermark(i, text1, text2);
if (args.PageSettings.PrinterSettings.CanDuplex)
{
args.PageSettings.PrinterSettings.Duplex = Duplex.Horizontal;
}
var m = args.MarginBounds;
if (i.Width / (double)i.Height > m.Width / (double)m.Height) // image is wider
{
m.Height = (int)(i.Height / (double)i.Width * m.Width);
}
else
{
m.Width = (int)(i.Width / (double)i.Height * m.Height);
}
args.Graphics.DrawImage(i, m);
if (counter <= 2)
{
counter++;
args.HasMorePages = true;
}
else
{
args.HasMorePages = false;
}
}
}
}

I found a easy solution to my problem after quit some trial and error:
using (var rasterizer = new PdfRasterizer(new Foxit.PDF.Rasterizer.InputPdf(pdfData)))
{
// Create a JpegImageFormat object.
var jpegImageFormat = new JpegImageFormat(100);
// Create a FixedImageSize object with required width and height.
var imageSize = new PercentageImageSize(400);
// Save the image.
var imagePages = rasterizer.Draw(jpegImageFormat, imageSize);
using (var pd = new PrintDocument())
{
var margins = new Margins(0, 40, 0, 40);
pd.DefaultPageSettings.Margins = margins;
pd.DefaultPageSettings.Color = true;
pd.DefaultPageSettings.Landscape = false;
var pageNumber = 0;
pd.PrintPage += (sender, args) =>
{
PrintPage(pageNumber, text1, text2, imagePages[pageNumber], args);
if (pageNumber < imagePages.Count())
{
pageNumber++;
args.HasMorePages = pageNumber != imagePages.Count();
}
};
pd.Print();
}
}
The problem was placement of the HasMorePages property.

Related

WinForms - When printing two datagridviews, print preview is good but on paper only 2nd dgv is printed

I am making a software to help me at work. It reads two excel (xlsx) files and get some info from them. Then it put the info in one of the two datagridviews or in both datagridviews.
Then i have a number that i need to show when i print the datagridviews. In print preview all is exactly as i want but when printing on paper it removes (or doesn't remember) everything about first datagridview and anything i print with it and start from the 2nd datagridview.
Here how it shows in print preview:
but when i print it on paper i get like this
Why the first datagridview gone on paper but if i print it PDF from Microsoft print to PDF i get PDF that has exactly as the preview.
Problem only when printing on paper.
Here is my printing code https://pastebin.com/6ZF2FevZ
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Text;
using System.Linq;
using System.Windows.Forms;
namespace Motabaka
{
class DGVsPrinter
{
private Font fontD;
private Brush brushD = Brushes.Black;
private bool dgv1Finished;
private string a3800, a1800;
public string Title { get; set; }
public bool IncludeDateAndTimeInFooter { get; set; }
public bool IncludePageNumberInFooter { get; set; }
public bool IncludeEndingPhraseAtTheEnd { get; set; }
public string EndingPhrase { get; set; } = "End";
private DataGridView dataGridView1, dataGridView2;
private int rowIndex;
private int cellCount;
private int pageNumber = 1;
private readonly PrintDocument printDoc;
private int iTotalWidth1;
private int iTotalWidth2;
private Dictionary<int, int> ColumntWidths;
private Dictionary<int, int> arrColumnWidths1 = new Dictionary<int, int>();
private Dictionary<int, int> arrColumnWidths2 = new Dictionary<int, int>();
private bool preview;
public DGVsPrinter(DataGridView dataGridView1, DataGridView dataGridView2, PrintDocument printDoc, string A3800, string A1800)
{
this.dataGridView1 = dataGridView1;
this.dataGridView2 = dataGridView2;
this.printDoc = printDoc;
fontD = new Font(dataGridView1.Font, FontStyle.Bold);
printDoc.BeginPrint += OnBeginPrint;
printDoc.PrintPage += OnPrintPage;
a3800 = A3800;
a1800 = A1800;
}
public void Print(bool preview = false)
{
this.preview = preview;
rowIndex = 0;
cellCount = 0;
pageNumber = 0;
var dgvs = new DataGridView[] { dataGridView1, dataGridView2 };
foreach (DataGridView dgv in dgvs)
{
var rows = dgv.Rows
.Cast<DataGridViewRow>()
.FirstOrDefault(r => !r.IsNewRow && r.Visible);
if (rows != null)
cellCount = rows.Cells
.Cast<DataGridViewCell>()
.Where(c => c.Visible)
.Count();
if (cellCount == 0)
{
//MessageBox.Show(new Form() { FormBorderStyle = FormBorderStyle.None }, "Nothing to print...");
continue;
}
}
if (cellCount == 0)
{
MessageBox.Show(new Form() { FormBorderStyle = FormBorderStyle.None }, "Nothing to print...");
return;
}
if (preview)
{
try
{
using (var pd = new PrintPreviewDialog())
{
pd.Document = printDoc;
pd.ShowDialog();
}
}
catch (Exception ex)
{
Error.Log(ex);
}
}
else
{
try
{
using (var pd = new PrintDialog())
{
pd.Document = printDoc;
if (pd.ShowDialog() == DialogResult.OK)
{
pd.Document.Print();
}
}
}
catch (Exception ex)
{
Error.Log(ex);
}
}
}
private void OnBeginPrint(object sender, PrintEventArgs e)
{
try
{
rowIndex = 0; pageNumber = 1;
iTotalWidth1 = 0;
iTotalWidth2 = 0;
arrColumnWidths1.Clear();
arrColumnWidths2.Clear();
foreach (DataGridViewColumn dgvGridCol in dataGridView1.Columns)
{
if (!dgvGridCol.Visible) { continue; }
iTotalWidth1 += dgvGridCol.Width;
}
foreach (DataGridViewColumn dgvGridCol in dataGridView2.Columns)
{
if (!dgvGridCol.Visible) { continue; }
iTotalWidth2 += dgvGridCol.Width;
}
}
catch (Exception ex)
{
Error.Log(ex, "Error while printing [OnBeginPrint]");
}
}
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
try
{
e.Graphics.Clear(Color.White);
var w = e.MarginBounds.Width / cellCount;
var x = e.MarginBounds.X;
var y = e.MarginBounds.Y;
int h;
Rectangle rec;
while (true)
{
var dgv = !dgv1Finished ? dataGridView1 : dataGridView2;
ColumntWidths = (dgv == dataGridView1) ? arrColumnWidths1 : arrColumnWidths2;
var totalWidths = (dgv == dataGridView1) ? iTotalWidth1 : iTotalWidth2;
var iTmpWidth = 0;
if (pageNumber == 1)
{
for (int i = dgv.Columns.Count - 1; i >= 0; i--)
{
DataGridViewColumn GridCol = dgv.Columns[i];
if (!GridCol.Visible) continue;
iTmpWidth = (int)(Math.Floor(GridCol.Width /
(double)totalWidths * totalWidths *
(e.MarginBounds.Width / (double)totalWidths)));
if (!ColumntWidths.Keys.Contains(GridCol.Index))
{
ColumntWidths.Add(GridCol.Index, iTmpWidth);
}
}
}
using (var sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
sf.Trimming = StringTrimming.None;
e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
// Uncomment to print the headers in the first page only.
//if (pageNumber == 1)
//{
h = dgv.RowTemplate.Height + 20;
#region Title
var TextToDraw = dgv.Tag?.ToString();
var xCenter = e.MarginBounds.Left + e.MarginBounds.Width / 2;
e.Graphics.DrawString(TextToDraw, fontD, brushD, xCenter, y - 20, sf);
#endregion
using (var strf = new StringFormat())
{
strf.Alignment = StringAlignment.Far;
strf.LineAlignment = StringAlignment.Near;
#region Amount
var amount = (dgv == dataGridView1) ? a1800 : a3800;
var XC = e.MarginBounds.Right;
var font = new Font(dgv.Font.FontFamily, 12f, FontStyle.Regular);
e.Graphics.DrawString(amount, font, brushD, XC, y - 20, strf);
#endregion
}
for (int i = dgv.Columns.Count - 1; i >= 0; i--)
{
var col = dgv.Columns[i];
if (col.Visible)
{
w = ColumntWidths[col.Index];
rec = new Rectangle(x, y, w, h);
e.Graphics.FillRectangle(Brushes.AliceBlue, rec);
e.Graphics.DrawString(
col.HeaderText,
col.DataGridView.Font,
Brushes.Black,
rec,
sf);
//Pen pen = new Pen(Color.Black, 1.3f);
//pen.Alignment = PenAlignment.Inset;
//e.Graphics.DrawRectangle(pen, rec);
Color[] colors;
if (false)
{
colors = new Color[]
{
SystemColors.ControlDark,
SystemColors.ControlDarkDark,
SystemColors.ControlLightLight,
SystemColors.ControlLight
};
}
else
{
colors = new Color[]
{
SystemColors.ControlLightLight,
SystemColors.ControlLight,
SystemColors.ControlDark,
SystemColors.ControlDarkDark
};
}
using (Pen p = new Pen(colors[0]))
{
e.Graphics.DrawLine(p, rec.X, rec.Bottom - 1,
rec.X, rec.Y);
e.Graphics.DrawLine(p, rec.X, rec.Y,
rec.Right - 1, rec.Y);
}
using (Pen p = new Pen(colors[1]))
{
e.Graphics.DrawLine(p, rec.X + 1, rec.Bottom - 2,
rec.X + 1, rec.Y + 1);
e.Graphics.DrawLine(p, rec.X + 1, rec.Y + 1,
rec.Right - 2, rec.Y + 1);
}
using (Pen p = new Pen(colors[2]))
{
e.Graphics.DrawLine(p, rec.X, rec.Bottom - 1,
rec.Right - 1, rec.Bottom - 1);
e.Graphics.DrawLine(p, rec.Right - 1, rec.Bottom - 1,
rec.Right - 1, rec.Y);
}
using (Pen p = new Pen(colors[3]))
{
e.Graphics.DrawLine(p, rec.X + 1, rec.Bottom - 2,
rec.Right - 2, rec.Bottom - 2);
e.Graphics.DrawLine(p, rec.Right - 2, rec.Bottom - 2,
rec.Right - 2, rec.Y + 1);
}
x += w;
}
}
x = e.MarginBounds.X;
y += h;
//}
/*Drawing the rows with their content*/
for (var i = rowIndex; i < dgv.RowCount; i++)
{
var row = dgv.Rows[i];
if (!row.Visible) continue;
if (row.IsNewRow) break;
h = GetRowHeight(e.Graphics, row, e.MarginBounds, sf);
if (h > e.MarginBounds.Height)
{
MessageBox.Show("Insufficient height.");
e.Cancel = true;
return;
}
/*Drawing row's cells with their content*/
for (int r = row.Cells.Count - 1; r >= 0; r--)
{
var cell = row.Cells[r];
if (!cell.Visible) { continue; }
w = ColumntWidths[cell.ColumnIndex];
rec = new Rectangle(x, y, w, h);
if (rec.Bottom > e.MarginBounds.Bottom)
{
pageNumber++;
rowIndex = i;
e.HasMorePages = true;
return;
}
var color = (cell.InheritedStyle.BackColor == Color.Yellow) ? new SolidBrush(SystemColors.Window) : new SolidBrush(cell.InheritedStyle.BackColor);
e.Graphics.FillRectangle(color, rec);
e.Graphics.DrawString(
cell.FormattedValue.ToString(),
dgv.Font,
Brushes.Black,
rec,
sf);
e.Graphics.DrawRectangle(Pens.Black, rec);
x += rec.Width;
}/*Done drawing the cells in a row*/
x = e.MarginBounds.X;
y += h;
}/*Done drawing all the rows*/
#region Date and time
if (IncludeDateAndTimeInFooter)
{
var strDate = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
var wdate = e.Graphics.MeasureString(strDate, fontD, e.MarginBounds.Height, sf).Width;
var hdate = e.Graphics.MeasureString(strDate, fontD, e.MarginBounds.Height, sf).Height;
var yBottom = e.MarginBounds.Height + e.MarginBounds.Top;
var recDate = new Rectangle(e.MarginBounds.X, yBottom, e.MarginBounds.Width, (int)hdate);
//e.Graphics.FillRectangle(Brushes.LightSkyBlue, recDate);
e.Graphics.DrawRectangle(Pens.Black, recDate);
e.Graphics.DrawString(strDate, fontD, brushD, recDate.X, yBottom);
}
#endregion
#region Page Number
if (IncludePageNumberInFooter)
{
var pNumber = "Page " + pageNumber;
var yBottom = e.MarginBounds.Height + e.MarginBounds.Top;
var xRight = e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(pNumber, fontD, e.MarginBounds.Width, sf).Width);
e.Graphics.DrawString(pNumber, fontD, brushD, xRight, yBottom);
}
#endregion
y = y + 70;
rowIndex = 0;
dgv1Finished = true;
e.HasMorePages = (dgv == dataGridView1);
}
if (!e.HasMorePages)
{
if (IncludeEndingPhraseAtTheEnd)
{
using (var strf = new StringFormat())
{
strf.LineAlignment = StringAlignment.Center;
strf.Alignment = StringAlignment.Center;
strf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
var fontD = new Font(dgv.Font, FontStyle.Bold);
var brushD = Brushes.Black;
var xCenter = e.MarginBounds.Left + e.MarginBounds.Width / 2;
var yBottom = e.MarginBounds.Top
+ e.MarginBounds.Height
+ e.Graphics.MeasureString(EndingPhrase, fontD, e.MarginBounds.Height, strf).Height;
e.Graphics.DrawString(EndingPhrase, fontD, brushD, xCenter, y - 60, strf);
}
}
break;
}
}
}
catch (Exception ex)
{
Error.Log(ex, " Error while printing [OnPrintPage]");
}
}
private int GetRowHeight(
Graphics g,
DataGridViewRow row,
Rectangle bounds,
StringFormat sf,
int minHeight = 22)
{
var cells = row.Cells.OfType<DataGridViewTextBoxCell>()
.Where(c => c.Visible);
if (cells == null) return minHeight;
/*(longest, next) => next.Length > longest.Length ? next : longest*/
var cell = cells.Aggregate((DataGridViewTextBoxCell)null, (x, y) => (x != null) && (!string.IsNullOrWhiteSpace(x.FormattedValue.ToString()) && !string.IsNullOrWhiteSpace(y.FormattedValue.ToString())) &&
(x.FormattedValue.ToString().Length > y.FormattedValue.ToString().Length) ? x : y);
if (cell == null) return minHeight;
var h = g.MeasureString(cell.FormattedValue.ToString(),
row.DataGridView.Font,
new SizeF(ColumntWidths[cell.ColumnIndex], bounds.Height),
sf).ToSize().Height;
return Math.Max(h + 10, minHeight); // 6 for top and bottom margins...
}
}
}
I call the print like this
var print = new DGVsPrinter(dataGridView1, dataGridView2, printDocument1, Final3800L.Text, Final1800L.Text)
{
IncludeDateAndTimeInFooter = true,
IncludePageNumberInFooter = true
};
print.Print(true);
I found the problem.
If i print it by print dialog it prints good. But when i show the preview, this problem happens. Because i didn't set dgv1Finished to false on OnBeginPrint.
when it actually prints It runs the code again and dgv1Finished is already true after the preview so it get datagridview2 first and nothing about datagridvie1.
I only have to set dgv1Finished = false in OnBeginPrint and now it prints everything.

How to print C# chart with more pages

I have C# project already done but im having issue with printing it's charts when comes out with more pages of data points, i got the scroll bar work when start getting more pages so the user can review all data on all pages but i could not find how to make the print preview shows them or print them,
when click on print, it shows only the first page on the print preview and same thing when print it out.
her is the print code:
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = this.chart1.Printing.PrintDocument;
((Form)ppd).WindowState = FormWindowState.Maximized;
chart1.Printing.PrintDocument.DefaultPageSettings.Landscape = true;
chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Left = 0;
chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Right = 0;
chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Top = 0;
chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Bottom = 0;
ppd.ShowDialog();
and here is the chart load code:
public void loadChart(string sqlvalue)//load chart method
{
chart1.ChartAreas[0].AxisY.Maximum = 55;
chart1.ChartAreas[0].AxisY.Minimum = 35;
chart1.ChartAreas[0].AxisY.Interval = 5;//control how many lines/Interval
chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = true;
chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisX.Minimum = 0;
// chart1.ChartAreas[0].AxisX.Maximum = 10;
chart1.ChartAreas[0].AxisX.Interval = 1;
//X AXES label angle
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 60;
da = new SqlDataAdapter(sqlvalue, cn.connect());
da.Fill(dt);
this.chart1.DataSource = dt;
this.chart1.Series["left"].XValueMember = dt.Columns[3].ToString();//date data
this.chart1.Series["left"].YValueMembers = dt.Columns[1].ToString();//spindle 1 data
this.chart1.Series["Right"].YValueMembers = dt.Columns[2].ToString();//spindle 2 data
//label the series lines, Backcolor and forcolor
chart1.Series[0].LabelBackColor = Color.Red;
chart1.Series[0].LabelForeColor = Color.White;
//datapoint marker's color, bordercolor,style and size
chart1.Series[0].MarkerColor = Color.White;
chart1.Series[0].MarkerBorderColor = Color.Black;
chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
chart1.Series[0].MarkerSize = 8;
//datapoint marker's color, style and size
chart1.Series[1].MarkerColor = Color.White;
chart1.Series[1].MarkerBorderColor = Color.Black;
chart1.Series[1].MarkerStyle = MarkerStyle.Circle;
chart1.Series[1].MarkerSize = 8;
chart1.Series[1].LabelBackColor = Color.Blue;
chart1.Series[1].LabelForeColor = Color.White;
//Chart background lines color
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Silver;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Silver;
this.chart1.DataBind();
cn.disconnect();
// enable autoscroll
chart1.ChartAreas[0].CursorX.AutoScroll = true;//------------
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(0, 15);
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
// set scrollbar small change to the target size
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = 15;
Legend left = new Legend();
Legend LC = CustomCloneLegend(chart1, left);
chart1.Legends.Add(LC);
chart1.Padding = Padding.Empty;
ChartArea CA = chart1.ChartAreas[0];
CA.Position = new ElementPosition(4,6, 100, 90);
}
Thank you for your time and help, here is the chart code update:
private static Image MergeImages(List<Image> imageList)
{
var finalSize = new Size();
foreach (var image in imageList)
{
if (image.Width > finalSize.Width)
{
finalSize.Width = image.Width;
}
finalSize.Height += image.Height;
}
var outputImage = new Bitmap(finalSize.Width, finalSize.Height);
using (var gfx = Graphics.FromImage(outputImage))
{
var y = 0;
foreach (var image in imageList)
{
gfx.DrawImage(image, 0, y);
y += image.Height;
}
}
return outputImage;
}
The second method:
List<Image> ChartsToImages(List<Chart> charts)
{
var imageList = new List<Image>();
foreach (var c in charts)
{
using (var ms = new MemoryStream())
{
c.SaveImage(ms, ChartImageFormat.Png);
var bmp = System.Drawing.Bitmap.FromStream(ms);
imageList.Add(bmp);
}
}
return imageList;
}
and this code
var chartList = new List<Chart> { chart1 };
var imageList = ChartsToImages(chartList);
var finalImage = MergeImages(imageList);
finalImage.Save("D:\\Junk.png", ImageFormat.Png);
Im not sure is that what you mean by your first comment, but i found this code here under Converting chart to image questions. this code convert and saves the chart in the same amount of pages but i need to show them in the printpreviewcontrol and print them.
Below code refers to the as per page count starting point and ending point based printing. And Grid view value chars are row loop based counting the page.
private int numberOfItemsPerPage = 0;
private int numberOfItemsPrintedSoFar = 0;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int height = 480; //page height stating point
for (int l = numberOfItemsPrintedSoFar; l < dataGridView2.Rows.Count; l++)
{
numberOfItemsPerPage = numberOfItemsPerPage + 1;
if (numberOfItemsPerPage <= 25) // 25 is Page Line Item
{
numberOfItemsPrintedSoFar++;
if (numberOfItemsPrintedSoFar <= dataGridView2.Rows.Count)
{
height += dataGridView2.Rows[0].Height;
e.Graphics.DrawString(dataGridView2.Rows[l].Cells[0].FormattedValue.ToString(), dataGridView2.Font = new Font("Book Antiqua", 8), Brushes.Black, new RectangleF(5, height, dataGridView2.Columns[0].Width, dataGridView2.Rows[0].Height));
e.Graphics.DrawString(dataGridView2.Rows[l].Cells[1].FormattedValue.ToString(), dataGridView2.Font = new Font("Book Antiqua", 8), Brushes.Black, new RectangleF(170, height, dataGridView2.Columns[0].Width, dataGridView2.Rows[0].Height));
e.Graphics.DrawString(dataGridView2.Rows[l].Cells[2].FormattedValue.ToString(), dataGridView2.Font = new Font("Book Antiqua", 8), Brushes.Black, new RectangleF(290, height, dataGridView2.Columns[0].Width, dataGridView2.Rows[0].Height));
e.Graphics.DrawString(dataGridView2.Rows[l].Cells[3].FormattedValue.ToString(), dataGridView2.Font = new Font("Book Antiqua", 8), Brushes.Black, new RectangleF(345, height, dataGridView2.Columns[0].Width, dataGridView2.Rows[0].Height));
}
else
{
e.HasMorePages = false;
}
}
else
{
numberOfItemsPerPage = 0;
e.HasMorePages = true;
return;
}
}
numberOfItemsPerPage = 0;
numberOfItemsPrintedSoFar = 0;
}

Sending wrong paper size to printer

We have started a project for printing, however we are completely stuck when it comes to telling the printer what paper size is selected.
Everytime we select the paper size and hit print, the printer preview is showing A4 everytime and not our selected size although if we open the print preferences the correct size is selected.
namespace CPrint
{
/// <summary>
/// Логика взаимодействия для ucPrint.xaml
/// </summary>
public partial class ucPrint : UserControl
{
bool SystemChange = false;
double? PaperHeight = null;
double? PaperWidth = null;
public ucPrint()
{
InitializeComponent();
App.Localization.AddControls(this, new string[]
{
"cHeader", "lPrinter", "lCopies","lLayout", "bPrintSettings","lColorManagement","lPrinterProfile", "lPositionSize", "cCenter", "lTop", "lLeft"
});
}
public static BitmapSource ConvertColorProfile(BitmapSource bitmapSource, ColorContext sourceProfile, ColorContext destinationProfile)
{
var bitmapConverted = new ColorConvertedBitmap();
bitmapConverted.BeginInit();
bitmapConverted.Source = bitmapSource;
//bitmapConverted.SourceColorContext = new ColorContext(PixelFormats.Pbgra32);// bitmapSourceFrame.ColorContexts == null ? sourceProfile : bitmapSourceFrame.ColorContexts[0];
bitmapConverted.SourceColorContext = sourceProfile;
bitmapConverted.DestinationColorContext = destinationProfile;
bitmapConverted.DestinationFormat = PixelFormats.Bgra32;
bitmapConverted.EndInit();
return bitmapConverted;
}
private void BPrint_Click(object sender, RoutedEventArgs e)
{
if (cPrinter.SelectedItem == null) { MessageBox.Show("Printer not set"); return; }
if (cPaperSize.SelectedItem == null) { MessageBox.Show("Paper size not set"); return; }
double marging = 30;
if (App.CurrentTemplateControl != null)
{
var img = App.CurrentTemplateControl.GetImage(true);
if (img == null) return;
var image = new Image() { Source = img };
if (cColorProfile != null && cColorProfile.SelectedItem != null && cColorProfile.SelectedIndex > 0)
{
Uri sourceProfileUri = new Uri((cColorProfile.SelectedItem as FileInfo).FullName);
image.Source = ConvertColorProfile(image.Source as BitmapSource, new ColorContext(PixelFormats.Pbgra32), new ColorContext(sourceProfileUri));
}
if (cMirror.IsChecked == true)
{
var transformGroup = new TransformGroup();
transformGroup.Children.Add(new ScaleTransform(-1, 1, img.Width / 2, img.Height / 2));
image.RenderTransform = transformGroup;
}
PrintDialog printDialog2 = new PrintDialog();
Size size = (Size)(cPaperSize.SelectedItem as ComboBoxItem).DataContext;
printDialog2.PrintQueue = new PrintQueue(new PrintServer(), cPrinter.Text);
//if (printDialog2.ShowDialog() == true)
//{
//Size size = new Size(printDialog2.PrintableAreaWidth, printDialog2.PrintableAreaHeight);
printDialog2.PrintTicket = new PrintTicket()
{
PageMediaSize = new PageMediaSize(size.Width, size.Height)
};
//printDialog2.PrintTicket
Canvas canvas = new Canvas()
{
//Height = PrintContext.ToPx(size.Height),
//Width = PrintContext.ToPx(size.Width),
Height = size.Height,
Width = size.Width,
Background = Brushes.White
};
canvas.Children.Add(image);
double scaleW = (size.Width - marging * 2) / img.Width;
double scaleH = (size.Height - marging * 2) / img.Height;
if (scaleW < 1 || scaleH < 1)
{
Canvas.SetLeft(image, marging);
Canvas.SetTop(image, marging);
double scale = scaleW > scaleH ? scaleH : scaleW;
var transformGroup = new TransformGroup();
transformGroup.Children.Add(new ScaleTransform(scale, scale, 0, 0));
image.RenderTransform = transformGroup;
}
else if (cCenter.IsChecked == true)
{
Canvas.SetLeft(image, size.Width / 2 - img.Width / 2);
Canvas.SetTop(image, size.Height / 2 - img.Height / 2);
}
else
{
Canvas.SetLeft(image, marging);
Canvas.SetTop(image, marging);
}
printDialog2.PrintVisual(canvas, "Print");
//}
}
return;
}
private void CPrinter_DropDownOpened(object sender, EventArgs e)
{
SystemChange = true;
var lastPrinterName = cPrinter.Text;
cPrinter.Items.Clear();
int index = -1;
cPrinter.SelectedIndex = index;
foreach (string strPrinter in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
index++;
cPrinter.Items.Add(strPrinter);
if (strPrinter == lastPrinterName)
cPrinter.SelectedIndex = index;
}
SystemChange = false;
}
private void CPrinter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0 && SystemChange == false)
{
var printer = new System.Drawing.Printing.PrinterSettings();
printer.PrinterName = e.AddedItems[0].ToString();
var lastPaperName = cPaperSize.Text;
cPaperSize.Items.Clear();
int index = -1;
cPaperSize.SelectedIndex = index;
foreach (System.Drawing.Printing.PaperSize paper in printer.PaperSizes)
{
index++;
cPaperSize.Items.Add(new ComboBoxItem() { Content = paper.PaperName, DataContext = new Size(paper.Width, paper.Height) });
if (paper.PaperName == lastPaperName)
cPaperSize.SelectedIndex = index;
}
Properties.Settings.Default.DefaultDirectPrinter = printer.PrinterName;
Properties.Settings.Default.Save();
}
}
private void CPaperSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
Properties.Settings.Default.DefaultDirectPaper = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
Properties.Settings.Default.Save();
}
}
public void UpdateControls()
{
SystemChange = true;
if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.DefaultDirectPrinter))
{
SystemChange = true;
var lastPrinterName = cPrinter.Text;
cPrinter.Items.Clear();
int index = -1;
cPrinter.SelectedIndex = index;
foreach (string strPrinter in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
index++;
cPrinter.Items.Add(strPrinter);
if (strPrinter == Properties.Settings.Default.DefaultDirectPrinter)
cPrinter.SelectedIndex = index;
}
SystemChange = false;
if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.DefaultDirectPaper))
{
var printer = new System.Drawing.Printing.PrinterSettings();
printer.PrinterName = Properties.Settings.Default.DefaultDirectPrinter;
string lastPaperName = Properties.Settings.Default.DefaultDirectPaper;
cPaperSize.Items.Clear();
int indexP = -1;
cPaperSize.SelectedIndex = indexP;
foreach (System.Drawing.Printing.PaperSize paper in printer.PaperSizes)
{
indexP++;
cPaperSize.Items.Add(new ComboBoxItem() { Content = paper.PaperName, DataContext = new Size(paper.Width, paper.Height) });
if (paper.PaperName == lastPaperName)
cPaperSize.SelectedIndex = indexP;
}
}
}
if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.DefaultDirectColorProfile))
{
var lastValue = Properties.Settings.Default.DefaultDirectColorProfile;
cColorProfile.Items.Clear();
int index = -1;
cColorProfile.SelectedIndex = index;
cColorProfile.Items.Add("");
index++;
foreach (var file in App.Icc.items)
{
index++;
cColorProfile.Items.Add(file);
if (file.FullName == lastValue)
cColorProfile.SelectedIndex = index;
}
}
SystemChange = false;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
private void CColorProfile_DropDownOpened(object sender, EventArgs e)
{
var lastValue = cColorProfile.Text;
cColorProfile.Items.Clear();
int index = -1;
cColorProfile.SelectedIndex = index;
cColorProfile.Items.Add("");
index++;
foreach (var file in App.Icc.items)
{
index++;
cColorProfile.Items.Add(file);
if (file.Name == lastValue)
cColorProfile.SelectedIndex = index;
}
}
private void CColorProfile_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!SystemChange)
{
Properties.Settings.Default.DefaultDirectColorProfile = (cColorProfile.SelectedItem as FileInfo)?.FullName;
Properties.Settings.Default.Save();
}
}
}
}
I expect if we select A5, it tells the print driver to print A5,
If we select a custom "user defined" paper size, it tells the printer which size is selected. And not fixing this at A4 everytime
We cant seem to set the paper size outside the print dialog.
I believe; following this and this MSDN article; you are going to want to do something along the lines of:
System.Drawing.Printing.PaperSize paperSize = new System.Drawing.Printing.PaperSize("custom", width, height);
PrintDocument printDoc = new PrintDocument();
printDoc.DefaultPageSettings.PaperSize = paperSize;
I have walk through your code,
I think some event raise by front end (XAML) on specific use cases that is the overriding the actual value in "Properties.Settings.Default."
It would be better to resolve if you provide a XAML code for this issue.
I can look into it and will give you better solution.
You can share me code here is my skype : shsakariya

How to insert picture into excel using MemoryStream?

Below are two methods, one uses MemoryStream and the other uses real file on the disk.
public void InsertImage(long x, long y, long? width, long? height, string sImagePath, WorksheetPart wsp)
{
try
{
DrawingsPart dp;
ImagePart imgp;
WorksheetDrawing wsd;
ImagePartType ipt;
switch (sImagePath.Substring(sImagePath.LastIndexOf('.') + 1).ToLower())
{
case "png":
ipt = ImagePartType.Png;
break;
case "jpg":
case "jpeg":
ipt = ImagePartType.Jpeg;
break;
case "gif":
ipt = ImagePartType.Gif;
break;
default:
return;
}
if (wsp.DrawingsPart == null)
{
//----- no drawing part exists, add a new one
dp = wsp.AddNewPart<DrawingsPart>();
imgp = dp.AddImagePart(ipt, wsp.GetIdOfPart(dp));
wsd = new WorksheetDrawing();
}
else
{
//----- use existing drawing part
dp = wsp.DrawingsPart;
imgp = dp.AddImagePart(ipt);
dp.CreateRelationshipToPart(imgp);
wsd = dp.WorksheetDrawing;
}
using (FileStream fs = new FileStream(sImagePath, FileMode.Open))
{
imgp.FeedData(fs);
}
int imageNumber = dp.ImageParts.Count<ImagePart>();
if (imageNumber == 1)
{
Drawing drawing = new Drawing();
drawing.Id = dp.GetIdOfPart(imgp);
wsp.Worksheet.Append(drawing);
}
NonVisualDrawingProperties nvdp = new NonVisualDrawingProperties();
nvdp.Id = new UInt32Value((uint)(1024 + imageNumber));
nvdp.Name = "Picture " + imageNumber.ToString();
nvdp.Description = "";
DocumentFormat.OpenXml.Drawing.PictureLocks picLocks = new DocumentFormat.OpenXml.Drawing.PictureLocks();
picLocks.NoChangeAspect = true;
picLocks.NoChangeArrowheads = true;
NonVisualPictureDrawingProperties nvpdp = new NonVisualPictureDrawingProperties();
nvpdp.PictureLocks = picLocks;
NonVisualPictureProperties nvpp = new NonVisualPictureProperties();
nvpp.NonVisualDrawingProperties = nvdp;
nvpp.NonVisualPictureDrawingProperties = nvpdp;
DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
stretch.FillRectangle = new DocumentFormat.OpenXml.Drawing.FillRectangle();
BlipFill blipFill = new BlipFill();
DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip();
blip.Embed = dp.GetIdOfPart(imgp);
blip.CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print;
blipFill.Blip = blip;
blipFill.SourceRectangle = new DocumentFormat.OpenXml.Drawing.SourceRectangle();
blipFill.Append(stretch);
DocumentFormat.OpenXml.Drawing.Transform2D t2d = new DocumentFormat.OpenXml.Drawing.Transform2D();
DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset();
offset.X = 0;
offset.Y = 0;
t2d.Offset = offset;
Bitmap bm = new Bitmap(sImagePath);
DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents();
if (width == null)
extents.Cx = (long)bm.Width * (long)((float)914400 / bm.HorizontalResolution);
else
extents.Cx = width * (long)((float)914400 / bm.HorizontalResolution);
if (height == null)
extents.Cy = (long)bm.Height * (long)((float)914400 / bm.VerticalResolution);
else
extents.Cy = height * (long)((float)914400 / bm.VerticalResolution);
bm.Dispose();
t2d.Extents = extents;
ShapeProperties sp = new ShapeProperties();
sp.BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto;
sp.Transform2D = t2d;
DocumentFormat.OpenXml.Drawing.PresetGeometry prstGeom = new DocumentFormat.OpenXml.Drawing.PresetGeometry();
prstGeom.Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle;
prstGeom.AdjustValueList = new DocumentFormat.OpenXml.Drawing.AdjustValueList();
sp.Append(prstGeom);
sp.Append(new DocumentFormat.OpenXml.Drawing.NoFill());
DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picture = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture();
picture.NonVisualPictureProperties = nvpp;
picture.BlipFill = blipFill;
picture.ShapeProperties = sp;
Position pos = new Position();
pos.X = x * 914400 / 72;
pos.Y = y * 914400 / 72;
Extent ext = new Extent();
ext.Cx = extents.Cx;
ext.Cy = extents.Cy;
AbsoluteAnchor anchor = new AbsoluteAnchor();
anchor.Position = pos;
anchor.Extent = ext;
anchor.Append(picture);
anchor.Append(new ClientData());
wsd.Append(anchor);
wsd.Save(dp);
}
catch (Exception ex)
{
throw ex; // or do something more interesting if you want
}
}
//use memorystream
public void InsertImage(long x, long y, long? width, long? height, MemoryStream ms, WorksheetPart wsp)
{
try
{
DrawingsPart dp;
ImagePart imgp;
WorksheetDrawing wsd;
ImagePartType ipt = ImagePartType.Jpeg;
if (wsp.DrawingsPart == null)
{
//----- no drawing part exists, add a new one
dp = wsp.AddNewPart<DrawingsPart>();
imgp = dp.AddImagePart(ipt, wsp.GetIdOfPart(dp));
wsd = new WorksheetDrawing();
}
else
{
//----- use existing drawing part
dp = wsp.DrawingsPart;
imgp = dp.AddImagePart(ipt);
dp.CreateRelationshipToPart(imgp);
wsd = dp.WorksheetDrawing;
}
Bitmap bitmap = new Bitmap(ms);
imgp.FeedData(ms);
int imageNumber = dp.ImageParts.Count<ImagePart>();
if (imageNumber == 1)
{
Drawing drawing = new Drawing();
drawing.Id = dp.GetIdOfPart(imgp);
wsp.Worksheet.Append(drawing);
}
NonVisualDrawingProperties nvdp = new NonVisualDrawingProperties();
nvdp.Id = new UInt32Value((uint)(1024 + imageNumber));
nvdp.Name = "Picture " + imageNumber.ToString();
nvdp.Description = "";
DocumentFormat.OpenXml.Drawing.PictureLocks picLocks = new DocumentFormat.OpenXml.Drawing.PictureLocks();
picLocks.NoChangeAspect = true;
picLocks.NoChangeArrowheads = true;
NonVisualPictureDrawingProperties nvpdp = new NonVisualPictureDrawingProperties();
nvpdp.PictureLocks = picLocks;
NonVisualPictureProperties nvpp = new NonVisualPictureProperties();
nvpp.NonVisualDrawingProperties = nvdp;
nvpp.NonVisualPictureDrawingProperties = nvpdp;
DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
stretch.FillRectangle = new DocumentFormat.OpenXml.Drawing.FillRectangle();
BlipFill blipFill = new BlipFill();
DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip();
blip.Embed = dp.GetIdOfPart(imgp);
blip.CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print;
blipFill.Blip = blip;
blipFill.SourceRectangle = new DocumentFormat.OpenXml.Drawing.SourceRectangle();
blipFill.Append(stretch);
DocumentFormat.OpenXml.Drawing.Transform2D t2d = new DocumentFormat.OpenXml.Drawing.Transform2D();
DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset();
offset.X = 0;
offset.Y = 0;
t2d.Offset = offset;
DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents();
//Bitmap bitmap = new Bitmap(ms);
if (width == null)
extents.Cx = (long)bitmap.Width * (long)((float)914400 / bitmap.HorizontalResolution);
else
extents.Cx = width * (long)((float)914400 / bitmap.HorizontalResolution);
if (height == null)
extents.Cy = (long)bitmap.Height * (long)((float)914400 / bitmap.VerticalResolution);
else
extents.Cy = height * (long)((float)914400 / bitmap.VerticalResolution);
bitmap.Dispose();
t2d.Extents = extents;
ShapeProperties sp = new ShapeProperties();
sp.BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto;
sp.Transform2D = t2d;
DocumentFormat.OpenXml.Drawing.PresetGeometry prstGeom = new DocumentFormat.OpenXml.Drawing.PresetGeometry();
prstGeom.Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle;
prstGeom.AdjustValueList = new DocumentFormat.OpenXml.Drawing.AdjustValueList();
sp.Append(prstGeom);
sp.Append(new DocumentFormat.OpenXml.Drawing.NoFill());
DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picture = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture();
picture.NonVisualPictureProperties = nvpp;
picture.BlipFill = blipFill;
picture.ShapeProperties = sp;
Position pos = new Position();
pos.X = x * 914400 / 72;
pos.Y = y * 914400 / 72;
Extent ext = new Extent();
ext.Cx = extents.Cx;
ext.Cy = extents.Cy;
AbsoluteAnchor anchor = new AbsoluteAnchor();
anchor.Position = pos;
anchor.Extent = ext;
anchor.Append(picture);
anchor.Append(new ClientData());
wsd.Append(anchor);
wsd.Save(dp);
}
catch (Exception ex)
{
throw ex; // or do something more interesting if you want
}
}
If I invoke the first method(use real file on the disk), it's ok, I can insert my picture into excel file. But if I read the file into memorystream and invoke method2, I can see the picture rectangle with error message.
So my question is how can I insert picture into excel via memorystream? Because I won't create too many files on disk.
I believe you need to create bitmap image data from stream first
There's a solution already for that here on Stack Overflow: Byte Array to Bitmap Image
I copy-paste the code from the solution:
int w= 100;
int h = 200;
int ch = 3; //number of channels (ie. assuming 24 bit RGB in this case)
byte[] imageData = new byte[w*h*ch]; //you image data here
Bitmap bitmap = new Bitmap(w,h,PixelFormat.Format24bppRgb);
BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr pNative = bmData.Scan0;
Marshal.Copy(imageData,0,pNative,w*h*ch);
bitmap.UnlockBits(bmData);
OK, I think I have found solution.
I change memorystream parameter to string, and convert it to memorystream like below:
MemoryStream ms = new System.IO.MemoryStream(System.Convert.FromBase64String(str))
Now, it works.
I learn it from open xml sdk 2.5 productivity tool.

Clicking on image always shows last image in full view Xamarin IOS

In tableview cell I am dynamically showing some messages and images,If i am having more images
showing in horizontal scroll. now what my task is Clicking on particular image need to show that image in full page view, i have done the code, but my problem is clicking on any image showing last image in full view.
Can anyone please help me.
if (contents.Count > 0)
{
_scrollView = new UIScrollView (){Tag = 1002};
//_image = new UIImageView (UIImage.FromBundle("placeholder.png"));
_scrollView.Frame = new RectangleF (0, rowHeight, 320, 300);
_scrollView.PagingEnabled = true;
_scrollView.Bounces = true;
_scrollView.DelaysContentTouches = true;
_scrollView.ShowsHorizontalScrollIndicator = true;
//_scrollView.ScrollRectToVisible (new RectangleF (320, 0, 320, 480), true);
ContentView.Add(_scrollView);
int x = 35;
bool setRowHeight = true;
int scrollwidth = 0;
foreach (var contentItem in contents)
{
if (contentItem.FilePath.Length != 0)
{
index++;
//_image.SetImage (new NSUrl (contentItem.FilePath), UIImage.FromBundle ("placeholder.png"));
//_image.Tag = 1001;
_image = new UIImageView(FromUrl(contentItem.FilePath)) { Tag = 1001 };
_imagePath = contentItem.FilePath;
_image.Frame = new RectangleF(x, 0, 250, _image.Image.CGImage.Height);
x = x + (Convert.ToInt32( Bounds.Width));
scrollwidth = scrollwidth + Convert.ToInt32( Bounds.Width);
_scrollView.ContentSize = new SizeF (scrollwidth,150);
_scrollView.Add (_image);
if (setRowHeight) {
rowHeight += _image.Image.CGImage.Height + 10;
setRowHeight = false;
}
// ================
var doubletap = new UITapGestureRecognizer();
doubletap.NumberOfTapsRequired = 1; // single tap
doubletap.AddTarget(this, new MonoTouch.ObjCRuntime.Selector("DoubleTapSelector"));
_image.AddGestureRecognizer(doubletap); // detect when the scrollView is tapped
_image.UserInteractionEnabled = true;
// =================
}
}
}
[MonoTouch.Foundation.Export("DoubleTapSelector")]
public void OnDoubleTap(UIGestureRecognizer sender)
{
FullImageViewControler fullImageViewController = new FullImageViewControler(_imagePath);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(fullImageViewController, true, null);
}

Categories

Resources