Skip to content

Instantly share code, notes, and snippets.

@dirq
Created August 27, 2018 16:47
Show Gist options
  • Save dirq/4cc0768cc28d7cb3a47c485b7e6f91ac to your computer and use it in GitHub Desktop.
Save dirq/4cc0768cc28d7cb3a47c485b7e6f91ac to your computer and use it in GitHub Desktop.
Adding printer marks with Aspose.Pdf
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;
}
}
}
@dirq
Copy link
Author

dirq commented Aug 27, 2018

Adds a Graph to each page in a Aspose.Pdf Document with trim marks.

Usage:

        [Test]
        public void PrinterMarksManualTest()
        {
            string methodName = MethodBase.GetCurrentMethod().Name;
            string path = DocConfig.GetAssetOutputPath(methodName + ".pdf");
            int pages = 4;

            var doc = new Document();

            for (int i = 1; i <= pages; i++)
            {
                Page page = doc.Pages.Add();

                page.PageInfo.Width = 678;
                page.PageInfo.Height = 858;
                page.PageInfo.Margin = new MarginInfo(0,0,0,0);
                page.ArtBox = new Rectangle(llx: 0, lly: 0, urx: 678, ury: 858); //678x858
                page.CropBox =  new Rectangle(llx: 0, lly: 0, urx: 678, ury: 858); //678x858
                page.MediaBox = new Rectangle(llx: 0, lly: 0, urx: 678, ury: 858); //678x858
                page.Rect = new Rectangle(llx: 0, lly: 0, urx: 678, ury: 858); //678x858

                page.TrimBox = new Rectangle(llx: 33, lly: 33, urx: 645, ury: 825); //612x792

                page.BleedBox = new Rectangle(llx: 24, lly: 24, urx: 654, ury: 834); //630x810

                page.Paragraphs.Add(new TextFragment($"{methodName} created on {DateTime.Now}.  Page {i}"));
            }

            doc.ProcessParagraphs();

            PrinterMarks.AddPrinterMarks(ref doc);

            doc.Save(path, SaveFormat.Pdf);
            doc.Dispose();

            FileAssert.Exists(path);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment