代码之家  ›  专栏  ›  技术社区  ›  Milhous

使用IText在PDF文档中绘制矩形

  •  6
  • Milhous  · 技术社区  · 16 年前

    在IText中有没有在PDF文档中绘制矩形的方法?

    5 回复  |  直到 8 年前
        1
  •  11
  •   khr055 Eduardo    13 年前

    这是解决方案。谢谢Dylan McClong。

    PdfWriter writer = ...;
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    cb.setColorStroke(Color.black);
    cb.rectangle(x,y,x1,y1);
    cb.stroke();
    cb.restoreState();
    
        2
  •  3
  •   Mayo    16 年前

    在.NET版本中,我只是创建一个带有边框的表。我知道它不是Java,但下面的代码可能会对你有所帮助。

    iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER, 20, 20, 20, 20);
    PdfPTable table;
    PdfPCell cell;
    
    // single element w/ border
    table = new PdfPTable(1);
    cell = new PdfPCell(new Phrase("BOLD WORDS", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11, Font.BOLD)));
    cell.BorderWidth = 2;
    cell.Padding = 5;
    cell.PaddingTop = 3;
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);
    table.SetWidthPercentage(new float[1] { 598f }, PageSize.LETTER);
    table.HorizontalAlignment = Element.ALIGN_CENTER;
    document.Add(table);
    
        3
  •  3
  •   Jeff S    14 年前
        4
  •  1
  •   sreeprasad    14 年前
    public static void drawRectangle(PdfContentByte content, float width, float height) {
        content.saveState();
        PdfGState state = new PdfGState();
        state.setFillOpacity(0.6f);
        content.setGState(state);
        content.setRGBColorFill(0xFF, 0xFF, 0xFF);
        content.setLineWidth(3);
        content.rectangle(0, 0, width, height);
        content.fillStroke();
        content.restoreState();
    }
    

    来自IText的API

        5
  •  0
  •   abh22ishek    10 年前
       private static void rect(PdfWriter writer) {
    
        PdfContentByte cb = writer.getDirectContent();
                try {
                    cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24);
                    cb.rectangle(140f,140f,280f,420f);
                    cb.stroke();
                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }
    
    推荐文章