我想要实现的目标
我正在制作一个程序,我想在标签上打印一个名称,标签在8 1/2 * 14纸上的4 * 2列中,可以剥离并粘贴到其他东西上。 。
我有什么
到目前为止,当我点击打印时,弹出一个模式表单,弹出4 * 2中的8个按钮,表示纸上的标签。
我需要什么
现在当我点击任何这些按钮时, 我想将他们的位置或硬编码的按钮ID传递给页面上的特定位置,所以如果我单击行中的第二个按钮,id就像我的打印位于第二行。基本上,选择按钮代表您要打印的标签。
(让我解释) 让我们说点击第一个按钮就像发送我的名字一样打印在纸张上的位置
x: 0
y: 0
让我们说第二个按钮被点击,我喜欢发送我的名字来打印 地点纸张
x: 5 (five being halfway)
y: 0
和按钮3将导致打印发生在
x:0
y:-5
答案 0 :(得分:3)
在您的视图中,创建一个表格来表示标签网格,并根据页面/标签大小设置<td>
元素的样式,并在第一个表格单元格中添加要打印的内容。
然后使用jquery处理每个表格单元格的click事件,以将内容重新定位到相应的标签。
最后添加@media print
样式以防止在页面上打印其他元素。
您的视图可能看起来像(对于3 x 2标签网格)
<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>
和脚本
var content = $('#content');
$('td').click(function() {
$('td').removeClass('active');
$(this).append(content).addClass('active');
})
和css
table {
border-collapse: collapse;
table-layout:fixed;
}
td {
box-sizing: border-box;
border: dashed grey 1px;
width: 150px; /*adjust to suit your label size*/
height: 75px; /*adjust to suit your label size*/
vertical-align: top;
}
td.active { /*adds an extra visual indicator*/
border: solid black 1px;
}
#content {
padding: 5px;
}
#message {
margin-top: 25px;
color: red;
}
@media print
{
td, td.active {
border: none;
}
#message {
display: none;
}
}
请参阅this fiddle了解工作示例
答案 1 :(得分:0)
我们将Crystal报表与模板一起使用。
您可能需要查看已建立的报告系统,例如:
您可以在代码中执行此操作,但每次您想要更改布局时,更容易不必编辑X和Y区域。使用Crystal,我们为报告创建数据,然后导出为pdf,可以随时编辑报告模板以添加更多字段,更改边距,调整页脚和标题等...
此处是结构化打印文档实用程序的链接。 Link可能会给你一个开始的地方。
希望这有帮助。
修改强>
创建模板并假设您创建了25个数据区域后,您可以在数据中添加一个字段来表示模板上的该区域,现在当您打印文档时,每个项目都将在您的打印输出上就位。链接到Creating a Report with a Selection Formula
答案 2 :(得分:-1)
我使用Miscrosoft Word Label Printing Utility issue中的代码创建了这个类
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
}
}
我没有发布Creditor类,但您可以轻松地重现使用的属性。
不幸的是,它编写的系统已经过时了,所以我现在无法运行代码。