---
title: "Working with PDF"
canonical: https://www.graphicsmill.com/docs/gm/working-with-pdf.htm
product: "Graphics Mill"
---

# Working with PDF
 
PDF was developed by Adobe Systems, Inc. for secure and reliable distribution of electronic documents independent of software, hardware, and operating system. In general, a PDF file includes a complete description of a fixed-layout flat document, such as the text, fonts, graphics, and other information needed to display it.
 
Graphics Mill supports reading and writing to PDF files. A PDF document can be either a raster or vector file. This topic demonstrates how to create PDF documents and read their content in Graphics Mill.
 
## Using Bitmap.Save
  
In the simplest case, you can create a single-page PDF document using the [Bitmap.Save](https://www.graphicsmill.com/docs/gm/O_T_Aurigma_GraphicsMill_Bitmap_Save.htm.md) method. You just create a bitmap containing the desired data and save it in the PDF file format. Additionally, you can provide PDF compression parameters via the [PdfSettings](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfSettings.htm.md) class.
 
The code below creates a PDF file with a single page containing a raster text string.
 
```csharp
using (var bitmap = new Bitmap(640, 480, PixelFormat.Format24bppRgb, RgbColor.White))
{
    using (var graphics = bitmap.GetGraphics())
    {
        var brush = new SolidBrush(RgbColor.Black);

        graphics.DrawText(new PlainText("Graphics Mill", graphics.CreateFont("Times New Roman", 20), brush, 220, 200));

        bitmap.Save(@"Images\Output\out.pdf", new PdfSettings(CompressionType.Jpeg, 90));
    }
}
```
 
If you open the resulting PDF file, you will note that the text quality degrades while zooming in/out and selecting a text string does not work. The reason is that the text is stored in a raster structure. Therefore, if you want to draw high-quality vector text and graphics or create multipage documents, you should use the **PdfWriter** class.
 
## Using PdfWriter
  
The [PdfWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfWriter.htm.md) class introduces a more flexible way to create PDF documents. Using this class, you can create PDF documents containing raster images and vector shapes. You can also draw vector text that has advantages compared to raster text - the text is scaled without distortion.
 
**PdfWriter** allows for creating multipage PDF documents. It provides the [AddPage](https://www.graphicsmill.com/docs/gm/O_T_Aurigma_GraphicsMill_Codecs_PdfWriter_AddPage.htm.md) method that appends a new blank page to the document. For each page, you can create [Graphics](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_Graphics.htm.md) via the [GetGraphics()](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_GetGraphics.htm.md) method, which is necessary for drawing on a PDF. Note that you can draw only on the last created page. To learn in detail how to draw **Graphics**, read the [Drawing](https://www.graphicsmill.com/docs/gm/advanced-drawing.htm.md) section. The following snippet demonstrates how to create a single-page PDF document with the specified size, and how to get **Graphics** from it:
 
```csharp
using (var pdfWriter = new PdfWriter(@"Images\Output\out.pdf"))
{
    pdfWriter.AddPage(500, 500);
    using (var graphics = pdfWriter.GetGraphics())
    {
        //...
    }
}
```
 
**PdfWriter** provides a number of overloaded **AddPage** methods allowing you to specify the DPI of a new page, its background color, color profile, or boundaries. Let us discuss how to specify some of these settings in more detail.
 
### Page Boundaries
 
Graphics Mill provides the following boundaries for PDF documents:
 
- **Crop Box** is a rectangle defining the region surrounding the content which is to be clipped when the PDF page is displayed or printed. The Crop Box specifies the visible part of the content.
- **Bleed Box** is a rectangle defining the region to which the contents of the page should be clipped when output in a production environment. By default, the Bleed Box equals the Crop Box.
- **Trim Box** is the dimensions of the fulfilled page. In contrast with the Crop Box, the Trim Box is important because it defines the actual page size used for positioning pages on a press sheet.

The following snippet demonstrates how to add a new page to the PDF document with the specified DPI, background color, crop box, trim box, and bleed box:
 
```csharp
using (var pdfWriter = new PdfWriter(@"Images\Output\out.pdf"))
{
    var dpi = 300f;

    var pdfPage = new PdfPage()
    {
        Width = 500,
        Height = 500,
        DpiX = dpi,
        DpiY = dpi,
        CropBox = new System.Drawing.RectangleF(10, 10, 480, 480),
        BleedBox = new System.Drawing.RectangleF(30, 30, 380, 380),
        TrimBox = new System.Drawing.RectangleF(20, 20, 400, 400),
    };

    pdfWriter.AddPage(pdfPage);

    using (var graphics = pdfWriter.GetGraphics())
    {
        //...
    }
}
```
 
### Color Profile
 
A PDF document does not have a single color space for its pages. All graphic elements drawn on a PDF page have their own color spaces. For instance, you can draw a CMYK line and RGB bitmap on the same page and both of these elements will be displayed correctly according to their color spaces. However, you can specify a color profile that defines color characteristics and the color gamut of the resulting PDF page. If you are not familiar with concepts such as color management and color profiles, read the [Color Management Basics](https://www.graphicsmill.com/docs/gm/color-management-basics.htm.md) article.
 
By default, the PDF page does not have a color profile, thus all graphic elements are drawn on the page without managing color. If the page was created with a profile, color management converts the colors of drawn graphics according to the specified profile. The following snippet illustrates how to add a new page to the PDF document with a CMYK profile:
 
```csharp
using (var pdfWriter = new PdfWriter(@"Images\Output\out.pdf"))
{
    pdfWriter.AddPage(500, 500, new ColorProfile(@"_Input\ColorProfiles\CoatedFOGRA27.icc"));
    using (var graphics = pdfWriter.GetGraphics())
    {
        //...
    }
}
```
 
### Compression Type
 
The [Compression](https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfWriter_Compression.htm.md) property defines the compression of a PDF file. Its default value is [Zip](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_CompressionType.htm.md). It works well for text PDFs, but for PDFs containing images, **Jpeg** can be significantly more effective. Also, you can set the [Quality](https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfWriter_Quality.htm.md) of JPEG compression.
 
You can set compression type to **Jpeg** via **PdfWriter.Compression**:
 
```csharp
PdfWriter pdfWriter = new PdfWriter(@"Images\Output\out.pdf");
pdfWriter.Compression = CompressionType.Jpeg;
pdfWriter.Quality = 70;
```
 
or via [PdfSettings.Compression](https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfSettings_Compression.htm.md):
 
```csharp
PdfSettings pdfSettings = new PdfSettings();
pdfSettings.Compression = CompressionType.Jpeg;
pdfSettings.Quality = 70;
```
 
### Using PdfPage
 
The [PdfPage](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfPage.htm.md) class describes a page of a PDF document. It sets the size, the resolution, the background color, the color profile, and the PDF boxes of the page. To add a page, create a **PdfPage** object, set its properties, and pass it to the [PdfWriter.AddPage(PdfPage)](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_AddPage_1_8a7e379c.htm.md) method of **PdfWriter**.
 
### Working with Device Independent Coordinates
 
When creating a PDF document, you may need to work with device independent coordinates (for example, inches) instead of pixels. However, the **AddPage** method only allows for specifying the size of a new page in pixels. In this case, you can use the [UnitConverter.ConvertUnitsToPixels(Single, Single, Unit)](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_UnitConverter_ConvertUnitsToPixels_3_1df1b0a2.htm.md) method, which converts inches to pixels according to the given DPI. Assume that we need to create a 3.5"x2.5" PDF with 300 DPI. Here is the snippet showing how to do this:
 
```csharp
var dpi = 300f;
using (var pdfWriter = new PdfWriter(@"Images\Output\out.pdf"))
{
    pdfWriter.AddPage(UnitConverter.ConvertUnitsToPixels(dpi, 3.5f, Unit.Inch),
        UnitConverter.ConvertUnitsToPixels(dpi, 2.0f, Unit.Inch), dpi, dpi);
    using (var graphics = pdfWriter.GetGraphics())
    {
        //...
    }
}
```
 
### Creating Multipage PDF
 
Now, let us discuss how to create a multipage PDF document. The following code snippet creates a PDF business card with two pages inside. The business card contains a raster logo, primitive vector graphics, and text strings.
 
```csharp
using (var writer = new PdfWriter(@"Images\Output\out.pdf"))
{
    writer.AddPage(350, 200);

    using (var page1 = writer.GetGraphics())
    {
        page1.DrawImage(new Bitmap(@"Images\logo.png"), 22, 8);
        
        var pen = new Pen(RgbColor.Gray, 1);
        page1.DrawRectangle(pen, new System.Drawing.RectangleF(324, 84, 26, 58));
        page1.DrawLine(pen, 24, 138, 174, 138);

        var font = page1.CreateFont("Verdana", "bold", 28);
        var brush = new SolidBrush(RgbColor.Gray);
        var point = new System.Drawing.PointF(115, 15);
        var plainText = new PlainText("Company", font, brush, point);
        page1.DrawText(plainText);

        font = page1.CreateFont("Verdana", 12);
        point = new System.Drawing.PointF(14, 116);
        plainText = new PlainText("Manager", font, brush, point);
        page1.DrawText(plainText);

        var boundedText = new BoundedText("Phone: 1.123.456.7890\nEmail: john@company.com", 
            font, brush, new System.Drawing.RectangleF(32, 150, 170, 40), TextAlignment.Left);
        page1.DrawText(boundedText);

        font = page1.CreateFont("Verdana", 18);
        brush = new SolidBrush(RgbColor.Black);
        point = new System.Drawing.PointF(14, 96);
        plainText = new PlainText("John Doe", font, brush, point);
        page1.DrawText(plainText);
    }

    writer.AddPage(350, 200);
    using (var page2 = writer.GetGraphics())
    {
        var font = page2.CreateFont("Times New Roman", 16);
        var brush = new SolidBrush(RgbColor.Blue);
        var point = new System.Drawing.PointF(82, 92);
        var plainText = new PlainText("http://company.com", font, brush, point);
        page2.DrawText(plainText);
    }
}
```
 
The resulting business card will look as follows:
 ![PDF business card](https://www.graphicsmill.com/docs/gm/PdfBusinessCard.png)
 
## Using PdfReader
  
The [PdfReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfReader.htm.md) class allows you to load PDF files. After you read a file, you can process it as a raster or vector graphics.
 
Let us look at the simplest use case, when you need to rasterize a one-page PDF file.
 
```csharp
var dpi = 90f;
var quality = 90;
using (var reader = new PdfReader(@"Images\in.pdf", dpi, dpi))
using (var writer = new JpegWriter(@"Images\Output\out.jpg", quality, false, true))
{
    Pipeline.Run(reader + writer);
}
```
 
In this example, you assign the **reader** and **writer** objects to set up the source and destination files. Then, you run the [Pipeline](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Pipeline.htm.md) to convert an image from PDF to JPG. In this case, implicit rasterization occurs in the pipeline.
 
When you work with vector graphics, [GraphicsContainer](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_GraphicsContainer.htm.md) is used to handle vector images without rasterization. Another image format of vector graphics that Graphics Mill supports is the SVG format. To illustrate how you can work with **GraphicsContainer**, let us convert a PDF to an SVG file.
 
```csharp
using (var reader = new PdfReader(@"Images\in.pdf", dpi, dpi))
using (var container = reader.Frames[0].GetContent())
using (var writer = new SvgWriter(@"Images\Output\out.svg", reader.Width, reader.Height))
using (var graphics = writer.GetGraphics())
{
    graphics.DrawContainer(container, 0, 0);
}
```
 
Here, [PdfFrame.GetContent()](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfFrame_GetContent.htm.md) returns **GraphicsContainer** with vector graphics that you then pass to the [Graphics.DrawContainer(GraphicsContainer, Single, Single)](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawContainer_3_098aa7ce.htm.md) method to draw it on the **writer** object and save into the output file in the vector form. If you only want to rasterize the source image, you do not need to call the **PdfFrame.GetContent()** method; use [Frame.GetBitmap()](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_Frame_GetBitmap.htm.md) instead.
 
## See Also
  
### Reference
 
- [PdfWriter Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfWriter.htm.md)
- [PdfPage Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfPage.htm.md)
- [PdfSettings Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfSettings.htm.md)
- [UnitConverter Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_UnitConverter.htm.md)
- [UnitConverter.ConvertUnitsToPixels(Single, Single, Unit) Method](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_UnitConverter_ConvertUnitsToPixels_3_1df1b0a2.htm.md)
- [Pipeline Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Pipeline.htm.md)
- [PdfReader Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfReader.htm.md)
- [SvgWriter Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_SvgWriter.htm.md)

### Manual
 
- [Working with Files](https://www.graphicsmill.com/docs/gm/working-with-files.htm.md)
- [Supported File Formats](https://www.graphicsmill.com/docs/gm/supported-file-formats.htm.md)
- [Manipulating Vector Images with Graphics Containers](https://www.graphicsmill.com/docs/gm/graphics-container.htm.md)
