2014年10月31日 星期五

javascript-checkbox 20141106


Size
style="height:18px; width:18px; float:left; overflow:hidden; margin: 0;padding: 0 ;"

html-check

s2=document.getElementById('admin').checked ? "1" :"0"


submit

<input id='testName' type='checkbox' value='Yes' name='testName'>
<input id='testNameHidden'  type='hidden' value='No' name='testName'>
Before submitting the form , disabled the hidden field based on the checked condition
if(document.getElementById("testName").checked){
  document.getElementById('testNameHidden').disabled = true;
}


value

$(this).val(this.checked ? 1 : 0);

if (this.checked) {_refreshv2('hidden_<%=tableField%>',1);} else {_refreshv2('hidden_<%=tableField%>',0);}
$('.DeliveryTo').css('display',(this.checked?'none':'inline'));
$('.DeliveryTo').css('display',(document.getElementById('this.id').checked?'none':'inline'));

2014年10月28日 星期二

itext empty line

private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
 paragraph.add(new Paragraph(" "));
}
}

2014年10月27日 星期一

javabean 20141021

e.g. ViewPDF.java

Servernet : PDFCreateAndView

import com.erp.utils.CompanyProfile;

public void processRequest(HttpServletRequest request,
HttpServletResponse response) {

CompanyProfile bean = (CompanyProfile) request.getSession().getAttribute("bean");
response.setContentType(this.CONTENT_TYPE);
String recordID=request.getParameter("recordid");
}


html window editing

// var myWindow = window.open("", "MsgWindow", "width=1200, height=400");
//myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");
//myWindow.document.write(url);

2014年10月26日 星期日

itext-total page -totalPage




allDocGenPDF.db=db;
allDocGenPDF.fileName=fileName;
allDocGenPDF.DocumentNumber=fileName; //20140410
allDocGenPDF.Document="po";
allDocGenPDF.recordID=recordID;
allDocGenPDF.companyID=companyID;

allDocGenPDF.run();


File file0 = new File("/erp/"+db+"/"+create+"/"+fileName+".pdf");  //20140920
FileInputStream fs0= new FileInputStream(file0); //20141026
byte[] byteArray0 = new byte[(int) file0.length()]; //20141026
PdfReader reader = new PdfReader(fs0); //20141026
int totalPage=reader.getNumberOfPages(); //20141026
allDocGenPDF.totalPage=totalPage; //20141026
allDocGenPDF.run();

2014年10月25日 星期六

itext-modifying

http://tutorials.jenkov.com/java-itext/modifying-pdf-documents.html

Java IText: Modifying Existing PDF Documents


By Jakob Jenkov
 Connect with me:
Rate article:
Share article:

IText can modify existing PDF files in many different ways. Here I'll just cover one of the most used modifications - stamping an existing PDF with text or images. Get the book "IText in Action" to get the full story on manipulating existing PDF documents.
If you already have a finished PDF, and just want to add a header, footer or watermark to it, IText provides the com.itextpdf.pdf.PdfStamper class.
First you read the existing document using a PdfReader, then modify it using the PdfStamper.
Here is a simple code example:
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.FileOutputStream;
import java.io.IOException;

public class PdfStamperExample {

  public static void main(String[] args) {
    try {
      PdfReader pdfReader = new PdfReader("HelloWorld.pdf");

      PdfStamper pdfStamper = new PdfStamper(pdfReader,
            new FileOutputStream("HelloWorld-Stamped.pdf"));

      Image image = Image.getInstance("watermark.png");

      for(int i=1; i<= pdfReader.getNumberOfPages(); i++){

          PdfContentByte content = pdfStamper.getUnderContent(i);

          image.setAbsolutePosition(100f, 700f);

          content.addImage(image);
      }

      pdfStamper.close();

    } catch (IOException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    }
  }
}

Adding Content to the PDF Document

To add content to the document you need to access to a PdfContentByte from thePdfStamper. You can add content either above or below the existing content in the PDF document. Here is how you obtain the PdfContentByte from the PdfStamper.
PdfContentByte underContent = pdfStamper.getUnderContent(1);

PdfContentByte overContent  = pdfStamper.getOverContent(1);
The number passed as parameter is the page number of the page to get the under or over content for.

The PdfContentByte object has methods for adding all kinds of content to a PDF including text, graphics, images etc.

itext-font

http://tutorials.jenkov.com/java-itext/font.html

Font f3 = FontFactory.getFont("MSung-Light","UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED);//chinese
Font f2 = new Font(baseFont);
//BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",false);//chinese








Java IText: Font


By Jakob Jenkov
 Connect with me:
Rate article:
Share article:

You can specify fonts for most text objects (Chunk, Phrase, Paragraph etc.) in IText. Actually, you can do a lot with fonts in IText. Too much to cover here, so I'll just cover the basics. Get the book "IText in Action" to get the full story on fonts.
To use a font you must first create the font. Then you pass it to the text object in it's constructor. Here is a simple code example:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.io.FileNotFoundException;

public class FontExample {

  public static void main(String[] args) {

    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
        new FileOutputStream("Font.pdf"));
      
          Font font1 = new Font(Font.FontFamily.HELVETICA  , 25, Font.BOLD);
          Font font2 = new Font(Font.FontFamily.COURIER    , 18,
          Font.ITALIC | Font.UNDERLINE);
          Font font3 = new Font(Font.FontFamily.TIMES_ROMAN, 27);
      
      document.open();

      document.add(new Chunk(    "This is sentence 1. ", font1));
      document.add(new Phrase(   "This is sentence 2. ", font2));
      document.add(new Paragraph("This is sentence 3. ", font3));

      document.close();

    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

  }
}
Here is what the generated document looks like:
An IText Font example
An IText Font example

itext-underline

http://tutorials.jenkov.com/java-itext/underline-strikethrough.html


Java IText: Underline + Strikethrough


By Jakob Jenkov
 Connect with me:
Rate article:
Share article:

You can add underline and strikethrough text using the Chunk class, and its setUnderline()method. You use a negative underline value to get the line lower below the text, and a positive underline value to get the line to strike through the text.
Here is a simple code example:
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class UnderlineStrikethroughExample {
  public static void main(String[] args) {

    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("UnderlineStrikethrough.pdf"));

      document.open();

      Chunk underline = new Chunk("Underline. ");
      underline.setUnderline(0.1f, -2f); //0.1 thick, -2 y-location
      document.add(underline);

      document.add(new Paragraph("   "));

      Chunk strikethrough = new Chunk("Strikethrough.");
      strikethrough.setUnderline(0.1f, 3f); //0.1 thick, 2 y-location
      document.add(strikethrough);

      document.close();

    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

  }

}
Here is what the generated document looks like:
IText Chunk's with underline and strikethrough
IText Chunk's with underline and strikethrough

itext-subscript-superscript

-http://tutorials.jenkov.com/java-itext/superscript-subscript.html


Java IText: Superscript + Subscript


By Jakob Jenkov
 Connect with me:
Rate article:
Share article:

You can write text as superscript or subscript using the Chunk class, and it's setTextRise()method. You use a positive text rise value for superscript, and a negative text rise value for subscript.
Here is a simple code example:
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class SuperSubScriptExample {
  public static void main(String[] args) {

    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("SuperSubScript.pdf"));

      document.open();

      Chunk normalText =
            new Chunk("Normal text at normal y-location. ");
      document.add(normalText);

      Chunk superScript = new Chunk("Superscript");
      superScript.setTextRise(5f);
      document.add(superScript);

      Chunk moreNormalText =
            new Chunk(". More normal y-location text. ");
      document.add(moreNormalText);

      Chunk subScript = new Chunk("Subscript");
      subScript.setTextRise(-5f);
      document.add(subScript);

      document.close();

    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

  }

}
Here is what the generated document looks like:
IText Chunk's with superscript and subscript
IText Chunk's with superscript and subscript

itext-images

-http://tutorials.jenkov.com/java-itext/image.html


You can do a lot with images in IText, including scaling, rotating, masking, absolute positioning, borders, alignment etc. I'll only go through the basics here. Get the "IText in Action" book if you want the full story on images. Here is a list of the topics covered in this text:
  1. Creating an Image
  2. Absolute Positioning
  3. Scaling
  4. Rotating

Creating an Image

The com.itextpdf.text.Image is used to add images to IText PDF documents. You can load images either from file or from a URL, like this:
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class ImageExample {
  public static void main(String[] args) {
    Document document = new Document();

    try {
        PdfWriter.getInstance(document,
                new FileOutputStream("Image.pdf"));
        document.open();

        Image image1 = Image.getInstance("watermark.png");
        document.add(image1);

        
            String imageUrl = "http://jenkov.com/images/" +
            "20081123-20081123-3E1W7902-small-portrait.jpg";

            Image image2 = Image.getInstance(new URL(imageUrl));
        document.add(image2);

        document.close();
    } catch(Exception e){
      e.printStackTrace();
    }
  }
}
Here is what the generated document looks like:
An IText Image example
An IText Image example

Absolute Positioning

You set the absolute position of an image using the setAbsolutePosition() method. Do so before adding the image to the document. This method takes two parameters: X and Y coordinate of the lower left corner of the image. Also keep in mind, that the origin coordinate system in a PDF document is the lower left corner of the document. Not the uppper left corner, like on a screen.
Here is a code example:
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.net.URL;

public class Image2Example {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("Image2.pdf"));
            document.open();

            String imageUrl = "http://jenkov.com/images/" +
                    "20081123-20081123-3E1W7902-small-portrait.jpg";

            Image image = Image.getInstance(new URL(imageUrl));
            image.setAbsolutePosition(500f, 650f);
            document.add(image);

            document.close();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}
Here is what the resulting document looks like:
An IText Image at an absolute position
An IText Image at an absolute position

Scaling

You can scale images using one of these Image methods:
scaleAbsolute()
scaleAbsoluteWidth()
scaleAbsoluteHeight()
scalePercentage()
scaleToFit()
Here is a simple example:
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.net.URL;

public class Image3Example {
  public static void main(String[] args) {
    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("Image3.pdf"));
      document.open();

      String imageUrl = "http://jenkov.com/images/" +
              "20081123-20081123-3E1W7902-small-portrait.jpg";

      Image image = Image.getInstance(new URL(imageUrl));
      image.scaleAbsolute(150f, 150f);
      document.add(image);

      Image image2 = Image.getInstance(new URL(imageUrl));
      image2.scalePercent(300f);
      document.add(image2);

      document.close();
    } catch(Exception e){
       e.printStackTrace();
    }
  }
}
Here is what the resulting document looks like:
Two IText Image's scaled
Two IText Image's scaled

Rotating

You can rotate images in IText PDF documents too, using these methods:
setRotationDegrees()
setRotation()
Here is a simple example:
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.net.URL;

public class Image4Example {
  public static void main(String[] args) {
    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("Image4.pdf"));
      document.open();

      String imageUrl = "http://jenkov.com/images/" +
              "20081123-20081123-3E1W7902-small-portrait.jpg";

      Image image = Image.getInstance(new URL(imageUrl));
      image.setRotationDegrees(45f);
      document.add(image);

      document.close();
    } catch(Exception e){
      e.printStackTrace();
    }
  }
}
Here is what the resulting document looks like:
An IText Image rotated
An IText Image rotated

itext-table

-http://tutorials.jenkov.com/java-itext/table.html
You can add tables to a PDF document using the com.itextpdf.text.PdfPTable class in IText. Tables are some of the more complex objects in IText, so this text is a bit larger than the rest of the texts in this tutorial. Here is a list of the topics covered:
  1. Creating a Table
  2. Table Width
  3. Spacing Before and After Table
  4. Column Widths
  5. Column Span
  6. Cell Text Mode and Composite Mode
  7. Default Cell Setting in Text Mode
  8. Cell Alignment
  9. Cell Indentation
  10. Cell Leading
  11. Cell Padding
  12. Cell Borders and Colors
  13. Cell Rotation
  14. Tables and Images
  15. Nested Tables

Creating a Table

When instantiating a PdfTable you must tell how many columns the table should have. You pass the number of columns as a parameter to the PdfPTable constructor.
To add cells to the table you call the addCell() method, passing PdfPCell instances, or other IText objects like Paragraph etc. Keep in mind though, that there is a difference in behaviour depending on what object you add. See the Cell Text Mode and Composite Modesection for more info.
Here is a simple code example:
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class TableExample {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                new FileOutputStream("HelloWorld-Table.pdf"));

            document.open();

            PdfPTable table = new PdfPTable(3); // 3 columns.

            PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
            PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
            PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));

            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);

            document.add(table);

            document.close();
        } catch(Exception e){

        }
    }
}
Here is what the generated document looks like:
An IText Table
An IText Table

Table Width

You can set the table width using the setWidthPercentage() metod. This sets the width of the table as a percentage of the width of the page. The default width is 80%. Here is a code example:
table.setWidthPercentage(100);

Spacing Before and After Table

You can set the spacing before and after the table like this:
table.setSpacingBefore(10f);

table.setSpacingAfter(10f);

Column Widths

You can set the column widths using the setWidths() method, like this:
float[] columnWidths = {2f, 1f, 1f};

table.setWidths(columnWidths);
The column widths in the float array are relative widths. In the example above the first column is twice the width of each of the following columns. You can use any numbers in the width array, and they will be interpreted as relative values. For instance, you could have written 100, 50, 50 if you needed a finer grained control over the column sizes.

Column Span

If you need a cell to span multiple columns you can do so using the setColspan() method, like this:
cell.setColspan(2);

Cell Text Mode and Composite Mode

Cells can be added in either text mode or composite mode.
In text mode the settings of the added element (PhraseParagraph etc.) is ignored. Only the settings of the cell is applied.
In composite mode the settings of the added element is respected. Settings like leading (line spacing), margins etc. is thus respected.
Content added to the PdfCell's constructor is considered text mode content. Content added via the PdfCell.addElement() method is considered composite mode content. Here are some examples:
PdfCell textModeCell = new PdfCell(new Paragraph("Text Mode"));

PdfCell compositeModeCell = new PdfCell();
compositeModeCell.addElement(new Paragraph("Composite Mode"));

table.addCell(new Paragraph("Text Mode"));

Default Cell Settings in Text Mode

You can set the default cell settings of new cells added, using the table.addCell() methods, like this:
PdfCell defaultCell = table.getDefaultCell();
defaultCell.setBorder(PdfCell.NO_BORDER);
//set more default settings.

//add cells with default settings:
table.addCell(new Paragraph("default text mode cell");
table.addCell(new Phrase("default text mode cell");

Cell Alignment

You can set the cell alignment using the setHorizontalAlignment() andsetVerticalAlignment(), like this:
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);

cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
In composite mode you can also set the alignment of each Paragraph individually, by setting the paragraph alignment on the Paragraph object.

Cell Indentation

You can set the indentation of cell content.
The method setIndent() sets the indentation of the first paragraph in the cell.
The method setFollowingIndent() sets the indentation of the following paragraphs in the cell.
The method setRightIndent() sets the right indentation of the cell content.

Cell Leading

You can set the leading (line spacing) of elements in a cell.
If the cell is in composite mode, just set the leading on the element added, e.g. set the leading of a Paragraph before adding it to the cell.
In text mode you can set a leading value that is used on the entire cell content. To so do, use thesetLeading() method. This method takes two parameters. A fixed leading and a leading calculated on font height. Here are two examples:
cell.setLeading(15f, 0f);

cell.setLeading(0f, 1.5f);
The first method call sets the leading to 15 points + 0 x font height.
The second method call sets the leading to 0 points + 1.5 x font height.

Cell Padding

You can set the padding of a cell (distance between cell edge and content) using these methods:
cell.setPadding(5);

cell.setPaddingLeft(8);
cell.setPaddingRight(8);
cell.setPaddingTop(8);
cell.setPaddingBottom(8);

Cell Borders and Colors

You can set the cell border and color using these methods:
cell.setBackgroundColor(BaseColor.YELLOW);   //sets BG color to yellow.

cell.setBorder(Rectangle.NO_BORDER);   // removes border

cell.setBorderWidth      (3f);         // sets border width to 3 units
cell.setBorderWidthLeft  (1f);
cell.setBorderWidthRight (1f);
cell.setBorderWidthTop   (1f);
cell.setBorderWidthBottom(1f);

cell.setBorderColor      (BaseColor.BLUE);  // sets blue border
cell.setBorderColorLeft  (BaseColor.GREEN);
cell.setBorderColorRight (BaseColor.GREEN);
cell.setBorderColorTop   (BaseColor.GREEN);
cell.setBorderColorBottom(BaseColor.GREEN);
To avoid having the cell border and the content overlap, if you are having thick cell borders, call the setUserBorderPadding(true), like this:
cell.setUserBorderPadding(true);

Cell Rotation

You can set the rotation of the cell content using the setRotation() method, like this:
cell.setRotation(90);

Tables and Images

You can add images to a table cell, and have either the cell fit the size of the image, or the image fit the size of the cell. You do so by passing the image to the PdfPCell constructor, along with a boolean saying whether the image should fit the cell (true), or the cell should fit the image (false). Here is how:
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.net.URL;

public class Table3Example {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                new FileOutputStream("Table3.pdf"));
            document.open();

            PdfPTable table = new PdfPTable(2); // 3 columns.

            Image image = Image.getInstance("jakob-jenkov.jpg");
            PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
            PdfPCell cell2 = new PdfPCell(image, false);

            table.addCell(cell1);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell(image, true);
            PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));

            table.addCell(cell3);
            table.addCell(cell4);


            document.add(table);

            document.close();
        } catch(Exception e){

        }
    }
}
Here is what the generated document looks like:
An IText Table with Image
An IText Table with Image

Nested Tables

You can add a PdfTable as content inside a PdfCell, thus nesting tables within tables. Here is an example:
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class Table2Example {
  public static void main(String[] args) {
    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
          new FileOutputStream("Table2.pdf"));

      document.open();

      PdfPTable table = new PdfPTable(3); // 3 columns.

      PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
      PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
      PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));

      PdfPTable nestedTable = new PdfPTable(2);
      nestedTable.addCell(new Paragraph("Nested Cell 1"));
      nestedTable.addCell(new Paragraph("Nested Cell 2"));

      cell3.addElement(nestedTable);

      table.addCell(cell1);
      table.addCell(cell2);
      table.addCell(cell3);

      document.add(table);

      document.close();

    } catch(Exception e){
      e.printStackTrace();
    }
  }
}
Here is what the generated document looks like:
An IText nested Table
An IText nested Table