我想使用pdfbox 1.8.2 c#wrapper实现在矩形边界上绘制云的功能。我能够使用此link中提到的代码绘制单个半圆。但是问题是,我只能画一个半圆。当我尝试绘制多个相邻的半圆时,它不起作用。下面是我正在使用的代码。

(createSmallArc()由Hans Mullerlicense: Creative Commons Attribution 3.0进行。所做的更改:将原始AS代码实现为Java。算法由Aleksas Riškus进行。)

public void addCloud(PDRectangle rect,PDDocument doc)
            {
                PDGamma yellow = new PDGamma();
                yellow.setR(255);
                yellow.setG(255);
                yellow.setB(0);
                PDPage page = (PDPage)doc.getDocumentCatalog().getAllPages().get(pageNum);
                float width = 215;
                float height = 156;
                int noXSemiCircles = 21;
                int noYSemiCircles = 15;
                float leftX = 203;
                float bottomY = 424;
                int index = 0;
                PDPageContentStream cs = new PDPageContentStream(doc, page,true,false);
                Matrix mt = Matrix.getTranslatingInstance(leftX + (index * 10), bottomY);
                AffineTransform at = mt.createAffineTransform();
                cs.concatenate2CTM(at);
                cs.setStrokingColor(255, 0, 0);
                while (index<noXSemiCircles)
                {
                    cs.moveTo(leftX + (index * 10), bottomY);
                    DrawSlice(cs, 5, 180,270, true);
                    DrawSlice(cs, 5, 270, 360, false);
                    index++;
                }
                cs.stroke();
                cs.close();
                doc.save(System.IO.Path.Combine(FilePath));
                doc.close();
            }
             private void DrawSlice(PDPageContentStream cs, float rad, float startDeg, float endDeg,bool move)
            {
                try
                {
                    List<float> smallArc = CreateSmallArc(rad, ConvertDegreesToRadians(startDeg), ConvertDegreesToRadians(endDeg));
                    if (move)
                    {
                        cs.moveTo(smallArc[0], smallArc[1]);
                    }
                    cs.addBezier312(smallArc[2], smallArc[3], smallArc[4], smallArc[5], smallArc[6], smallArc[7]);
                }
                catch (Exception ex)
                {

                }
            }

最佳答案

concatenate2CTM()方法相对于当前位置而不是绝对位置。然后将您的stroke()调用移入其中,否则它将不会显示在Adobe Reader中(PDFBox会显示它)。因此,像这样更改代码:

    while (index < noXSemiCircles)
    {
        cs.saveGraphicsState();
        Matrix mt = Matrix.getTranslatingInstance(leftX + (index * 10), bottomY);
        AffineTransform at = mt.createAffineTransform();
        cs.concatenate2CTM(at);
        DrawSlice(cs, 5, 180, 270, true);
        DrawSlice(cs, 5, 270, 360, true);
        cs.stroke();
        cs.restoreGraphicsState();
        index++;
    }


这就是我得到的:

java - 无法使用PDPageContentStream绘制多个半圆-LMLPHP

10-05 22:24