• <menu id="w2i4a"></menu>
  • logo Aspose.PDF for .NET開發(fā)者使用教程

    文檔首頁>>Aspose.PDF for .NET開發(fā)者使用教程>>PDF管理控件Aspose.PDF for .Net使用教程(四十三):圖形的使用

    PDF管理控件Aspose.PDF for .Net使用教程(四十三):圖形的使用


    Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴展的安全控制和自定義字體處理。

    在接下來的系列教程中,將為開發(fā)者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉(zhuǎn)換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何使用圖形,包括:

    • 創(chuàng)建填充的矩形對象
    • 在圖形對象中添加文本
    • 將線對象添加到PDF
    • 在頁面上畫線
    • 使用Alpha顏色通道創(chuàng)建矩形
    • 添加具有透明顏色的圖形
    • 控制矩形的Z順序
    • 添加帶有漸變填充的圖形

    >>Aspose.PDF for .NET更新至最新版v20.7,歡迎下載體驗。


    對于使用Adobe Acrobat Writer或其他PDF處理應(yīng)用程序的開發(fā)人員,將圖形添加到PDF文檔是一項非常常見的任務(wù)。PDF應(yīng)用程序中可以使用多種圖形。

    Aspose.PDF還支持將圖形添加到PDF文檔。為此,Graph提供了該類。Graph是段落級別的元素,可以Paragraphs在Page實例中添加到集合中。

    創(chuàng)建填充的矩形對象

    用于.NET的Aspose.PDF支持將圖形對象(例如圖形,線,矩形等)添加到PDF文檔的功能。它還提供了用某種顏色填充矩形對象的功能。

    下面的代碼片段顯示了如何添加一個Rectangle充滿顏色的對象。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();
    
    // Create Document instance
    Document doc = new Document();
    // Add page to pages collection of PDF file
    Page page = doc.Pages.Add();
    // Create Graph instance
    Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(100, 400);
    // Add graph object to paragraphs collection of page instance
    page.Paragraphs.Add(graph);
    // Create Rectangle instance
    Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 120);
    // Specify fill color for Graph object
    rect.GraphInfo.FillColor = Aspose.Pdf.Color.Red;
    // Add rectangle object to shapes collection of Graph object
    graph.Shapes.Add(rect);
    dataDir = dataDir + "CreateFilledRectangle_out.pdf";
    // Save PDF file
    doc.Save(dataDir);

    在圖形對象中添加文本

    Aspose.PDF支持在Graph對象內(nèi)添加文本。圖形對象的文本屬性提供了設(shè)置圖形對象文本的選項。以下代碼段顯示了如何在Rectangle對象內(nèi)添加文本。

    // Open document
    string outFile = "Graph.pdf";
    Aspose.PDF.Document pdfDoc = new Aspose.PDF.Document();
    Aspose.PDF.Page pdfPage = pdfDoc.Pages.Add();
    Aspose.PDF.Drawing.Graph graph = new Aspose.PDF.Drawing.Graph(500, 100);
    pdfPage.Paragraphs.Add(graph);
     
    //Add rectangle with text
    Aspose.PDF.Drawing.Rectangle rect = new Aspose.PDF.Drawing.Rectangle(0, 30, 50, 40);
    rect.GraphInfo.LineWidth = 1f;
    rect.GraphInfo.Color = Aspose.PDF.Color.Black;
    rect.Text = new TextFragment("Rectangle");
    graph.Shapes.Add(rect);
    pdfDoc.Save(outFile);

    將線對象添加到PDF

    Aspose.PDF支持利用杠桿來添加Line對象,還可以在其中指定虛線元素,顏色和Line元素的其他格式。下面的代碼片段顯示了如何添加一個Rectangle充滿顏色的對象。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();
    
    // Create Document instance
    Document doc = new Document();
    // Add page to pages collection of PDF file
    Page page = doc.Pages.Add();
    // Create Graph instance
    Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(100, 400);
    // Add graph object to paragraphs collection of page instance
    page.Paragraphs.Add(graph);
    // Create Rectangle instance
    Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { 100, 100, 200, 100 });
    // Specify fill color for Graph object
    line.GraphInfo.DashArray = new int[] { 0, 1, 0 };
    line.GraphInfo.DashPhase = 1;
    // Add rectangle object to shapes collection of Graph object
    graph.Shapes.Add(line);
    dataDir = dataDir + "AddLineObject_out.pdf";
    // Save PDF file
    doc.Save(dataDir);

    在頁面上畫線

    Aspose.PDF支持線對象繪制從左下角到右上角以及從左上角到右下角的十字。請仔細閱讀以下代碼片段以實現(xiàn)此要求。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();
    
    // Create Document instance
    Document pDoc = new Document();
    // Add page to pages collection of PDF document
    Page pg = pDoc.Pages.Add();
    // Set page margin on all sides as 0
    pg.PageInfo.Margin.Left = pg.PageInfo.Margin.Right = pg.PageInfo.Margin.Bottom = pg.PageInfo.Margin.Top = 0;
    // Create Graph object with Width and Height equal to page dimensions
    Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)pg.PageInfo.Width , (float)pg.PageInfo.Height);
    // Create first line object starting from Lower-Left to Top-Right corner of page
    Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { (float)pg.Rect.LLX, 0, (float)pg.PageInfo.Width, (float)pg.Rect.URY });
    // Add line to shapes collection of Graph object
    graph.Shapes.Add(line);
    // Draw line from Top-Left corner of page to Bottom-Right corner of page
    Aspose.Pdf.Drawing.Line line2 = new Aspose.Pdf.Drawing.Line(new float[] { 0, (float)pg.Rect.URY, (float)pg.PageInfo.Width, (float)pg.Rect.LLX });
    // Add line to shapes collection of Graph object
    graph.Shapes.Add(line2);
    // Add Graph object to paragraphs collection of page
    pg.Paragraphs.Add(graph);
    
    dataDir = dataDir + "DrawingLine_out.pdf";
    // Save PDF file
    pDoc.Save(dataDir);

    使用Alpha顏色通道創(chuàng)建矩形

    Aspose.PDF支持用某種顏色填充矩形對象。矩形對象也可以具有Alpha顏色通道以提供透明外觀。以下代碼段顯示了如何添加具有Alpha顏色通道的Rectangle對象。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();
    
    // Instantiate Document instance
    Document doc = new Document();
    // Add page to pages collection of PDF file
    Aspose.Pdf.Page page = doc.Pages.Add();
    // Create Graph instance
    Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400);
    // Create rectangle object with specific dimensions
    Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 100);
    // Set graph fill color from System.Drawing.Color structure from a 32-bit ARGB value
    rect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(12957183)));
    // Add rectangle object to shapes collection of Graph instance
    canvas.Shapes.Add(rect);
    
    // Create second rectangle object
    Aspose.Pdf.Drawing.Rectangle rect1 = new Aspose.Pdf.Drawing.Rectangle(200, 150, 200, 100);
    rect1.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(16118015)));
    canvas.Shapes.Add(rect1);
    // Add graph instance to paragraph collection of page object
    page.Paragraphs.Add(canvas);
    
    dataDir = dataDir + "CreateRectangleWithAlphaColor_out.pdf";
    // Save PDF file
    doc.Save(dataDir);

    使用Alpha顏色通道創(chuàng)建矩形

    創(chuàng)建矩形,圓形,Eclipse等圖形對象時,我們提供邊框的顏色信息以及填充顏色信息。為了具有透明的填充效果,可以使用Aspose.PDF.Color對象的FromArgb(..)方法。請查看以下代碼片段,該代碼片段演示了使用透明顏色填充矩形對象的功能。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();
    
    int alpha = 10;
    int green = 0;
    int red = 100;
    int blue = 0;
    // Create Color object using Alpha RGB 
    Aspose.Pdf.Color alphaColor = Aspose.Pdf.Color.FromArgb(alpha, red, green, blue); // Provide alpha channel
    // Instantiate Document object
    Document document = new Document();
    // Add page to pages collection of PDF file
    Page page = document.Pages.Add();
    // Create Graph object with certain dimensions
    Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(300, 400);
    // Set border for Drawing object
    graph.Border = (new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Black));
    // Add graph object to paragraphs collection of Page instance
    page.Paragraphs.Add(graph);
    // Create Rectangle object with certain dimensions
    Aspose.Pdf.Drawing.Rectangle rectangle = new Aspose.Pdf.Drawing.Rectangle(0, 0, 100, 50);
    // Create graphInfo object for Rectangle instance
    Aspose.Pdf.GraphInfo graphInfo = rectangle.GraphInfo;
    // Set color information for GraphInfo instance
    graphInfo.Color = (Aspose.Pdf.Color.Red);
    // Set fill color for GraphInfo
    graphInfo.FillColor = (alphaColor);
    // Add rectangle shape to shapes collection of graph object
    graph.Shapes.Add(rectangle);
    dataDir = dataDir + "AddDrawing_out.pdf";
    // Save PDF file
    document.Save(dataDir);

    控制矩形的Z順序

    在PDF文件中添加同一對象的多個實例時,我們可以通過指定Z順序來控制它們的呈現(xiàn)。當我們需要在彼此上方渲染對象時,也可以使用Z順序。下面的代碼片段顯示了將Rectangle對象彼此呈現(xiàn)的步驟。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();
    
    // Instantiate Document class object
    Document doc1 = new Document();
    /// Add page to pages collection of PDF file
    Aspose.Pdf.Page page1 = doc1.Pages.Add();
    // Set size of PDF page
    page1.SetPageSize(375, 300);
    // Set left margin for page object as 0
    page1.PageInfo.Margin.Left = 0;
    // Set top margin of page object as 0
    page1.PageInfo.Margin.Top = 0;
    // Create a new rectangle with Color as Red, Z-Order as 0 and certain dimensions
    AddRectangle(page1, 50, 40, 60, 40, Aspose.Pdf.Color.Red, 2);
    // Create a new rectangle with Color as Blue, Z-Order as 0 and certain dimensions
    AddRectangle(page1, 20, 20, 30, 30, Aspose.Pdf.Color.Blue, 1);
    // Create a new rectangle with Color as Green, Z-Order as 0 and certain dimensions
    AddRectangle(page1, 40, 40, 60, 30, Aspose.Pdf.Color.Green, 0);
    dataDir = dataDir + "ControlRectangleZOrder_out.pdf";
    // Save resultant PDF file
    doc1.Save(dataDir);


    private static void AddRectangle(Aspose.Pdf.Page page, float x, float y, float width, float height, Aspose.Pdf.Color color, int zindex)
    {
        // Create graph object with dimensions same as specified for Rectangle object
        Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph(width, height);
        // Can we change the position of graph instance
        graph.IsChangePosition = false;
        // Set Left coordinate position for Graph instance
        graph.Left = x;
        // Set Top coordinate position for Graph object
        graph.Top = y;
        // Add a rectangle inside the "graph"
        Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(0, 0, width, height);
        // Set rectangle fill color
        rect.GraphInfo.FillColor = color;
        // Color of graph object
        rect.GraphInfo.Color = color;
        // Add rectangle to shapes collection of graph instance
        graph.Shapes.Add(rect);
        // Set Z-Index for rectangle object
        graph.ZIndex = zindex;
        // Add graph to paragraphs collection of page object
        page.Paragraphs.Add(graph);
    }

    添加帶有漸變填充的圖形

    Aspose.PDF支持創(chuàng)建純PDF文檔的功能,該文檔具有從一種專色/印刷色到另一種專色/印刷色的單一漸變。在下面的代碼示例中對此進行了說明。

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_AsposePdf_Graphs();
    
    // Instantiate Document class object
    Document doc1 = new Document();
    /// Add page to pages collection of PDF file
    Aspose.Pdf.Page page1 = doc1.Pages.Add();
    // Set size of PDF page
    page1.SetPageSize(375, 300);
    // Set left margin for page object as 0
    page1.PageInfo.Margin.Left = 0;
    // Set top margin of page object as 0
    page1.PageInfo.Margin.Top = 0;
    // Create a new rectangle with Color as Red, Z-Order as 0 and certain dimensions
    AddRectangle(page1, 50, 40, 60, 40, Aspose.Pdf.Color.Red, 2);
    // Create a new rectangle with Color as Blue, Z-Order as 0 and certain dimensions
    AddRectangle(page1, 20, 20, 30, 30, Aspose.Pdf.Color.Blue, 1);
    // Create a new rectangle with Color as Green, Z-Order as 0 and certain dimensions
    AddRectangle(page1, 40, 40, 60, 30, Aspose.Pdf.Color.Green, 0);
    dataDir = dataDir + "ControlRectangleZOrder_out.pdf";
    // Save resultant PDF file
    doc1.Save(dataDir);


    還想要更多嗎?您可以點擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。


    掃碼咨詢


    添加微信 立即咨詢

    電話咨詢

    客服熱線
    023-68661681

    TOP
    三级成人熟女影院,欧美午夜成人精品视频,亚洲国产成人乱色在线观看,色中色成人论坛 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();