// Get DfpUser from "~/dfp.properties".
DfpUser user = new DfpUser();
// Get the CreativeService.
CreativeServiceInterface creativeService =
user.getService(DfpService.V201308.CREATIVE_SERVICE);
// Set the ID of the advertiser (company) that all creative will be
// assigned to.
Long advertiserId = Long.parseLong("INSERT_ADVERTISER_COMPANY_ID_HERE");
// Use the image banner with optional third party tracking template.
Long creativeTemplateId = 10000680L;
// Create the local custom creative object.
TemplateCreative templateCreative = new TemplateCreative();
templateCreative.setName("Template creative");
templateCreative.setAdvertiserId(advertiserId);
templateCreative.setCreativeTemplateId(creativeTemplateId);
// Set the creative size.
templateCreative.setSize(new Size(300, 250, false));
// Create the asset variable value.
AssetCreativeTemplateVariableValue assetVariableValue =
new AssetCreativeTemplateVariableValue();
assetVariableValue.setUniqueName("Imagefile");
assetVariableValue.setAssetByteArray(MediaUtils.getAssetDataFromUrl(
"http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg"));
// Filenames must be unique.
assetVariableValue.setFileName(String.format("image%s.jpg", System.currentTimeMillis()));
// Create the image width variable value.
LongCreativeTemplateVariableValue imageWidthVariableValue =
new LongCreativeTemplateVariableValue();
imageWidthVariableValue.setUniqueName("Imagewidth");
imageWidthVariableValue.setValue(300L);
// Create the image height variable value.
LongCreativeTemplateVariableValue imageHeightVariableValue =
new LongCreativeTemplateVariableValue();
imageHeightVariableValue.setUniqueName("Imageheight");
imageHeightVariableValue.setValue(250L);
// Create the URL variable value.
UrlCreativeTemplateVariableValue urlVariableValue =
new UrlCreativeTemplateVariableValue();
urlVariableValue.setUniqueName("ClickthroughURL");
urlVariableValue.setValue("www.google.com");
// Create the target window variable value.
StringCreativeTemplateVariableValue targetWindowVariableValue =
new StringCreativeTemplateVariableValue();
targetWindowVariableValue.setUniqueName("Targetwindow");
targetWindowVariableValue.setValue("__blank");
templateCreative.setCreativeTemplateVariableValues(new BaseCreativeTemplateVariableValue[] {
assetVariableValue, imageWidthVariableValue, imageHeightVariableValue, urlVariableValue,
targetWindowVariableValue});
// Create the template creative on the server.
templateCreative = (TemplateCreative) creativeService.createCreative(templateCreative);
if (templateCreative != null) {
System.out.println("A template creative with ID \"" + templateCreative.getId()
+ "\", name \"" + templateCreative.getName() + "\", and type \""
+ templateCreative.getCreativeType() + "\" was created and\n"