private static final String ADVERTISER_ID = "INSERT_ADVERTISER_COMPANY_ID_HERE";
public static void runExample(DfpServices dfpServices, DfpSession session, long advertiserId)
throws Exception {
// Get the CreativeService.
CreativeServiceInterface creativeService =
dfpServices.get(session, CreativeServiceInterface.class);
// Create creative size.
Size size = new Size();
size.setWidth(300);
size.setHeight(250);
size.setIsAspectRatio(false);
// Create a custom creative.
CustomCreative customCreative = new CustomCreative();
customCreative.setName("Custom creative #" + new Random().nextInt(Integer.MAX_VALUE));
customCreative.setAdvertiserId(advertiserId);
customCreative.setDestinationUrl("http://google.com");
customCreative.setSize(size);
// Set the custom creative image asset.
CustomCreativeAsset customCreativeAsset = new CustomCreativeAsset();
customCreativeAsset.setMacroName("IMAGE_ASSET");
customCreativeAsset.setAssetByteArray(Media.getMediaDataFromUrl(
"http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg"));
// Filenames must be unique.
customCreativeAsset.setFileName(
String.format("image%s.jpg", new Random().nextInt(Integer.MAX_VALUE)));
customCreative.setCustomCreativeAssets(new CustomCreativeAsset[] {customCreativeAsset});
// Set the HTML snippet using the custom creative asset macro.
customCreative.setHtmlSnippet("<a href='%%CLICK_URL_UNESC%%%%DEST_URL%%'>" +
"<img src='%%FILE:" + customCreativeAsset.getMacroName() + "%%'/>" +
"</a><br>Click above for great deals!");
// Create the creative on the server.
Creative[] creatives =
creativeService.createCreatives(new Creative[] {customCreative});
for (Creative createdCreative : creatives) {
System.out.printf("A creative with ID \"%d\", name \"%s\", and type \"%s\""
+ " was created and can be previewed at: %s\n", createdCreative.getId(),
createdCreative.getName(), createdCreative.getCreativeType(),