HiQPdf Documentation

Edit PDF Documents

Quickly Create High Quality PDFs

The HiQPdf software is able to load external PDF documents and to add new objects over the existing content.

Edit PDF Demo

In this demo you can learn how to load a PDF document from a file and layout various PDF objects on top of the existing content.

The demo will make two changes to the PDF document to edit: will create an orange border for each PDF page of the loaded document and will create a canvas whose content is automatically repeated on each PDF page. The content from a HTML document will be laid out in the repeated canvas.

The HTML document does not have a background which makes visible the existing content from PDF and even more, the PNG image used in HTML is also transparent to create a very special effect.

Demo Source Code

C#
private void buttonEditPdf_Click_1(object sender, EventArgs e)
{
    // the path PDF document to edit
    string pdfDocumentToEdit = Application.StartupPath + @"\DemoFiles\Pdf\WikiPdf.pdf";

    // load the PDF document to edit
    PdfDocument document = PdfDocument.FromFile(pdfDocumentToEdit);

    #region Add an orange border to each page of the loaded PDF document

    // add an orange border to each PDF page in the loaded PDF document
    foreach (PdfPage pdfPage in document.Pages)
    {
        float crtPdfPageWidth = pdfPage.Size.Width;
        float crtPdfPageHeight = pdfPage.Size.Height;

        // create a PdfRectangle object
        PdfRectangle pdfRectangle = new PdfRectangle(2, 2, crtPdfPageWidth - 4, crtPdfPageHeight - 4);
        pdfRectangle.LineStyle.LineWidth = 2;
        pdfRectangle.ForeColor = Color.OrangeRed;

        // layout the rectangle in PDF page
        pdfPage.Layout(pdfRectangle);
    }

    #endregion Add an orange border to each page of the loaded PDF document

    #region Layout HTML in a canvas to be repeated on each page of the loaded PDF document

    PdfPage pdfPage1 = document.Pages[0];
    float pdfPageWidth = pdfPage1.Size.Width;
    float pdfPageHeight = pdfPage1.Size.Height;

    // the width of the HTML logo in pixels
    int htmlLogoWidthPx = 400;
    // the width of the HTML logo in points
    float htmlLogoWidthPt = PdfDpiTransform.FromPixelsToPoints(htmlLogoWidthPx);
    float htmlLogoHeightPt = 100;

    // create a canvas to be repeated in the center of each PDF page
    // the canvas is a PDF container that can contain PDF objects ( HTML, text, images, etc )
    PdfRepeatCanvas repeatedCanvas = document.CreateRepeatedCanvas(new RectangleF((pdfPageWidth - htmlLogoWidthPt) / 2, (pdfPageHeight - htmlLogoHeightPt) / 2,
                htmlLogoWidthPt, htmlLogoHeightPt));

    // the HTML file giving the content to be placed in the repeated canvas
    string htmlFile = Application.StartupPath + @"\DemoFiles\Html\Logo.Html";

    // the HTML object to be laid out in repeated canvas
    PdfHtml htmlLogo = new PdfHtml(0, 0, repeatedCanvas.Width, repeatedCanvas.Height, htmlFile);
    // the browser width when rendering the HTML
    htmlLogo.BrowserWidth = htmlLogoWidthPx;
    // the HTML content will fit the destination hight which is the same with repeated canvas height
    htmlLogo.FitDestHeight = true;

    // layout the HTML object in the repeated canvas
    PdfLayoutInfo htmlLogLayoutInfo = repeatedCanvas.Layout(htmlLogo);

    #endregion Layout HTML in a canvas to be repeated on each page of the loaded PDF document

    Cursor = Cursors.WaitCursor;
    string pdfFile = Application.StartupPath + @"\DemoOutput\EditPdf.pdf";
    try
    {
        document.WriteToFile(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Cannot create the PDF document. {0}", ex.Message));
        return;
    }
    finally
    {
        document.Close();
        Cursor = Cursors.Arrow;
    }

    // open the created PDF document
    try
    {
        System.Diagnostics.Process.Start(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("The PDF document was created but cannot open '{0}'. {1}", pdfFile, ex.Message));
    }
}
See Also

Other Resources