Javascript 打印到纸张C的特定区域#

Javascript 打印到纸张C的特定区域#,javascript,c#,jquery,asp.net-mvc-4,printing,Javascript,C#,Jquery,Asp.net Mvc 4,Printing,我正在努力实现的目标 我正在开发一个程序,在这个程序中,我想在标签上打印一个名字,标签在8 1/2*14的纸上有4*2列,可以剥离并粘贴到其他东西上 我拥有的 到目前为止,当我单击“打印”时,会弹出一个模式表单,其中有8个4*2的按钮,表示纸张上的标签 我需要什么 现在当我点击任何一个按钮, 我想将它们的位置或硬编码的按钮id传递到页面上的特定位置,因此如果我单击行中的第二个按钮,我希望我的打印位于第二行。本质上,选择按钮表示要打印的标签 (让我解释一下) 假设单击了第一个按钮,我想将我的名字打

我正在努力实现的目标

我正在开发一个程序,在这个程序中,我想在标签上打印一个名字,标签在8 1/2*14的纸上有4*2列,可以剥离并粘贴到其他东西上

我拥有的

到目前为止,当我单击“打印”时,会弹出一个模式表单,其中有8个4*2的按钮,表示纸张上的标签

我需要什么

现在当我点击任何一个按钮, 我想将它们的位置或硬编码的按钮id传递到页面上的特定位置,因此如果我单击行中的第二个按钮,我希望我的打印位于第二行。本质上,选择按钮表示要打印的标签

(让我解释一下) 假设单击了第一个按钮,我想将我的名字打印在位置的纸张上

x: 0
y: 0
假设第二个按钮被点击,我想把我的名字打印在 现场纸张

x: 5 (five being halfway)
y: 0
按钮三将使打印在

x:0
y:-5 
我使用中的代码创建了这个类

using System;
using System.Collections.Generic;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace MKML_Labels
{
    /// <summary>
    /// Contains code to generate a Word document that can
    /// be used to print &/or save the labels.
    /// </summary>
    public class Labels
    {
        #region Fields
        public enum DestinationType : short
        {
            Invalid = 0,
            Print,
            Save,
            Both,
            Open
        }

        private DestinationType destType;
        private string saveFilePath;
        private const int BOLD = 1;
        private const int UNBOLD = 0;

        // L7160 is an Avery type, 21 labels per A4 sheet, 63.5x38.1 mm. See e.g. Amazon ASIN: B0082AWFP8
        private const string LABEL_TYPE = "L7160";

        // The following constants depend on the label type
        private const int NUM_COLUMNS = 6;  // Number of columns on a sheet of labels, 3 labels and 3 separators
        private const int NUM_ROWS = 7; // Number of rows on a page (or sheet of labels)
        private const int NAME_COLUMN_WIDTH = 130;
        private const float NAME_FONT_SIZE = 14.0F;
        private const float NUMBER_FONT_SIZE = 18.0F;
        private const float TEXT_FONT_SIZE = 10.0F;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dest">One of the DestinationType enum values</param>
        /// <param name="path">Full path to the saved file (Word document containing the labels). May be empty if Save not chosen</param>
        public Labels(DestinationType dest, string path)
        {
            destType = dest;
            saveFilePath = path;
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Print the labels
        /// Copied and amended from https://stackoverflow.com/questions/18056117/miscrosoft-word-label-printing-utility-issue
        /// </summary>
        /// <param name="creditors">List of creditors</param>
        /// <exception cref=">ApplicationException">Thrown when a Word error occurs</exception>
        public void PrintLabels(List<Creditor> creditors)
        {
            Application wordApp;
            wordApp = new Application();
            Document wordDoc = null;
            Object missing = System.Reflection.Missing.Value;

            try
            {
                wordDoc = wordApp.Documents.Add();

                // This adds one page full of a table with space for 21 labels. See below if more pages are necessary
                // I don't know WHY we need 2 documents, but I can't get it to work with only one.
                var newDoc = wordApp.MailingLabel.CreateNewDocument(LABEL_TYPE, "", Type.Missing, false, Type.Missing, Type.Missing, Type.Missing);
                wordApp.Visible = false;

                // Close the empty, original document
                ((_Document)wordDoc).Close(false, missing, missing);

                var table = newDoc.Content.ConvertToTable().Tables[1];
                int column = -1;
                int row = 1;

                // When row > n * 7, need to add new rows, because we have started a new page
                foreach (Creditor c in creditors)
                {
                    column += 2;
                    if (column > NUM_COLUMNS)
                    {
                        column = 1;
                        row++;
                        if (row > NUM_ROWS)
                        {
                            // After filling the first page, add a new row as required
                            table.Rows.Add();
                        }
                    }

                    // Create an inner table in the cell, with the name in bold and the number right-justified
                    var innertable = table.Cell(row, column).Range.ConvertToTable();
                    innertable.Columns[2].Cells[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                    innertable.Columns[1].Cells[1].SetWidth(NAME_COLUMN_WIDTH, WdRulerStyle.wdAdjustFirstColumn);
                    innertable.Columns[1].Cells[1].Range.Text = c.Name;
                    innertable.Columns[1].Cells[1].Range.Font.Bold = BOLD;
                    innertable.Columns[1].Cells[1].Range.Font.Color = WdColor.wdColorBlack;
                    innertable.Columns[1].Cells[1].Range.Font.Size = NAME_FONT_SIZE;

                    innertable.Columns[2].Cells[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                    innertable.Columns[2].Cells[1].Range.Text = c.LineNumber;
                    innertable.Columns[2].Cells[1].Range.Font.Bold = UNBOLD;
                    innertable.Columns[2].Cells[1].Range.Font.Color = WdColor.wdColorPink;
                    innertable.Columns[2].Cells[1].Range.Font.Size = NUMBER_FONT_SIZE;

                    // Add constants and text for optional data
                    // reference and phone are never in CFS data, and are optional in the Centre Manager database
                    innertable.Rows.Add();
                    Cell cell = innertable.Cell((row + 1), 1);
                    cell.Range.Font.Bold = UNBOLD;
                    cell.Range.Font.Color = WdColor.wdColorBlack;
                    cell.Range.Font.Size = TEXT_FONT_SIZE;
                    cell.Range.Text = "Ref. No.: " + c.Reference;
                    innertable.Rows.Add();
                    cell = innertable.Cell((row + 2), 1);
                    cell.Range.Text = "Tel. No.: " + c.Phone;
                }

                if (destType == DestinationType.Save || destType == DestinationType.Both)
                {
                    // Save and close the document
                    // It seems necessary to use a file name without an extension, if the format is specified
                    WdSaveFormat format = (Path.GetExtension(saveFilePath) == ".docx") ? WdSaveFormat.wdFormatDocument : WdSaveFormat.wdFormatDocument97;
                    string saveFile = Path.GetDirectoryName(saveFilePath) + "\\" + Path.GetFileNameWithoutExtension(saveFilePath);
                    newDoc.SaveAs(saveFile,
                        format, missing, missing,
                        false, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing,
                        missing, missing);

                    ((_Document)newDoc).Close(false, missing, missing);
                }

                if (destType == DestinationType.Print || destType == DestinationType.Both)
                {
                    // Print the labels
                    System.Windows.Forms.PrintDialog pDialog = new System.Windows.Forms.PrintDialog();
                    if (pDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        wordApp.ActivePrinter = pDialog.PrinterSettings.PrinterName;
                        newDoc.PrintOut();
                    }

                    ((_Document)newDoc).Close(false, missing, missing);
                }

                if (destType == DestinationType.Open)
                {
                    // Don't close the document, the user is editting it
                    wordApp.Visible = true;
                }
            }

            // Does not catch ApplicationException, allow it to be passed to the caller
            catch (System.Runtime.InteropServices.COMException eCOM)
            {
                throw new ApplicationException("Word document create failed", eCOM);
            }
        }
        #endregion
    }
}
使用系统;
使用System.Collections.Generic;
使用Microsoft.Office.Interop.Word;
使用System.IO;
名称空间MKML_标签
{
/// 
///包含生成Word文档的代码,该文档可以
///用于打印和/或保存标签。
/// 
公共类标签
{
#区域字段
公共枚举目标类型:short
{
无效=0,
印刷品,
拯救
二者都
打开
}
私有DestinationType destType;
私有字符串保存文件路径;
私有常量int BOLD=1;
私有常量int unbld=0;
//L7160是一种Avery类型,每A4页21个标签,63.5x38.1 mm。参见亚马逊ASIN:B0082AWFP8
私有常量字符串标签\u TYPE=“L7160”;
//以下常量取决于标签类型
private const int NUM_COLUMNS=6;//一张标签上的列数、3个标签和3个分隔符
private const int NUM_ROWS=7;//页面(或标签页)上的行数
private const int NAME_COLUMN_WIDTH=130;
private const float NAME\u FONT\u SIZE=14.0F;
私有常量浮点数\u FONT\u SIZE=18.0F;
private const float TEXT\u FONT\u SIZE=10.0F;
#端区
#区域构造函数
/// 
///建造师
/// 
///DestinationType枚举值之一
///保存文件(包含标签的Word文档)的完整路径。如果未选择“保存”,则可能为空
公共标签(DestinationType dest,字符串路径)
{
destType=dest;
saveFilePath=path;
}
#端区
#区域公共方法
/// 
///打印标签
///抄袭并修订自https://stackoverflow.com/questions/18056117/miscrosoft-word-label-printing-utility-issue
/// 
///债权人名单
///发生单词错误时引发
公共作废打印标签(债权人名单)
{
应用wordApp;
wordApp=新应用程序();
单据wordDoc=null;
缺少对象=System.Reflection.missing.Value;
尝试
{
wordDoc=wordApp.Documents.Add();
//这将添加一个充满表格的页面,其中有21个标签的空间。如果需要更多页面,请参阅下文
//我不知道为什么我们需要两个文档,但我不能让它只使用一个。
var newDoc=wordApp.MailingLabel.CreateNewDocument(LABEL_TYPE,“”,TYPE.Missing,false,TYPE.Missing,TYPE.Missing,TYPE.Missing);
可见=false;
//关闭空的原始文档
((_Document)wordDoc.Close(false、missing、missing);
var table=newDoc.Content.ConvertToTable().Tables[1];
int列=-1;
int行=1;
//当row>n*7时,需要添加新行,因为我们已经开始了一个新页面
foreach(债权人中的债权人c)
{
列+=2;
如果(列>列数)
{
列=1;
行++;
如果(行>行数)
{
//填写第一页后,根据需要添加新行
table.Rows.Add();
}
}
//在单元格中创建一个内部表格,名称以粗体显示,数字右对齐
var innertable=table.Cell(行、列).Range.ConvertToTable();
innertable.Columns[2]。单元格[1]。Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphLeft;
innertable.Columns[1]。单元格[1]。SetWidth(NAME\u COLUMN\u WIDTH,WdRulerStyle.wdAdjustFirstColumn);
innertable.Columns[1]。单元格[1]。Range.Text=c.Name;
innertable.Columns[1]。单元格[1]。Range.Font.Bold=Bold;
innertable.Columns[1]。单元格[1]。Range.Font.Color=WdColor.wdColorBlack;
innertable.Columns[1]。单元格[1]。Range.Font.Size=NAME\u Font\u Size;
innertable.Columns[2]。单元格[1]。Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphRight;
innertable.Columns[2]。单元格[1]。Range.Text=c.LineNumber;
innertable.Columns[2]。单元格[1]。Range.Font.Bold=未绑定;
innertable.Columns[2]。单元格[1]。Range.Font.Color=WdColor.wdColorPink;
innertable.Columns[2]。单元格[1]。Range.Font.Size=NUMBER\u Font\u Size;
//为可选数据添加常量和文本
//参考和电话从不在CFS数据中,在中是可选的
<table>
    <tr>
        <td class="active">
            <div id="content">@Model.SomeProperty</div>
        </td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</table>
<div id="message">Click on a table cell to position the content</div>