印刷

2016-05-22 6 views
2

、ラベルは8に4つの* 2列にしている私は私が私がラベルわたしの名前を印刷したいプログラムに取り組んでいます印刷

を達成しようとしています剥がして他のものに貼り付けることができる1/2 * 14の紙。 。

私が持っているもの

これまでのところ、私はモーダルフォームは4 * 2の8つのボタン、紙の上のラベルの表現でポップアップ表示印刷]をクリックしたとき。

私はそれらのいずれかのボタンをクリックしたとき、私は今

が必要なもの、 私がクリックした場合ので、その場所またはハードページ上の特定の位置にコード化されたボタンIDを渡したいです行の2番目のボタン、私の印刷が2番目の行にあるようなid。基本的に、ボタンを選択すると、印刷するラベルが決まります。

はidがお送りしたいと、

x: 0 
y: 0 

第二ボタンがクリックされたとしましょう最初のボタンをクリックするidが場所で紙に印刷するように私の名前を送信したいと言うことができます(私が言えましょう)私の印刷が

x:0 
y:-5 
0で発生するようになります場所

x: 5 (five being halfway) 
y: 0 

とボタンを3で 紙に印刷する名前

+0

h Microsoft Officeの場合は、[MailingDocument](https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.mailinglabel%28v=office.15%29.aspx)をご覧ください。ここにいくつかのコードがありますhttp://stackoverflow.com/questions/18056117/miscrosoft-word-label-printing-utility-issue –

+0

@PeterBillこれでインポートできるナゲットはありますか? – Grace

+0

私は以下の答えが役立つことを願っています。 –

答えて

3

ビューで、ラベルのグリッドを表す表を作成し、ページ/ラベルのサイズに基づいて<td>要素のスタイルを設定し、最初の表のセルに印刷する内容を追加します。

jqueryを使用して、各表のセルのクリックイベントを処理し、コンテンツを適切なラベルに再配置します。

最後に、@media printスタイルを追加して、ページ上の他の要素の印刷を防止します。

あなたのビューは

<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(ラベルの3×2のグリッド用)

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を参照してくださいように見えるかもしれません作業例

+0

OPは印刷前に完全なフォームを記入したいと思うので、彼女の選択を印刷のために完全なフォームに送り返す必要があります。 –

+0

@RichBailo、いいえ「フォーム」はありません(または問題がない場合でも)。 OPがラベルに何かを印刷したいというイントラネットアプリケーションです。ラベルシートに2 x 4行のラベルがあり、最初の2つのラベルがすでに使用されている可能性があるので、OPはコンテンツをレイアウトに配置する必要があります。正しい位置に印刷されます。 –

+0

用紙上の特定の場所を参照していません。設定した4 x 2の高さが各行に異なる場合、列幅と列の間隔が異なる場合はどうなりますか。つまり、常にテンプレートを使用して塗りつぶす必要があるため、テンプレートは印刷用に設計することができます。 –

-1

私は

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 
    } 
} 

このクラスを作成するために、Miscrosoft Word Label Printing Utility issueのコードを使用し、私は債権のクラスを投稿していないが、あなたは簡単に使用されている性質を再現することができます。

残念なことに、書かれたシステムが廃止されたので、今すぐコードを実行することはできません。

+0

OPは、デスクトップアプリケーションではなく、Webアプリケーションを作成しています。これはOPの問題とは関係ありません。 –

0

テンプレートを使用したCrystalレポートを使用します。

あなたが好き確立報告システムを見て利用したい場合があります。あなたは、コードが、その中でこれを行うことができます

を報告

  • FastReport.NET
  • Crystal Reportsの
  • DevExpress社をレイアウトを変更するたびにXとYの領域を編集する必要はありません。 Crystalを使用してレポートのデータを作成し、pdfにエクスポートすると、いつでもレポートテンプレートを編集して、フィールドを追加したり、余白を変更したり、ページのフッターとヘッダーなどを調整できます。

    Structured Print Documentユーティリティへのリンク。 Linkはどこかで始めるかもしれません。

    これが役に立ちます。

    EDIT:

    は、各ドキュメントを印刷するとき、あなたのテンプレートを作成し、データのための25個の領域を作成したとしましょうたら、あなたは自分のデータにフィールドを追加することができます今、テンプレートにその領域を表し、アイテムはあなたのプリントアウトの場所に置かれます。 Creating a Report with a Selection Formula

+0

あなたの方法では、印刷する用紙のどこに正確に記述できますか?スティッキーラベルの一部が削除されているインスタンスを想像しているので、残っているラベルがもう1つしかないので、リントに正確にリダイレクトできるようにする必要があります。 – Grace

+0

はい、テンプレートにパラメータを作成し、データテンプレートに設定したパラメータにインポートされます。ここにリンクがありますhttp://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-tutorial.htm –

+0

私はクリスタルレポートの作成に精通していますが、私はこのプログラムを " 「 – Grace