Last active
March 12, 2021 19:04
-
-
Save groupdocs-cloud-gists/77afaca784c16e5cec9d560ac497de4e to your computer and use it in GitHub Desktop.
Update Metadata of your PDF documents programmatically using a REST API in C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Edit Metadata of PDF Documents | |
1. Programmatically upload a PDF file on the cloud | |
2. Update Metadata of PDF Documents programmatically on the cloud using a REST API in C#. | |
3. Download the updated file from the cloud. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string clientID = "112f0f38-9dae-42d5-b4fc-cc84ae644972"; | |
string clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618"; | |
string myStorage = ""; | |
Configuration configuration = new Configuration(clientID, clientSecret); | |
configuration.ApiBaseUrl = "https://api.groupdocs.cloud"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# api initialization | |
var fileApi = new FileApi(configuration); | |
var file = "metadata\\set_metadata\\input_pdf\\input.pdf"; | |
var downloadRequest = new DownloadFileRequest(file, myStorage); | |
Stream downloadResponse = fileApi.DownloadFile(downloadRequest); | |
using (var fileStream = File.Create("C:\\Files\\sample_input.pdf")) | |
{ | |
downloadResponse.Seek(0, SeekOrigin.Begin); | |
downloadResponse.CopyTo(fileStream); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# api initialization | |
var apiInstance = new MetadataApi(configuration); | |
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo | |
{ | |
FilePath = "input.pdf", | |
StorageName = myStorage | |
}; | |
// Define Set Options | |
var options = new SetOptions | |
{ | |
FileInfo = fileInfo, | |
Properties = new List<SetProperty> | |
{ | |
new SetProperty | |
{ | |
NewValue = "This is title", | |
Type = "String", | |
SearchCriteria = new SearchCriteria | |
{ | |
NameOptions = new NameOptions | |
{ | |
Value = "Title", | |
MatchOptions = new MatchOptions | |
{ | |
ExactPhrase = true | |
} | |
} | |
}, | |
} | |
} | |
}; | |
// Define Set Request | |
var request = new SetRequest(options); | |
var response = apiInstance.Set(request); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# api initialization | |
var apiInstance = new MetadataApi(configuration); | |
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo | |
{ | |
FilePath = "input.pdf", | |
StorageName = myStorage | |
}; | |
// Define Set Options | |
var options = new SetOptions | |
{ | |
FileInfo = fileInfo, | |
Properties = new List<SetProperty> | |
{ | |
new SetProperty | |
{ | |
NewValue = "new value", | |
Type = "String", | |
SearchCriteria = new SearchCriteria | |
{ | |
NameOptions = new NameOptions | |
{ | |
Value = "^Tit.*", | |
MatchOptions = new MatchOptions | |
{ | |
IsRegex = true | |
} | |
} | |
}, | |
} | |
} | |
}; | |
// Define Set Request | |
var request = new SetRequest(options); | |
var response = apiInstance.Set(request); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# api initialization | |
var apiInstance = new MetadataApi(configuration); | |
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo | |
{ | |
FilePath = "input.pdf", | |
StorageName = myStorage | |
}; | |
// Define Set Options | |
var options = new SetOptions | |
{ | |
FileInfo = fileInfo, | |
Properties = new List<SetProperty> | |
{ | |
new SetProperty | |
{ | |
NewValue = "Simply set by Property Value", | |
Type = "String", | |
SearchCriteria = new SearchCriteria | |
{ | |
ValueOptions = new ValueOptions | |
{ | |
Value = "Windows User", | |
Type = "String" | |
} | |
}, | |
} | |
} | |
}; | |
// Define Set Request | |
var request = new SetRequest(options); | |
var response = apiInstance.Set(request); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# api initialization | |
var apiInstance = new MetadataApi(configuration); | |
try | |
{ | |
var fileInfo = new GroupDocs.Metadata.Cloud.Sdk.Model.FileInfo | |
{ | |
FilePath = "input.pdf", | |
StorageName = myStorage | |
}; | |
// Define Set Options | |
var options = new SetOptions | |
{ | |
FileInfo = fileInfo, | |
Properties = new List<SetProperty> | |
{ | |
new SetProperty | |
{ | |
NewValue = "hello", | |
Type = "String", | |
SearchCriteria = new SearchCriteria | |
{ | |
NameOptions = new NameOptions | |
{ | |
Value = "Keywords" | |
} | |
}, | |
} | |
} | |
}; | |
// Define Set Request | |
var request = new SetRequest(options); | |
var response = apiInstance.Set(request); | |
Console.WriteLine($"Count of changes: {response.SetCount}"); | |
Console.WriteLine("Resultant file path: " + response.Path); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Exception while calling MetadataApi: " + e.Message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# api initialization | |
FileApi fileApi = new FileApi(configuration); | |
string path = @"C:\Files"; | |
var file = Directory.GetFiles(path, "input.pdf", SearchOption.AllDirectories).FirstOrDefault(); | |
if (file.Length != 0) | |
{ | |
var relativeFilePath = file.Replace(path, string.Empty).Trim(Path.DirectorySeparatorChar); | |
var fileStream = File.Open(file, FileMode.Open); | |
fileApi.UploadFile(new UploadFileRequest(relativeFilePath, fileStream, myStorage)); | |
fileStream.Close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment