Not able to draw multiple semi circles using PDPageContentStream

只愿长相守 提交于 2019-12-12 04:45:22

问题


I want to implement functionality to draw cloud on the boundary of a rectangle using pdfbox 1.8.2 c# wrapper.I am able to draw a single semi circle using the code mentioned in this link. But the problem is that, I am able to draw only a single semi circle. It doesn't work when I try to draw multiple adjacent semi circles. Below is the code that I am using.

(createSmallArc() is by Hans Muller, license: Creative Commons Attribution 3.0. Changes made: implemented original AS code into java. Algorithm is by 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:


the concatenate2CTM() method is relative to the current position and not absolute. And move your stroke() call inside or it won't be displayed in Adobe Reader (PDFBox does display it). Thus change your code like this:

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

And this is what I get:



来源:https://stackoverflow.com/questions/42648299/not-able-to-draw-multiple-semi-circles-using-pdpagecontentstream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!