Notes
public class ContentVersionUtility {
public static void NewPDF(){
Contact ac = new Contact();
ac.LastName='Bank';
insert ac;
Blob fileBody;
String filePath = 'fileName.pdf';
fileBody = [SELECT Body FROM StaticResource WHERE Name = 'YourStaticResourceName'].Body;
ContentVersion version = new ContentVersion(
Title = 'New',
PathOnClient = 'fileName.pdf',
VersionData = fileBody,
Origin = 'H'
);
insert version;
ContentVersion newContentVersion = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :version.id];
ContentDocumentLink link = new ContentDocumentLink(
LinkedEntityId = ac.id,
ShareType = 'V',
ContentDocumentId = newContentVersion.ContentDocumentId
);
insert link;
}
}