Loading

iTextSharp 开发技巧

iTextSharp 开发技巧:

1、设置文件大小
方法一:创建文件 PageSize.A7.Rotate()表示A7纸横向输出     
方法二:document.SetPageSize(PageSize.A6);A6纸纵向输出
方法三:Rectangle nSize = new Rectangle(595f, 842f); Document document = new Document(nSize, 0, 0, 0, 0); margin 上下左右

2//块(Chunk)、短句(Phrase)、段落(Paragraph)、列表(List)

    ///方法一
    ///pcb.SetFontAndSize(base_stsong, 20);
    ///pcb.SetTextMatrix(document.Left + 200, document.Top - 20);  //(xPos, yPos)
    ///pcb.ShowText("AAABBBCCC中国人111222333:");
    ///方法二
    ///alignment:左、右、居中(ALIGN_CENTER, ALIGN_RIGHT or ALIGN_LEFT)
    ///text:要输出的文本
    ///x:文本输入的X坐标
    ///y:文本输入的Y坐标
    ///rotation:文本的旋转角度
    ///pcb.ShowTextAligned(Element.ALIGN_LEFT, "AAABBBCCC中国人111222333", document.Left + 200, document.Top - 20,0);
    ///pcb.ShowTextAlignedKerned(Element.ALIGN_LEFT, "AAABBBCCC中国人111222333", document.Left + 200, document.Top - 20, 0);
    pcb.EndText();
    ///方法三
    //Phrase phrase = new Phrase();
    //Chunk t = new Chunk("AAA我是一个小片段222", stsong_0);
    ////t.SetTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 0.5f, BaseColor.RED);//填充颜色
    //t.SetUnderline(0.5f, -5f);//下划线
    //t.SetSkew(0, 0);//旋转、倾斜
    ////t.SetHorizontalScaling(0.5f);//缩放
    //Chunk c = new Chunk("©", stsong_1);
    //c.SetBackground(BaseColor.BLACK, 0.1f, 0.1f, 0.1f, 0.1f);
    //c.SetTextRise(10);
    //phrase.Add(t);
    //phrase.Add(c);
    //ColumnText.ShowTextAligned(pcb, Element.ALIGN_LEFT, phrase, document.Left + 200, document.Top - 40, 0);
    ////ColumnText.ShowTextAligned(pcb, Element.ALIGN_CENTER, phrase, 400, 380, 30, PdfWriter.RUN_DIRECTION_DEFAULT, 0);

    ///方法四
    //ColumnText ct = new ColumnText(pcb);
    //ct.SetSimpleColumn(phrase, 0, 0, 200, 100, 16, Element.ALIGN_MIDDLE);
    //ct.Go();

    document.PageSize.Width;
    document.PageSize.Height;
    document.LeftMargin 
    document.RightMargin 
    float x = document.GetRight(document.RightMargin);
    float y = document.RightMargin;
    float z = document.Right;

    //PdfContentByte pcb = pdfWriter.DirectContent;
    //pcb.BeginText();
    //pcb.EndText();
    //pcb.Stroke();

    //table1.DefaultCell.Border = Rectangle.NO_BORDER;
    //pdfWriter.CropBoxSize = new Rectangle(0,0,300,300);  //PDF裁剪

    //设置表的列头
    //table1.HeaderRows = 1;
    //document.Add(table1);

    //iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(@"F:\Template\EVG.png");
    //Paragraph p1 = new Paragraph();
    //p1.Add(new Phrase("Text next to the image "));
    //p1.Add(new Chunk(image, 0, 0,true));
    //p1.Add(new Phrase(" and text after the image."));
    //document.Add(p1);

    pdfWriter.DirectContent.SetLineWidth(0.5f);   // Make a bit thicker than 1.0 default


    //Paragraph p_line = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(1F, 90F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)));
    //ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_LEFT, p_line, document.Left, positon, 0);
 
    base_stsong.GetWidthPoint(writer.PageNumber.ToString(), 8);  //获取字符串的长度

 表格直接添加行:

               PdfPCell c_shipping_marks = new PdfPCell()
                {
                    UseVariableBorders = false,
                    BorderColor = PDFSetting.border_color,
                    BorderWidth = PDFSetting.border_w,
                    VerticalAlignment = Element.ALIGN_MIDDLE,
                    HorizontalAlignment = Element.ALIGN_LEFT,
                    Padding = 3f,
                    NoWrap = false,
                };
                c_shipping_marks.AddElement(new Phrase(dr["shipping_marks"].ToString(), PDFSetting.font_normal_n));

                PdfPCell c_description_of_goods = new PdfPCell()
                {
                    UseVariableBorders = false,
                    BorderColor = PDFSetting.border_color,
                    BorderWidth = PDFSetting.border_w,
                    VerticalAlignment = Element.ALIGN_MIDDLE,
                    HorizontalAlignment = Element.ALIGN_LEFT,
                    Padding = 3f,
                    NoWrap = false,
                };
                c_description_of_goods.AddElement(new Phrase(dr["description_of_goods"].ToString(), PDFSetting.font_normal_n));

                PdfPCell c_total_quantity_pcs = new PdfPCell()
                {
                    UseVariableBorders = false,
                    BorderColor = PDFSetting.border_color,
                    BorderWidth = PDFSetting.border_w,
                    VerticalAlignment = Element.ALIGN_MIDDLE,
                    HorizontalAlignment = Element.ALIGN_LEFT,
                    Padding = 3f,
                    NoWrap = false,
                };
                c_total_quantity_pcs.AddElement(new Phrase(dr["total_quantity_pcs"].ToString(), PDFSetting.font_normal_n));

                PdfPCell c_total_g_w_kgs = new PdfPCell()
                {
                    UseVariableBorders = false,
                    BorderColor = PDFSetting.border_color,
                    BorderWidth = PDFSetting.border_w,
                    VerticalAlignment = Element.ALIGN_MIDDLE,
                    HorizontalAlignment = Element.ALIGN_LEFT,
                    Padding = 3f,
                    NoWrap = false,
                };
                c_total_g_w_kgs.AddElement(new Phrase(dr["total_g_w_kgs"].ToString(), PDFSetting.font_normal_n));

                PdfPCell c_total_packages = new PdfPCell()
                {
                    UseVariableBorders = false,
                    BorderColor = PDFSetting.border_color,
                    BorderWidth = PDFSetting.border_w,
                    VerticalAlignment = Element.ALIGN_MIDDLE,
                    HorizontalAlignment = Element.ALIGN_LEFT,
                    Padding = 3f,
                    NoWrap = false,
                };
                c_total_packages.AddElement(new Phrase(dr["total_packages"].ToString(), PDFSetting.font_normal_n));

                PdfPRow pr = new PdfPRow(new PdfPCell[] { c_shipping_marks, c_description_of_goods, c_total_quantity_pcs, c_total_g_w_kgs, c_total_packages });

                tb_content.Rows.Add(pr);

                float vv2 = tb_content.CalculateHeights();

 

posted @ 2017-10-11 16:17  Sam Xiao  阅读(148)  评论(0)    收藏  举报