Tutorials

Edit PDFs Programmatically Using C#

By ComPDFKit | Wed. 19 Oct. 2022
C#Document Editor

We could see there are lots of applications that could edit PDFs because of widely used PDF documents. More and more app owners realize that providing features for editing PDFs makes their app more practiced and welcomed.

 

In this blog post, we will talk about the whole steps from integration to editing PDFs. Some feature examples will be displayed in the following part like inserting PDFs, annotating PDFs, etc.  

 

 

Why Use ComPDFKit to Edit PDFs?

 

ComPDFKit could be trusted for the following reasons.

 

- The Mature PDF Features: The technologies of ComPDFKit are tested and used by millions of users.

- The Complete Guides: The guides of ComPDFKit contain multiple development platforms and explain all the features.

- Flexible License: You could choose and purchase the features just as you need.

- High-quality Service: The experienced support team of ComPDFKit will help with your questions within 24h.

- Easy-to-use Core API: Integrate with a few lines of code even if you have no deep knowledge of PDF documents.

 

ComPDFKit combines PDF SDK & Conversion SDK. Here we are going to take the PDF SDK in C# as an example. 

 

 

Integrate and Initialize ComPDFKit

 

You need to contact our sales for downloading the ComPDFKit PDF SDK package and try it. A one-month license key will also be sent along with the package. After getting the SDK package of ComPDFKit PDF SDK, you need to add ComPDFKit PDF SDK dynamic library to C# project. And then, apply the license key. You can take a deep look at the following code example. For more details about how to integrate ComPDFKit PDF SDK, please turn to the documentation part.

 

bool LicenseVerify()
{
    if (!CPDFSDKVerifier.LoadNativeLibrary())
        return false;

    LicenseErrorCode verifyResult = CPDFSDKVerifier.LicenseVerify("Input your license here.", false);
    return (verifyResult == LicenseErrorCode.E_LICENSE_SUCCESS);
}



Import a PDF File

 

Once you finish integrating the ComPDFKit PDF SDK, a PDF document could be opened and displayed by the CPDFDocument object.

 

// Get the path of a PDF
string filePath ="";
var dlg = new OpenFileDialog();
dlg.Filter = "PDF Files (*.pdf)|*.pdf";
if (dlg.ShowDialog() == true)
{
filePath = dlg.FileName;
}
else
{
return;
}
// Initialize a CPDFDocument object with the path to the PDF file

CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
if(document==null)
{
return;
}
if(document.ErrorType != CPDFDocumentError.CPDFDocumentErrorSuccess
&& document.ErrorType != CPDFDocumentError.CPDFDocumentPasswordError)
{
return;
}

 

 

Edit the PDF

 

The mature and complete PDF features are included in ComPDFKit which contains viewing, editing, annotating, form filling, converting, security, redacting, OCR, navigating, etc. In the following section, we will introduce how to make some of the features possible. If you are interested in other features, please read the related documentation or blogs.

 

Insert PDF pages:

Assume that you want to add the first PDF page which is in another PDF file. And you want to add the page before the first PDF page. Follow the code example here and try to do it.

 

CPDFDocument otherDocument = CPDFDocument.InitWithFilePath("***");
document.ImportPagesAtIndex(otherDocument, "0", 0);
otherDocument.Release();

 

Annotation: Add text explanation

After inserting the PDF pages, other processes will also be needed. For instance, you could use text annotations when there is a line of words that may need more details to explain. Here, you can find the methods about how to add text annotations.

 

Annotations: Protrude the content of the PDF

Now, there are some keywords that you need to protrude. ComPDFKit supports normal approaches to do that, such as highlighting, underlining, ink, and more. The following code shows the method to highlight all the keywords “Step” on page 5.

 

CPDFPage page = document.PageAtIndex(4);
if (page == null)
    return;

List rects = new List();
CPDFTextPage textPage = page.GetTextPage();
CPDFTextSearcher searcher = new CPDFTextSearcher();
int findIndex = 0;

if (searcher.FindStart(textPage, "Step", C_Search_Options.Search_Case_Sensitive, findIndex))
{
    CRect textRect = new CRect();
    string textContent = "";
    while (searcher.FindNext(page, textPage, ref textRect, ref textContent, ref findIndex))
    {
        rects.Add(textRect);
    }
}
searcher.FindClose();

CPDFHighlightAnnotation highlight = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
byte[] color = { 0, 255, 0 };
highlight.SetColor(color);
highlight.SetTransparency(120);
highlight.SetQuardRects(rects);
highlight.UpdateAp();

 

 

Save the Changes You Made

 

After making all the changes, we could save it and finish our use. Here is the code method to save all the changes you made in a new PDF file.

 

document.WriteToFilePath("newFilePath");
document.Release();

 

 

Conclusion

 

There are also some PDF features that are supported by ComPDFKit. It’s easy to integrate these features into your applications or systems. Take a look at our documentation page to see more SDK feature details and contact ComPDFKit sales for a free trial.