无法使用PDPageContentStream绘制多个半圆

时间:2017-03-07 12:26:37

标签: java c# pdfbox

我想使用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)
                {

                }
            }

1 个答案:

答案 0 :(得分:1)

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

这就是我得到的:

semicircles

相关问题