Created
August 27, 2018 16:47
-
-
Save dirq/4cc0768cc28d7cb3a47c485b7e6f91ac to your computer and use it in GitHub Desktop.
Adding printer marks with Aspose.Pdf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
//tested using Aspose.Pdf 18.8.0 | |
using Aspose.Pdf; | |
using Aspose.Pdf.Drawing; | |
namespace Dirq.DocBuilder.Rendering | |
{ | |
public static class PrinterMarks | |
{ | |
private enum Direction | |
{ | |
Up, | |
Right, | |
Down, | |
Left | |
} | |
/// <summary> | |
/// Adds a Graph to each page in the document. Then creates lines on each point of the TrimBox for a printer house cut lines. | |
/// </summary> | |
/// <param name="doc"></param> | |
public static void AddPrinterMarks(ref Document doc) | |
{ | |
foreach (Page page in doc.Pages) | |
{ | |
var graph = new Graph((float)page.Rect.Width, (float)page.Rect.Height) | |
{ | |
Margin = new MarginInfo(0, 0, 0, 0), | |
ZIndex = 5555, | |
Width = (float)page.Rect.Width, | |
Height = (float)page.Rect.Height, | |
Left = 0, | |
Top = 0 | |
}; | |
float gap = (float)Math.Max(page.TrimBox.LLX - page.BleedBox.LLX, page.TrimBox.LLY - page.BleedBox.LLY); | |
page.Paragraphs.Add(graph); | |
//upper left | |
graph.Shapes.Add(GetLine((float)page.TrimBox.LLX, (float)page.TrimBox.URY, Direction.Up, Color.Blue)); | |
graph.Shapes.Add(GetLine((float)page.TrimBox.LLX, (float)page.TrimBox.URY, Direction.Left, Color.Blue)); | |
//upper right | |
graph.Shapes.Add(GetLine((float)page.TrimBox.URX, (float)page.TrimBox.URY, Direction.Up, Color.Blue)); | |
graph.Shapes.Add(GetLine((float)page.TrimBox.URX, (float)page.TrimBox.URY, Direction.Right, Color.Blue)); | |
//lower right | |
graph.Shapes.Add(GetLine((float)page.TrimBox.URX, (float)page.TrimBox.LLY, Direction.Down, Color.Blue)); | |
graph.Shapes.Add(GetLine((float)page.TrimBox.URX, (float)page.TrimBox.LLY, Direction.Right, Color.Blue)); | |
//lower left | |
graph.Shapes.Add(GetLine((float)page.TrimBox.LLX, (float)page.TrimBox.LLY, Direction.Down, Color.Blue)); | |
graph.Shapes.Add(GetLine((float)page.TrimBox.LLX, (float)page.TrimBox.LLY, Direction.Left, Color.Blue)); | |
} | |
} | |
private static Line GetLine(float startX, float startY, Direction direction, Color color = null) | |
{ | |
//we don't want the points actually touching the trim mark | |
//cause printer cuts are not going to be exact and we don't want anything showing | |
const float gap = 9; | |
//length of the line. It'll probably go off the page. | |
const float length = 36; | |
float endX = startX; | |
float endY = startY; | |
if (color == null) | |
{ | |
color = Color.Gray; | |
} | |
switch (direction) | |
{ | |
case Direction.Up: | |
startY += gap; | |
endY += length; | |
break; | |
case Direction.Right: | |
startX += gap; | |
endX += length; | |
break; | |
case Direction.Down: | |
startY -= gap; | |
endY -= length; | |
break; | |
case Direction.Left: | |
startX -= gap; | |
endX -= length; | |
break; | |
} | |
//lower left to upper right. wtf. | |
var line = new Line(new[] {startX, startY, endX, endY}); | |
line.GraphInfo.Color = color; | |
return line; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds a Graph to each page in a Aspose.Pdf Document with trim marks.
Usage: